-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto.py
73 lines (51 loc) · 1.67 KB
/
auto.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
import sys
import requests
import string
import xmltodict
sentence = sys.argv[1].strip()
replacements = dict()
if len(sys.argv) > 2:
for replacement in sys.argv[2].split(";"):
key, value = replacement.split(":")
replacements[key] = value
all_suggestions = set()
def replace(s, replacements):
for (key, value) in replacements.iteritems():
if key in s:
s = s.replace(key, value)
return s
url = "http://google.com/complete/search"
for letter in string.ascii_lowercase:
r = requests.get(url, params={
'output': 'toolbar',
'q': sentence + " " + letter
})
xml = xmltodict.parse(r.text)
if "toplevel" not in xml:
continue
if not xml["toplevel"]:
continue
if "CompleteSuggestion" not in xml["toplevel"]:
continue
if not xml["toplevel"]["CompleteSuggestion"]:
continue
if type(xml["toplevel"]["CompleteSuggestion"]) is not list:
xml["toplevel"]["CompleteSuggestion"] = [xml["toplevel"]["CompleteSuggestion"]]
for suggestion_xml in xml["toplevel"]["CompleteSuggestion"]:
if "suggestion" not in suggestion_xml:
continue
if not suggestion_xml["suggestion"]:
continue
if "@data" not in suggestion_xml["suggestion"]:
continue
if not suggestion_xml["suggestion"]["@data"]:
continue
suggestion = replace(
suggestion_xml["suggestion"]["@data"],
replacements
)
if not suggestion.lower().startswith(sentence.lower()):
continue
all_suggestions.add(suggestion)
for suggestion in sorted(all_suggestions):
print suggestion