-
Notifications
You must be signed in to change notification settings - Fork 0
/
rofi-web-search.py
executable file
·189 lines (175 loc) · 7.55 KB
/
rofi-web-search.py
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
#!/usr/bin/env python3
import json
import re
import urllib.parse
import urllib.request
import sys
import os
import datetime
import gzip
import subprocess as sp
import html
################################################################################
##### C O N F I G U R A T I O N ######
################################################################################
SEARCH_ENGINE = 'brave-search' # or 'duckduckgo'
BROWSER = 'brave-beta' # or 'firefox', 'chromium', 'brave', 'lynx'
TERMINAL = ['gnome-terminal', '--'] # or ['st', '-e'] or something like that
################################################################################
CONFIG = {
'BROWSER_PATH' : {
'chrome' : ['google-chrome-stable'],
'firefox' : ['firefox'],
'chromium' : ['chromium-browser'],
'brave' : ['brave-browser'],
'brave-beta' : ['brave-beta'],
'lynx' : TERMINAL + ['lynx']
},
'USER_AGENT' : {
'chrome' : 'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36',
'firefox' : 'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:69.0) Gecko/20100101 Firefox/69.0',
'chromium' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/76.0.3809.100 Chrome/76.0.3809.100 Safari/537.36',
'brave' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36',
'brave-beta' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36',
'lynx' : 'Lynx/2.8.9rel.1 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/1.1.1d'
},
'SEARCH_ENGINE_NAME' : {
'google' : 'Google',
'duckduckgo' : 'DuckDuckGo',
'brave-search' : 'Brave Search'
},
'SEARCH_URL' : {
'google' : 'https://www.google.com/search?q=',
'duckduckgo' : 'https://duckduckgo.com/?q=',
'brave-search' : 'https://search.brave.com/search?q='
},
'SUGGESTION_URL' : {
'google' : 'https://www.google.com/complete/search?',
'duckduckgo' : 'https://duckduckgo.com/ac/?'
},
}
SUGGESTIONS_INCOMPATIBLE = ('brave-search')
def cleanhtml(txt):
return re.sub(r'<.*?>', '', txt)
def fetch_suggestions(search_string):
if SEARCH_ENGINE == 'google':
r = {
'q' : search_string,
'cp' : '11',
'client' : 'psy-ab',
'xssi' : 't',
'gs_ri' : 'gws-wiz',
'hl' : 'en-IT',
'authuser' : '0'
}
url = CONFIG['SUGGESTION_URL'][SEARCH_ENGINE] + urllib.parse.urlencode(r)
headers = {
'sec-fetch-mode' : 'cors',
'dnt' : '1',
'accept-encoding' : 'gzip',
'accept-language' : 'en-US;q=0.9,en;q=0.8',
'pragma' : 'no-cache',
'user-agent' : CONFIG['USER_AGENT'][BROWSER],
'accept' : '*/*',
'cache-control' : 'no-cache',
'authority' : 'www.google.com',
'referer' : 'https://www.google.com/',
'sec-fetch-site' : 'same-origin'
}
req = urllib.request.Request(url, headers=headers, method='GET')
reply_data = gzip.decompress(urllib.request.urlopen(req).read()).split(b'\n')[1]
reply_data = json.loads(reply_data)
return [ cleanhtml(res[0]).strip() for res in reply_data[0] ]
elif SEARCH_ENGINE == 'duckduckgo': # 'duckduckgo'
if search_string.startswith('!'):
bang_search = True
search_string = search_string.lstrip('!')
else:
bang_search = False
r = {
'q' : search_string,
'callback' : 'autocompleteCallback',
'kl' : 'wt-wt',
'_' : str(int((datetime.datetime.now().timestamp())*1000))
}
url = CONFIG['SUGGESTION_URL'][SEARCH_ENGINE] + urllib.parse.urlencode(r)
if bang_search:
url = url.replace('?q=', '?q=!')
headers = {
'pragma' : 'no-cache',
'dnt' : '1',
'accept-encoding' : 'gzip',
'accept-language' : 'en-US;q=0.9,en;q=0.8',
'user-agent' : CONFIG['USER_AGENT'][BROWSER],
'sec-fetch-mode' : 'no-cors',
'accept' : '*/*',
'cache-control' : 'no-cache',
'authority' : 'duckduckgo.com',
'referer' : 'https://duckduckgo.com/',
'sec-fetch-site' : 'same-origin',
}
req = urllib.request.Request(url, headers=headers, method='GET')
reply_data = gzip.decompress(urllib.request.urlopen(req).read()).decode('utf8')
reply_data = json.loads(re.match(r'autocompleteCallback\((.*)\);', reply_data).group(1))
return [ cleanhtml(res['phrase']).strip() for res in reply_data ]
def main():
search_string = html.unescape((' '.join(sys.argv[1:])).strip())
if (search_string.endswith('!')) and (SEARCH_ENGINE not in SUGGESTIONS_INCOMPATIBLE):
search_string = search_string.rstrip('!').strip()
results = fetch_suggestions(search_string)
for r in results:
print(html.unescape(r))
elif search_string == '':
print('!!-- Type something and search it with %s' % CONFIG['SEARCH_ENGINE_NAME'][SEARCH_ENGINE])
print('!!-- Close your search string with "!" to get search suggestions')
elif (search_string.endswith('!')) and (SEARCH_ENGINE in SUGGESTIONS_INCOMPATIBLE):
print('Suggestions from', CONFIG['SEARCH_ENGINE_NAME'][SEARCH_ENGINE], 'are not supported.')
else:
url = CONFIG['SEARCH_URL'][SEARCH_ENGINE] + urllib.parse.quote_plus(search_string)
sp.Popen(CONFIG['BROWSER_PATH'][BROWSER] + [url], stdout=sp.DEVNULL, stderr=sp.DEVNULL, shell=False)
def validate_config(c):
if type(c) != dict:
print('Configuration file must be a JSON object', file=sys.stderr)
sys.exit(1)
for k in ('SEARCH_ENGINE', 'BROWSER', 'TERMINAL'):
if k not in c:
print('Configuration file is missing %s' % k, file=sys.stderr)
sys.exit(1)
for k in ('SEARCH_ENGINE', 'BROWSER'):
if type(c[k]) != str:
print('Configuration Error: The value of %s must be a string' % k, file=sys.stderr)
if type(c['TERMINAL']) != list:
print('Configuration Error: The value of TERMINAL must be a list of strings', file=sys.stderr)
sys.exit(1)
for x in c['TERMINAL']:
if type(x) != str:
print('Configuration Error: The value of TERMINAL must be a list of strings', file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
try:
fname = os.path.expanduser('~/.config/rofi-web-search/config.json')
if os.path.exists(fname):
try:
config = json.loads(open(fname, 'r').read())
except json.JSONDecodeError:
print('Configuration file %s is not a valid JSON' % fname, file=sys.stderr)
sys.exit(1)
validate_config(config)
SEARCH_ENGINE = config['SEARCH_ENGINE']
BROWSER = config['BROWSER']
TERMINAL = config['TERMINAL']
else:
# Create default config
config = {
'SEARCH_ENGINE' : SEARCH_ENGINE,
'BROWSER' : BROWSER,
'TERMINAL' : TERMINAL
}
os.makedirs(os.path.dirname(fname))
f = open(fname, 'w')
f.write(json.dumps(config, indent=4))
f.write('\n')
f.close()
main()
except:
sys.exit(1)