-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternet-search.sh
More file actions
executable file
·249 lines (223 loc) · 6.47 KB
/
internet-search.sh
File metadata and controls
executable file
·249 lines (223 loc) · 6.47 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#! /bin/sh
#
# A little script to search with different online search engines from the
# commandline.
#
# Author: luc
# Thanks:
# * This script is based on an idea of johnraff @ crunchbanglinux.org/forums
# see http://crunchbanglinux.org/forums/post/87022/#p87022
#
# dependencies:
# xsel, firefox, lynx, elinks
#
# TODO list
# add options to urls
# helper functions {{{1
urlencode () {
# ideas from
# http://stackoverflow.com/questions/296536/urlencode-from-a-bash-script
# encode the argument to be used in search urls.
#perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$@"
perl -MURI::Escape -e 'print join("+", map{uri_escape $_}(@ARGV));' "$@"
}
insert_quotes () {
local string=
for arg; do
if echo "$arg" | grep ' ' >/dev/null; then
string="$string+\"$arg\""
else
string="$string+$arg"
fi
done
echo "${string#+}"
}
# functions to create the correct url which can be handed to a browser {{{1
create_commandlinefu_url () {
local base='http://www.commandlinefu.com/commands/matching/'
#SEARCH+="/`echo -n "$SEARCH" | openssl base64`"
#TEXT="wget --quiet -O -"
#if [ $TYPE = TEXT ]; then OPTIONS=/plaintext
#else OPTIONS=/sort-by-votes
#fi
}
create_duckduckgo_url () {
local base='https://duckduckgo.com/?q='
echo "$base`insert_quotes "$@"`"
}
create_google_url () {
local base='http://www.google.de/search?q='
echo "$base`insert_quotes "$@"`"
}
create_leo_dict_url () {
local base='http://dict.leo.org/ende?search='
echo "$base`insert_quotes "$@"`"
}
create_mvv_url () {
# $1 -> start
# $2 -> destination
# $3 -> time (optional)
local base='http://www.mvv-muenchen.de/de/fahrplanauskunft/index.html'
if [ $# -lt 2 ]; then
echo error >&2
exit 2
fi
name_origin=`urlencode "$1"`
name_destination=`urlencode "$2"`
echo "$base?name_origin=$name_origin&name_destination=$name_destination"
}
create_wikipedia_url () {
local base='http://de.wikipedia.org/w/index.php?fulltext=Search&search='
# 'http://en.wikipedia.org/w/index.php?fulltext=Search&search='
echo "$base`insert_quotes "$@"`"
}
create_youtube_url () {
local base='http://www.youtube.com/results?search_query='
echo "$base`insert_quotes "$@"`"
}
# urls
CMDFU='http://www.commandlinefu.com/commands/matching/'
DUCKDUCKGO='https://duckduckgo.com/?q='
GOOGLE='http://www.google.de/search?q='
LEO='http://dict.leo.org/ende?search='
WIKI='http://de.wikipedia.org/w/index.php?fulltext=Search&search='
WIKIEN='http://en.wikipedia.org/w/index.php?fulltext=Search&search='
YOUTUBE='http://www.youtube.com/results?search_query='
# Preparing the different commands depending on the system. TYPE may be one of
# XORG CLI TEXT
XORG=
CLI=lynx
TEXT='elinks --dump'
FALLBACK='wget --output-document=-'
TYPE=XORG
for CLI in lynx elinks links w3m w3; do
if which $CLI >/dev/null 2>&1; then
break
fi
CLI=
done
if [ -z $CLI ]; then
echo 'Error: No command line browser found!' >&2
fi
case `uname` in
linux|Linux|LINUX) XORG=firefox CLIPBOARD=xsel;;
Darwin) XORG=open CLIPBOARD=pbpaste;;
*) TYPE=CLI;; #unknown system. some kind of unix?
esac
help () {
echo search engines:
echo ' -d duckduckgo.com'
echo ' -f www.commandlinefu.com'
echo ' -g www.google.de'
echo ' -l dict.leo.org'
echo ' -w de.wikipedia.org'
echo ' -y www.youtube.com'
echo
echo other options:
echo ' -c use a command line browser'
echo ' -t display results as text'
echo ' -x use a graphical browser'
}
while getopts cdfghltwxy FLAG; do
case $FLAG in
# general options
h) help; exit;;
# type options
c) TYPE=CLI;;
t) TYPE=TEXT;;
x) TYPE=XORG;;
# search engine options
d) BASE="$DUCKDUCKGO";;
f) BASE="$CMDFU";;
g) BASE="$GOOGLE";;
l) BASE="$LEO";;
w) BASE="$WIKI";;
y) BASE="$YOUTUBE";;
esac
#OPTIONS=??
done
shift $((OPTIND-1))
if [ -z "$BASE" ]; then
echo "Which search engine should be used?" >&2
exit 1
fi
# Preparing the searchterms and puting them together for the URL.
if [[ "$1" = *\ * ]]; then SEARCH="\"$1\""; else SEARCH="$1"; fi
shift
for TERM in "$@"; do
if [[ "$TERM" = *\ * ]]; then
SEARCH="$SEARCH+\"$TERM\""
else
SEARCH="$SEARCH+$TERM"
fi
done
if [ -z "$SEARCH" ]; then SEARCH=`eval $CLIPBOARD`; fi
if [ -z "$SEARCH" ]; then echo "No search terms."; exit 2; fi
if [[ "$BASE" = *commandlinefu* ]]; then
SEARCH+="/`echo -n "$SEARCH" | openssl base64`"
TEXT="wget --quiet -O -"
if [ $TYPE = TEXT ]; then OPTIONS=/plaintext
else OPTIONS=/sort-by-votes
fi
fi
# Execute the search. Pick the specified method for it.
COMMAND=${!TYPE}
${COMMAND} "${BASE}${SEARCH}${OPTIONS}"
exit
####### VERSION 1.0 by johnraff@crunchbanglinux.org/forums ############
#!/bin/bash
# g.sh google search on inputted string
# needs xsel to read from clipboard
# edit to taste:
REGULAR_BROWSER=firefox
QUICK_BROWSER=dillo
#############################################
if [[ -n "$1" ]] # get input from command line or clipboard, put in array
then
terms=("$@")
else
terms=($(xsel))
fi
for i in ${!terms[@]} # put back quotes
do
[[ ${terms[$i]} = *\ * ]] && terms[$i]=\"${terms[$i]}\"
done
SEARCH="${terms[*]}"
SEARCH=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$SEARCH") # from: http://stackoverflow.com/questions/296536/urlencode-from-a-bash-script
if pgrep $REGULAR_BROWSER >/dev/null
then
BROWSER=$REGULAR_BROWSER
else
BROWSER=$QUICK_BROWSER
fi
# Feel free to change the google request string if you know what those options do...
# The "#res" at the end skips the stuff at the top of the page and takes you straight to the search results.
$BROWSER "http://www.google.co.jp/search?as_q=${SEARCH}&num=25&hl=en&btnG=Google+Search&as_qdr=all&as_occt=any&safe=off#res"
exit
############################## SOME OLD FUNCTIONS ##########################
cmdfu () {
if [ -z "$1" ]; then
echo "Give an arguemnt." 1>&2
return 2
fi
local search
search="http://www.commandlinefu.com/commands/matching/$@/"
search="$search`echo -n "$@" | openssl base64`/plaintext"
wget --quiet -O - "$search"
}
wiki () {
# http://www.commandlinefu.com/commands/view/10813
dig +short txt $1.wp.dg.cx
}
yt () {
# from:
# http://www.commandlinefu.com/commands/view/6689/stream-youtube-url-directly-to-mplayer
mplayer -fs -quiet $(youtube-dl -g "$1")
}
dict() {
# from:
# http://www.commandlinefu.com/commands/view/1884/look-up-the-definition-of-a-word
#curl dict://dict.org/d:$1
curl -s dict://dict.org/d:$1 | \
perl -ne 's/\r//; last if /^\.$/; print if /^151/../^250/'
}