-
Notifications
You must be signed in to change notification settings - Fork 3
/
wordsearch.py
234 lines (194 loc) · 7.47 KB
/
wordsearch.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
import requests
import bs4
import re
import os
import argparse
import configparser
import sys
def parse_site(url, selection):
"""Request site, select elements define by selection and return it/them
as a list"""
try:
res = requests.get('http://' + url)
res.raise_for_status()
except:
print('Can\'t connect to source')
return {"encoding": "NA",
"content": []}
site = bs4.BeautifulSoup(res.text, 'html.parser')
content = site.select(selection)
if type(content) == list:
elements = {"encoding": res.encoding,
"content": [item.text for item in content]}
else:
elements = {"encoding": res.encoding,
"content": [content.text]}
return elements
def clean_link(link):
"""Normalize source, this is needed since some source has http, or www.
and others doesn't"""
if link.startswith('http') or link.startswith('https'):
link = re.sub(r'http[s]?://', '', link)
if link.startswith('www.'):
link = re.sub(r'www\.', '', link)
return link
def get_domain(link):
"""Short hand for splitting a cleaned source and returning the domaen
ie. en.wikipedia.org"""
return link.split('/')[0]
def search_google(args):
"""Search google, parse the html and return all result links as a list"""
if args.synonym:
query = args.query + " synonyms"
else:
query = "define " + args.query
payload = {'q': query, 'num': '25'}
try:
res = requests.get('https://www.google.dk/search?', params=payload)
res.raise_for_status()
except:
sys.exit('Google not responding, check your internet')
soup = bs4.BeautifulSoup(res.text, 'html.parser')
results = soup.select('cite')
return [clean_link(hit.text) for hit in results]
def clear_screen():
"""Clears the commandline window"""
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
def print_pages(pages, source, n, n_sources):
"""Iterate over a list of pages(strings) pausing after
each paragraph has been displayed, returns true/false determining whether
the next source should be displayed if any"""
page_n = 0
while page_n < len(pages["content"]):
if len(pages["content"][page_n]) > 10: # Skip overly short pages
clear_screen()
try:
print('{}\n'.format(pages["content"][page_n]))
except UnicodeEncodeError:
print('{}\n'.format(
str(pages["content"][page_n].encode(pages["encoding"],
"replace"))))
print('Page: {}/{}'.format(page_n + 1, len(pages["content"])))
print('Source {}/{}: {}'.format(n, n_sources, source))
u_input = input('\ne: end script | n: next source | b: back\n:> ')
if u_input.lower() == 'e':
return False
if u_input.lower() == 'n':
return True
elif u_input.lower() == 'b':
if page_n > 0:
page_n -= 1
else:
page_n = 0
else:
if page_n < len(pages["content"])-1:
page_n += 1
else:
return True
else:
page_n += 1
return True
def create_list(word_list, divider="- ", space=4):
"""Convert at list of words to a neatly formatted string"""
max_width = 0
# Find the width of the largest element in the word_list + the divider
for entry in word_list["content"]:
entry = entry.strip("\n").strip("\t").strip(" ")
total_length = len(entry) + len(divider)
if total_length > max_width:
max_width = total_length
if max_width > 15:
columns = 3
else:
columns = 4
string_list = []
for entry in word_list["content"]:
list_element = ""
list_element += divider + entry
while len(list_element) < max_width:
list_element += " "
string_list.append(list_element)
return_string = ""
for i, entry in enumerate(string_list, start=1):
return_string += entry + " "*space
if i % columns == 0:
return_string += "\n"
return return_string
def print_list(elements, source, word, place, total):
"""Print a list of scraped definitions"""
clear_screen()
try:
print(create_list(elements))
except UnicodeEncodeError:
print(str(create_list(elements).encode(elements["encoding"],
'replace')))
print("\nSynonyms for " + word)
print("Source {}/{}: {}".format(place, total, source))
u_input = input("\ne: end script\n")
if u_input == "e":
return False
else:
return True
def a_parse():
"""Parse commandline arguments"""
parser = argparse.ArgumentParser()
parser.add_argument("query", help="Word you need defined")
parser.add_argument("-s", "--synonym", help="Look up synonyms",
action="store_true", default=False)
return parser.parse_args()
def parse_hits(hits, config, synonym=False):
"""Parse a list of links returning all those with a known selector as a
dictionary with url, css selector, domain"""
valid_hits = []
for hit in hits:
domain = get_domain(hit)
if domain in config:
if config[domain]["syn"] == "false" and not synonym:
context = {"url": hit,
"selector": config[domain]["selector"],
"domain": domain}
valid_hits.append(context)
elif config[domain]["syn"] == "true" and synonym:
context = {"url": hit,
"selector": config[domain]["selector"],
"domain": domain}
valid_hits.append(context)
return valid_hits
def main():
# Read config and commandline args
config = configparser.ConfigParser()
config.read('config.ini')
args = a_parse()
print('Googling that for you...')
# Search google and extract result links
# Sort out all domains not in config.ini
links = parse_hits(search_google(args), config, synonym=args.synonym)
if len(links) < 1:
sys.exit('No definition found')
cont = True
for n, source in enumerate(links):
if config[source["domain"]]["lang"] == config["DEFAULT"]["lang"] \
or config["DEFAULT"]["lang"] == "all":
if cont:
print('Looking up source...')
# Request site and extract word definiton from it using
# the selector in config.ini, one definition element is a pages
pages = parse_site(source["url"], source["selector"])
if len(pages) > 0 and args.synonym:
cont = print_list(pages, source["domain"], args.query,
n + 1, len(links))
elif len(pages) > 0 and not args.synonym:
# If any definition was found enter the print pages loop
cont = print_pages(pages, source["domain"],
n + 1, len(links))
else: # Source depleted, fetch next source if any
continue
else: # User stopped the script
break
else: # Wrong language, fetch next source if any
continue
if __name__ == '__main__':
main()