-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsearch_engines.py
142 lines (125 loc) · 5.25 KB
/
search_engines.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
'''
Nebula Keyword Researcher: is designed to scrape keywords from
major search engines like Google, Bing and Yahoo.
Copyright (C) 2021. Eneiro A. Matos B.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import requests
import config
from file_writter import FileWriter as fw
class SearchEngineSuggestions:
headers = {
'user-agent': config.user_agent
}
@staticmethod
def get_yahoo(kw: str) -> tuple[str] | tuple[bool, str]:
kw = kw.casefold().replace(' ', '+')
n_results = 20
try:
html = requests.get(f'https://search.yahoo.com/sugg/gossip/gossip-us-ura/?'
f'command={kw}&output=json&nresults={n_results}',
headers=SearchEngineSuggestions.headers, timeout=config.timeout)
except Exception as e:
return False, str(e)
else:
results = html.json()['gossip']['results']
return tuple([result['key'] for result in results])
@staticmethod
def get_ask(kw: str) -> tuple[str] | tuple[bool, str]:
kw = kw.casefold().replace(' ', '+')
n_results = 20
try:
html = requests.get(f'https://amg-ss.ask.com/query?limit={n_results}&q={kw}',
headers=SearchEngineSuggestions.headers, timeout=config.timeout)
except Exception as e:
return False, str(e)
else:
return tuple(html.json()[1])
@staticmethod
def get_yandex_us(kw: str) -> tuple[str] | tuple[bool, str]:
kw = kw.casefold().replace(' ', '+')
try:
html = requests.get(f'https://yandex.com/suggest/suggest-ya.cgi?v=4&bemjson=0&part={kw}',
headers=SearchEngineSuggestions.headers, timeout=config.timeout)
except Exception as e:
return False, str(e)
else:
results = html.json()
return tuple(results[1])
@staticmethod
def get_duckduckgo(kw: str) -> tuple[str] | tuple[bool, str]:
kw = kw.casefold().replace(' ', '+')
try:
html = requests.get(f'https://duckduckgo.com/ac/?q={kw}',
headers=SearchEngineSuggestions.headers, timeout=config.timeout)
except Exception as e:
return False, str(e)
else:
results = html.json()
return tuple([sugg['phrase'] for sugg in results])
@staticmethod
def get_ecosia(kw: str) -> tuple[str] | tuple[bool, str]:
kw = kw.casefold().replace(' ', '+')
try:
html = requests.get(f'https://ac.ecosia.org/?q={kw}',
headers=SearchEngineSuggestions.headers, timeout=config.timeout)
except Exception as e:
return False, str(e)
else:
results = html.json()
return tuple(results['suggestions'])
@staticmethod
def get_brave(kw: str) -> tuple[str] | tuple[bool, str]:
kw = kw.casefold().replace(' ', '+')
try:
html = requests.get(f'https://search.brave.com/api/suggest?q={kw}',
headers=SearchEngineSuggestions.headers, timeout=config.timeout)
except Exception as e:
return False, str(e)
else:
results = html.json()
return tuple(results[1])
@staticmethod
def get_google(kw: str) -> tuple[str] | tuple[bool, str]:
kw = kw.casefold().replace(' ', '+')
try:
html = requests.get(f'https://www.google.com/complete/search?q={kw}'
f'&client=firefox', headers=SearchEngineSuggestions.headers,
timeout=config.timeout)
except Exception as e:
return False, str(e)
else:
results = html.json()
return tuple(results[1])
@staticmethod
def get_bing(kw: str) -> tuple[str] | tuple[bool, str]:
kw = kw.casefold().replace(' ', '+')
try:
# Got the API from https://stackoverflow.com/a/15318150
html = requests.get(f'https://api.bing.com/osjson.aspx?query={kw}',
headers=SearchEngineSuggestions.headers, timeout=config.timeout)
except Exception as e:
return False, str(e)
else:
results = html.json()
return tuple(results[1])
@staticmethod
def kw_results(search_engine, kw):
kws_search_engine = search_engine(kw)
if len(kws_search_engine) > 0:
if kws_search_engine[0] is False:
fw.write_log(kws_search_engine[1])
return tuple()
else:
return kws_search_engine
else:
return tuple()