-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetchAutocomplete.py
138 lines (108 loc) · 4.44 KB
/
fetchAutocomplete.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
from suggestion import Suggestion, db
from concepts import concepts
import time
class AutocompleteFetcher:
def __init__(self, driver, country_IP):
self.driver = driver
self.db = db
self.country_IP = country_IP
self.onlyEnglish = False
def getSearchBox(self):
pass
def getSuggestions(self):
pass
def doBeforeQuery(self):
pass
def doAfterPageLoad(self):
pass
def fetch(self):
self.driver.get(self.url)
self.doAfterPageLoad()
for concept in concepts:
for query in concept["queries"]:
phrase = query["query"].format(concept = concept["name"])
language = query["lang"]
if not (self.onlyEnglish and language != "en"):
self.doBeforeQuery()
q = self.getSearchBox()
q.clear()
time.sleep(self.waitBeforeQuery)
# Type in the query
for letter in phrase:
q.send_keys(letter)
time.sleep(.05)
# Extract the suggestions
time.sleep(self.waitAfterQuery)
suggestions = self.getSuggestions()
# Stores the suggestions in the DB
for suggestion in suggestions.text.split("\n") :
Suggestion(concept=concept["name"]
, phrase=phrase
, suggestion=suggestion
, language=language
, country_IP=self.country_IP
, service=self.service).save()
class getGoogleTranslate(AutocompleteFetcher):
def __init__(self, driver, country_IP):
AutocompleteFetcher.__init__(self, driver, country_IP)
self.url = "https://translate.google.com/#view=home&op=translate&sl=en&tl=en"
self.service = "Google Translate"
self.waitBeforeQuery = 1
self.waitAfterQuery = 3
self.onlyEnglish = True
def getSearchBox(self):
return self.driver.find_element_by_id("source")
def getSuggestions(self):
return self.driver.find_element_by_id("gt-src-is")
class getGoogleSearch(AutocompleteFetcher):
def __init__(self, driver, country_IP):
AutocompleteFetcher.__init__(self, driver, country_IP)
self.url = "https://google.com"
self.service = "Google Search"
self.waitBeforeQuery = .5
self.waitAfterQuery = 1.5
def getSearchBox(self):
return self.driver.find_element_by_name("q")
def getSuggestions(self):
return self.driver.find_element_by_xpath("//ul[@role = 'listbox']")
class getYandex(AutocompleteFetcher):
def __init__(self, driver, country_IP):
AutocompleteFetcher.__init__(self, driver, country_IP)
self.url = "https://yandex.ru/"
self.service = "Yandex"
self.waitBeforeQuery = 1
self.waitAfterQuery = 2
def getSearchBox(self):
return self.driver.find_element_by_class_name("input__control")
def getSuggestions(self):
return self.driver.find_element_by_class_name("mini-suggest__popup")
class getBing(AutocompleteFetcher):
def __init__(self, driver, country_IP):
AutocompleteFetcher.__init__(self, driver, country_IP)
self.url = "https://www.bing.com"
self.service = "Bing Search"
self.waitBeforeQuery = 2
self.waitAfterQuery = 3
def getSearchBox(self):
return self.driver.find_element_by_id("sb_form_q")
def getSuggestions(self):
return self.driver.find_element_by_id("sw_as")
def doBeforeQuery(self):
self.driver.find_element_by_id("b_logo").click()
class getYahoo(AutocompleteFetcher):
def __init__(self, driver, country_IP):
AutocompleteFetcher.__init__(self, driver, country_IP)
self.url = "https://search.yahoo.com/"
self.service = "Yahoo Search"
self.waitBeforeQuery = 1
self.waitAfterQuery = 2
def getSearchBox(self):
return self.driver.find_element_by_id("yschsp")
def getSuggestions(self):
return self.driver.find_element_by_class_name("sa-sbx-container")
def doAfterPageLoad(self):
try:
q = self.driver.find_element_by_id("yschsp")
except Exception:
self.driver.find_element_by_name("agree").click()
self.driver.get("https://search.yahoo.com/")