-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathduckduckgo.sh
76 lines (65 loc) · 2.03 KB
/
duckduckgo.sh
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
#!/bin/bash
# Note: This is only a beta version
# Date: 2021/09/19
# 0x0d1n
if [ $# -eq 0 ]; then
echo "usage: ${0##*/} [query]" >&2
exit 2
fi
red=`tput setaf 1`
reset=`tput sgr0`
UA='Mozilla/5.0 (Windows NT 10.0; rv:81.0) Gecko/20100101 Firefox/81.0'
PER_PAGE=50
START_PAGE=0
declare -n page=START_PAGE # like pointer
function urlencode {
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf "$c" | xxd -p -c1 | while read x;do printf "%%%s" "$x";done
esac
done
}
function search {
echo "Searching for \"$1\"..." > /dev/tty
for i in {1..5}; do
html=$(curl -A "$UA" -s 'https://duckduckgo.com/?q='$(urlencode "$1")'&t=hx&va=g')
[ ${#html} -gt 0 ] && break || return 1
done
link=$(sed 's/;/\n/g' <<< "$html" | grep 'initialize' | sed "s/')//g" | sed 's/.*\//https:\/\/links.duckduckgo.com\//g')
link=$(sed -r 's/&s=[0-9]{1,}&/\&s='$page'\&/g' <<< "$link")
for i in {1..5};do
data=$(curl -A "$UA" -s "$link")
[ ${#data} -gt 0 ] && { echo "$data"; break; } || return 1
done
}
function format {
c=1
sed 's/,/\n/g' <<< "$1" | grep '"t":\|"u"' | awk -F'":"' '{print $2}' | sed 's/"//g' \
| sed 's/}//g' | perl -MHTML::Entities -pe 'decode_entities($_);' \
| while read -r line; do
printf '%b\n' "$line"
if [[ $c -ge 2 ]]; then
echo; c=0
fi
let c++
done
}
kw="$@"
format "$(search "$kw")"
while true; do
read -n 1 -p "${red}Show More?[Y/n]: ${reset}" show_more
echo
case $show_more in
y)
let START_PAGE+=$PER_PAGE
format "$(search "$kw")"
;;
*)
break
;;
esac
done
exit 0