-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwitr
More file actions
executable file
·140 lines (119 loc) · 4.22 KB
/
witr
File metadata and controls
executable file
·140 lines (119 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
#--------------------------------------------------------------------------
#
# ▗▐
# ▌ ▌▄▜▀ ▙▀▖
# ▐▐▐ ▐▐ ▖▌
# ▘▘ ▀▘▀ ▘
#
# Description:
# Translate a term by looking it up in Wikipedia, through interwiki
#
#--------------------------------------------------------------------------
#
# Found this useful? Appalling? Appealing? Please let me know.
# The Unabashed welcomes your impressions.
#
# You will find the
# unabashed
# at the location opposite to
# moc • thgimliam
#
#--------------------------------------------------------------------------
#
# License:
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
#--------------------------------------------------------------------------
# Program name from its filename
prog=${0##*/}
# Some colors
LIGHTRED='\e[1;31m'
LIGHTPURPLE='\e[1;35m'
YELLOW='\e[1;33m'
NC='\e[0m'
# Calling for help is the same as calling without arguments.
case $1 in --help|-[h?]) $prog; exit 1 ;; esac
# Usage if less than three arguments are given
[[ $# -lt 3 ]] && {
clear
echo -e "
${LIGHTRED}Description:${NC}
Translate a term by looking it up in Wikipedia, through interwiki.
${LIGHTRED}Usage:${NC}
${YELLOW}$prog [lang_origin] [lang_destination] terms${NC}
${LIGHTRED}Examples:${NC}
Try this for Japanese translations:
${YELLOW}$prog en ja Spiderman roller_coaster gnome \"Akira Kurosawa\"${NC}
[en] Spiderman [ja] スパイダーマン
[en] roller_coaster [ja] ローラーコースター
[en] gnome [ja] ノーム (妖精)
[en] Akira Kurosawa [ja] 黒澤明
Try this for Korean translations:
${YELLOW}$prog en ko Spiderman roller_coaster kimchi Seoul${NC}
[en] Spiderman [ko] 스파이더맨
[en] roller_coaster [ko] 롤러코스터
[en] kimchi [ko] 김치
[en] Seoul [ko] 서울특별시
${LIGHTRED}Dependencies:${NC}
Must have curl and xsel installed.
Try ${YELLOW}sudo apt-get install curl xsel${NC}
"
exit 1
}
#----------------------------------------------------------------------------
# Check for missing commands
needed_commands="curl sed xsel"
missing_counter=0
for needed_command in $needed_commands; do
if ! hash "$needed_command" >/dev/null 2>&1; then
printf "Command not found in PATH: %s\n" "$needed_command" >&2
: $((missing_counter++))
fi
done
if [[ $missing_counter -eq 1 ]]; then
printf "At least %d command is missing, install it\n" "$missing_counter" >&2
exit 1
elif [[ $missing_counter -gt 1 ]]; then
printf "At least %d commands are missing, install them\n" "$missing_counter" >&2
exit 2
fi
#----------------------------------------------------------------------------
lang_orig="$1"
lang_dest="$2"
shift 2
# Temp files
mkdir -p $HOME/tmp &&
tmpfile=$(mktemp --tmpdir=$HOME/tmp ${PROG}.XXXXXXXXXX)
while [[ $# -ne 0 ]]
do
term_orig="$1"
term_dest=""
term_dest=$(\
curl -s -L "https://$lang_orig.wikipedia.org/wiki/$term_orig" |
sed 's,/li><li,/li>\n<li,g' |
grep 'li class="interlanguage-link interwiki' |
grep "interwiki\-$lang_dest" |
sed 's|.*title="\([^"]*\) –.*|\1|')
input="[$lang_orig] $term_orig"
[[ "$term_dest" == "" ]] &&
output="[$lang_dest] * not found *" ||
output="[$lang_dest] $term_dest"
echo "$input = $output" >> $tmpfile
shift
done
<$tmpfile column -s= -t
rm $tmpfile
exit 0
#------------------END of PROGRAM----------------------------