/
__init__.py
379 lines (332 loc) · 16.6 KB
/
__init__.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#
# py-google-search Copyright (C) 2018 Javinator9889
# This program comes with ABSOLUTELY NO WARRANTY; for details add the "-h" option.
# This is free software, and you are welcome to redistribute it
# under certain conditions; type "-L" for details.
#
import urllib.parse
from pyGle.values import *
from pyGle.values.InterfaceLanguages import InterfaceLanguages
from .url_constants import __google_base_url__, __google_url_modifiers__
class GoogleSearch:
def __init__(self, query: str = None):
self.query = query
self.words_in_order = None
self.or_words = None
self.words_excluded = None
self.number_of_results = None
self.filetype = None
self.sitesearch = None
self.time_limit = None
self.rights = None
self.words_in_title = None
self.words_in_body = None
self.words_in_url = None
self.words_in_anchor = None
self.between_two_numbers = None
self.synonym = None
self.containing_two_words = None
self.safe = None
self.related = None
self.linked = None
self.personalized_search = None
self.results_lang = None
self.country = None
self.document_country = None
self.search_between_two_dates = None
self.sort_by_update_time = None
self.search_at_different_pages = False
self.search_at = None
self.start_position = None
self.image_params = None
self.patents_params = None
self.shop_params = None
self.interface_language = None
self.book_params = None
self.video_params = None
def withQuery(self, query: str):
self.query = query.replace(' ', '+')
return self
def withWordsInSpecificOrderSearch(self, words: list):
self.words_in_order = '+'.join(words)
return self
def withOneOrMoreWordsInResult(self, words: list):
self.or_words = '+'.join(words)
return self
def withExcludedWords(self, words: list):
self.words_excluded = '+'.join(words)
return self
def withNumberOfResults(self, results: int = 10):
self.number_of_results = str(results)
return self
def withFileSearchByFileType(self, filetype: str):
self.filetype = filetype.replace(' ', '+')
return self
def withSiteSearch(self, site: str):
self.sitesearch = site.replace(' ', '+')
return self
def withTimeLimit(self, time_limit: TimeLimit):
self.time_limit = time_limit.getTimeLimit()
return self
def withRights(self, rights: Rights):
self.rights = rights.getRights()
return self
def withTextInTitle(self, text: str):
self.words_in_title = text.replace(' ', '+')
return self
def withTextInBody(self, text: str):
self.words_in_body = text.replace(' ', '+')
return self
def withTextInUrl(self, text: str):
self.words_in_url = text.replace(' ', '+')
return self
def withTextInAnchor(self, text: str):
self.words_in_anchor = text.replace(' ', '+')
return self
def withResultBetweenTwoNumbers(self, first_number: float, second_number: float):
self.between_two_numbers = [str(first_number), str(second_number)]
return self
def withRelatedPagesToADocument(self, document_url: str):
self.related = document_url.replace(' ', '+')
return self
def withSynonymousSearchingToAWord(self, word: str):
self.synonym = word.replace(' ', '+')
return self
def withContainingTwoTerms(self, first_term: str, second_term: str):
self.containing_two_words = [first_term.replace(' ', '+'), second_term.replace(' ', '+')]
return self
def withSafeModeActivated(self):
self.safe = __google_url_modifiers__["with_safe_active"]
return self
def withSafeModeDeactivated(self):
self.safe = __google_url_modifiers__["with_safe_deactivated"]
return self
def withLinkingToUrl(self, url: str):
self.linked = url.replace(' ', '+')
return self
def withPersonalizedSearch(self):
self.personalized_search = __google_url_modifiers__["with_personalized_search"]
return self
def withNoPersonalizedSearch(self):
self.personalized_search = __google_url_modifiers__["with_no_personalized_search"]
return self
def withResultsLanguage(self, lang: Languages):
self.results_lang = lang.getLanguage()
return self
def withResultsAtCountry(self, country: Countries):
self.country = country.getCountry()
return self
def withDocumentCountry(self, country: Countries):
self.document_country = country.getCountry()
return self
def withSearchBetweenTwoDates(self, dates: Dates):
self.search_between_two_dates = dates.getDates()
return self
def withSortByUpdateTime(self):
self.sort_by_update_time = True
return self
def withSearchingAtDifferentGooglePages(self, google_pages: GooglePages):
self.search_at_different_pages = True
self.search_at = google_pages.getGooglePage()
return self
def withSearchStartPositionAt(self, index: int):
self.start_position = str(index)
return self
def withImageParams(self, params: GoogleImages):
params_dict = params.getImageParams()
self.image_params = {}
for key, value in params_dict.items():
if value:
self.image_params[key] = value
return self
def withPatentParams(self, params: GooglePatents):
params_dict = params.getPatentModifiers()
self.patents_params = {}
for key, value in params_dict.items():
if value:
self.patents_params[key] = value
return self
def withShopOptions(self, params: GoogleShop):
params_dict = params.getShopModifiers()
self.shop_params = {}
for key, value in params_dict.items():
if value:
self.shop_params[key] = value
return self
def withInterfaceLanguage(self, language: InterfaceLanguages):
self.interface_language = language
return self
def withBookParams(self, params: GoogleBooks):
params_dict = params.getBooksModifiers()
self.book_params = {}
for key, value in params_dict.items():
if value:
self.book_params[key] = value
return self
def withVideoParams(self, params: GoogleVideos):
params_dict = params.getVideoModifiers()
self.video_params = {}
for key, value in params_dict.items():
if value:
self.video_params[key] = value
return self
class URLBuilder:
def __init__(self, google_search_params: GoogleSearch):
self.params = google_search_params
def __params_validator(self):
from errors import TimeCombinationNonValid, MixedSearchException
if self.params.search_between_two_dates and self.params.time_limit:
raise TimeCombinationNonValid("You cannot search between two dates and a define a time limit")
if self.params.search_at_different_pages:
if self.params.search_at == __google_url_modifiers__["with_searching"]["books"]:
if self.params.image_params or self.params.patents_params or self.params.shop_params \
or self.params.video_params:
raise MixedSearchException("You cannot combine a Google Books Search with non-proper params")
elif self.params.search_at == __google_url_modifiers__["with_searching"]["images"]:
if self.params.shop_params or self.params.patents_params or self.params.book_params \
or self.params.video_params:
raise MixedSearchException("You cannot combine a Google Images Search with non-proper params")
elif self.params.search_at == __google_url_modifiers__["with_searching"]["news"]:
if self.params.shop_params or self.params.patents_params or self.params.book_params or \
self.params.image_params or self.params.video_params:
raise MixedSearchException("You cannot combine a Google News Search with non-proper params")
elif self.params.search_at == __google_url_modifiers__["with_searching"]["patents"]:
if self.params.shop_params or self.params.image_params or self.params.book_params \
or self.params.video_params:
raise MixedSearchException("You cannot combine a Google Patents Search with non-proper params")
elif self.params.search_at == __google_url_modifiers__["with_searching"]["shops"]:
if self.params.patents_params or self.params.book_params or self.params.image_params \
or self.params.video_params:
raise MixedSearchException("You cannot combine a Google Shops Search with non-proper params")
elif self.params.search_at == __google_url_modifiers__["with_searching"]["videos"]:
if self.params.shop_params or self.params.patents_params or self.params.book_params or \
self.params.image_params:
raise MixedSearchException("You cannot combine a Google Videos Search with non-proper params")
def build(self):
# type: () -> tuple
from errors import NullQueryError
self.__params_validator()
main_query = []
if self.params.query:
main_query.append(__google_url_modifiers__["query"].format(self.params.query))
else:
raise NullQueryError("At least \"query\" must be provided")
if self.params.words_in_title:
main_query.append(__google_url_modifiers__["with_text_in_title"].format(self.params.words_in_title))
if self.params.words_in_body:
main_query.append(__google_url_modifiers__["with_text_in_body"].format(self.params.words_in_body))
if self.params.words_in_url:
main_query.append(__google_url_modifiers__["with_text_in_url"].format(self.params.words_in_url))
if self.params.words_in_anchor:
main_query.append(
__google_url_modifiers__["with_text_in_anchor"].format(self.params.words_in_anchor))
if self.params.between_two_numbers:
main_query.append(
__google_url_modifiers__["between_two_numbers"].format(self.params.between_two_numbers[0],
self.params.between_two_numbers[1]))
if self.params.synonym:
main_query.append(__google_url_modifiers__["with_synonymous_searching"].format(self.params.synonym))
if self.params.containing_two_words:
main_query.append(__google_url_modifiers__["with_containing_two_terms"].format(
self.params.containing_two_words[0], self.params.containing_two_words[1]))
extra_attributes_query = []
tbs_attributes = []
if self.params.words_in_order:
extra_attributes_query.append(
__google_url_modifiers__["in_order_displayed"].format(self.params.words_in_order))
if self.params.or_words:
extra_attributes_query.append(__google_url_modifiers__["or"].format(self.params.or_words))
if self.params.words_excluded:
extra_attributes_query.append(
__google_url_modifiers__["exclude"].format(self.params.words_excluded))
if self.params.number_of_results:
extra_attributes_query.append(
__google_url_modifiers__["number_of_results"].format(self.params.number_of_results))
if self.params.filetype:
extra_attributes_query.append(
__google_url_modifiers__["with_extension"].format(self.params.filetype))
if self.params.sitesearch:
extra_attributes_query.append(__google_url_modifiers__["at_site"].format(self.params.sitesearch))
if self.params.time_limit:
extra_attributes_query.append(
__google_url_modifiers__["with_time_limit"].format(self.params.time_limit))
if self.params.rights:
extra_attributes_query.append(__google_url_modifiers__["with_rights"].format(self.params.rights))
if self.params.safe:
extra_attributes_query.append(self.params.safe)
if self.params.related:
extra_attributes_query.append(
__google_url_modifiers__["with_related_pages_to_a_doc"].format(self.params.related))
if self.params.linked:
extra_attributes_query.append(
__google_url_modifiers__["with_linking_to_url"].format(self.params.linked))
if self.params.personalized_search:
extra_attributes_query.append(self.params.personalized_search)
if self.params.results_lang:
extra_attributes_query.append(
__google_url_modifiers__["with_results_language"].format(self.params.results_lang))
if self.params.interface_language:
extra_attributes_query.append(
__google_url_modifiers__["with_language_interface"].format(self.params.interface_language))
if self.params.country:
extra_attributes_query.append(
__google_url_modifiers__["with_country_results"].format(self.params.country))
if self.params.document_country:
extra_attributes_query.append(
__google_url_modifiers__["with_document_county"].format(self.params.document_country))
if self.params.search_between_two_dates:
if self.params.sort_by_update_time:
tbs_attributes.append(
__google_url_modifiers__["with_search_between_two_dates_by_update_time"].format(
self.params.search_between_two_dates[0], self.params.search_between_two_dates[1]))
else:
tbs_attributes.append(__google_url_modifiers__["with_search_between_two_dates"].format(
self.params.search_between_two_dates[0], self.params.search_between_two_dates[1]))
if self.params.sort_by_update_time and not self.params.search_between_two_dates:
tbs_attributes.append(__google_url_modifiers__["with_search_by_update_time"])
if self.params.search_at_different_pages:
extra_attributes_query.append(
__google_url_modifiers__["with_searching_at_different_google_pages"].format(
self.params.search_at))
if self.params.start_position:
extra_attributes_query.append(
__google_url_modifiers__["with_starting_at_position"].format(self.params.start_position))
if self.params.image_params:
for key, value in self.params.image_params.items():
tbs_attributes.append(value)
if self.params.patents_params:
for key, value in self.params.patents_params.items():
tbs_attributes.append(value)
if self.params.book_params:
for key, value in self.params.book_params.items():
tbs_attributes.append(value)
if self.params.video_params:
for key, value in self.params.video_params.items():
tbs_attributes.append(value)
if self.params.shop_params:
try:
if self.params.shop_params["min_price"] is not None:
tbs_attributes.append(__google_url_modifiers__["with_shop_params"]["with_custom_price"])
except KeyError:
try:
if self.params.shop_params["max_price"] is not None:
tbs_attributes.append(__google_url_modifiers__["with_shop_params"]["with_custom_price"])
except KeyError:
pass
for key, value in self.params.shop_params.items():
tbs_attributes.append(value)
final_query = '+'.join(main_query)
final_attributes = '&'.join(extra_attributes_query)
if len(tbs_attributes) > 0:
extra_attributes = __google_url_modifiers__["with_extra_attributes"].format(','.join(tbs_attributes))
else:
extra_attributes = None
if final_attributes and extra_attributes:
attributes_query = final_query + '&' + final_attributes + '&' + extra_attributes
elif final_attributes:
attributes_query = final_query + '&' + final_attributes
elif extra_attributes:
attributes_query = final_query + '&' + extra_attributes
else:
attributes_query = final_query
return urllib.parse.quote_plus(__google_base_url__ + attributes_query, safe="/?+&:=_.(|)*-%,~")