-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.py
73 lines (55 loc) · 1.96 KB
/
query.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
"""
front: https://www.google.com/search?
All: as_q=All+these+words
Exact: as_epq=%22Exact+Phrase
Any: as_oq=Any+here
None: as_eq=None+This
Site: as_sitesearch=smutek.net
Empty queries get passed in without value
sep query strings with &
end of query
these params are appended by default, not sure what as_occt is
&as_occt=any&safe=images&as_filetype=&as_rights=
"""
class Query:
def __init__(self):
self.front = "https://www.google.com/search?"
self.params = {
"as_q=": "", # term+term
"&as_epq=": "", # exact %22term+term%22
"&as_oq=": "", # term+term
"&as_eq=": "", # none term+term
"&as_sitesearch=": ""
}
self.prompts = {
"as_q=": "All of these terms: ", # term+term
"&as_epq=": "This exact term or phrase: ", # exact %22term+term%22
"&as_oq=": "Any of these terms: ", # term+term
"&as_eq=": "None of these terms: ", # none term+term
"&as_sitesearch=": "Search this site:"
}
self.tail = "&as_occt=any&safe=images&as_filetype=&as_rights="
# prompt the user for query terms
def terms(self):
print("Enter terms. Separate multiple values with space.")
print("Press enter to skip.")
for key, value in self.prompts.items():
# prompt user
terms = str(input(value))
# if user has entered terms
if terms:
# replace spaces with plus
terms = terms.replace(" ", "+")
# if exact match param, wrap in %22
if key == "&as_epq=":
terms = "%22" + terms + "%22"
self.params[key] = terms
return self.params
# construct query string
def url(self, dictionary):
url = self.front
for key, value in dictionary.items():
param = key + value
url += param
url += self.tail
return url