/
xwordlist.py
executable file
·434 lines (364 loc) · 17.5 KB
/
xwordlist.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/env python3
import configargparse
import os
import sys
import requests
import pathlib
import urllib.parse
import time
import re
from prompt_toolkit import prompt, print_formatted_text, HTML
from bs4 import BeautifulSoup
from anyascii import anyascii
# Set up globals
__version__ = '22.01.05'
exec_name = os.path.basename(__file__)
exec_pieces = os.path.splitext(exec_name)
config_name = os.path.join(os.path.dirname(os.path.abspath(__file__)), '{}.conf'.format(exec_pieces[0]))
GLOBAL_SETTINGS = {
'urllist_delay': 20,
'file_add': 'xwl',
'impact_color': 'ansired',
}
IMPACT_COLOR = GLOBAL_SETTINGS['impact_color']
COLOR_OPTIONS = ['ansiblack', 'ansired', 'ansigreen', 'ansiyellow', 'ansiblue', 'ansimagenta',
'ansicyan', 'ansigray', 'ansibrightblack', 'ansibrightred', 'ansibrightgreen',
'ansibrightyellow', 'ansibrightblue', 'ansibrightmagenta', 'ansibrightcyan', 'ansiwhite']
class WordList:
def __init__(self, myList=[]):
self.myList = myList
# List transformation options
def remove_min(self, numChars):
newList = []
for line in self.myList:
if line and len(line) >= numChars:
newList.append(line)
self.myList = newList
def strip_nonalpha(self, stripWhat):
newList = []
if stripWhat in ['keepdiacritic', 'diacritic', 'true']:
for word in self.myList:
if stripWhat != 'keepdiacritic':
word = anyascii(word)
newWord = ''.join([i for i in word if i.isalpha()])
if len(newWord) > 0:
newList.append(newWord)
self.myList = newList
else:
print_text = 'Exiting... unknown strip option <{color}>{stripWhat}</{color}>'
print_line(print_text, {'stripWhat': stripWhat})
sys.exit()
def case_change(self, newCase):
case_dict = {
'upper': str.upper,
'lower': str.lower,
}
if newCase in case_dict:
self.myList = list(case_dict[newCase](line) for line in self.myList)
elif newCase != 'none':
print_text = 'Exiting... unknown case option <{color}>{newcase}</{color}>'
print_line(print_text, {'newcase': newCase})
sys.exit()
def uniquify(self, dedupeType):
if dedupeType == 'bycase':
self.myList = list(dict.fromkeys(self.myList))
elif dedupeType in ['nocase', 'true']:
newList = []
newSet = set()
for line in self.myList:
newLine = line.casefold()
if newLine not in newSet:
newSet.add(newLine)
newList.append(line)
self.myList = newList
else:
print_text = 'Exiting... incorrect dedupe option <{color}>{dedupeType}</{color}>'
print_line(print_text, {'dedupeType': dedupeType})
sys.exit()
def alphabetize(self):
self.myList = sorted(self.myList, key=str.casefold)
# Content parsing options
def regex(self, regexInput):
try:
newList = []
for line in self.myList:
newList.extend(re.findall(regexInput, line))
self.myList = newList
except re.error:
error_text = 'Regex pattern <{color}>{regexInput}</{color}> not valid, please check and try again'
print_line(error_text, {'regexInput': regexInput})
sys.exit()
except Exception as e:
print_line('Error {e}', {'e': e})
sys.exit()
def convert(self, parseChars):
# First trap for problem where defaults are assumed but not specified
if parseChars == 'true':
parseChars = ' '
for char in parseChars:
newList = []
for line in self.myList:
if line.find(char) != -1:
newList.extend(line.split(char))
else:
newList.append(line)
self.myList = newList
def print_line(printText, argument={}, endText='\n'):
print_formatted_text(HTML(printText.format(**argument, color=IMPACT_COLOR)), end=endText)
return
def create_dict(localAttrs):
returnDict = {}
for attr in localAttrs:
dict_parts = attr.split('=')
if dict_parts[0][:2] == '--':
dict_parts[0] = dict_parts[0][2:]
# See if it is a class dictionary, in which case we make a tuple
if dict_parts[0] == 'class':
numParts = int(dict_parts[2]) if len(dict_parts) == 3 else 0
returnDict[dict_parts[0]] = (dict_parts[1], numParts)
elif len(dict_parts) > 1:
returnDict[dict_parts[0]] = dict_parts[1]
else:
print_text = 'Exiting... incorrect option <{color}>{dictPart}</{color}>'
print_line(print_text, {'dictPart': dict_parts[0]})
sys.exit()
if len(returnDict) > 0:
return returnDict
else:
print_line('Exiting... something is wrong with your configuration file')
sys.exit()
def extract_from_web(extractWhat, soup, extractURL):
# A few ways the default option can come in, try that first
if extractWhat == 'text' or extractWhat == 'true' or extractWhat is None:
localWords = soup.stripped_strings
elif extractWhat == 'links':
localWords = []
for link in soup.find_all('a'):
getURL = link.get('href')
if getURL:
parsePieces = urllib.parse.urlsplit(getURL)
# Check to see if absolute or relative URL. If relative, make it absolute
if parsePieces.scheme == '' and parsePieces.netloc == '':
parseExtract = urllib.parse.urlsplit(extractURL)
getURL = urllib.parse.urljoin('{}://{}'.format(parseExtract.scheme, parseExtract.netloc), getURL)
localWords.append(getURL)
elif extractWhat[:5] == 'html-':
localWords = []
extractTags = extractWhat[5:].split('_')
for tag in extractTags:
for link in soup.find_all(tag):
text = link.get_text()
localWords.append(text)
else:
print_text = 'Exiting... incorrect option for webextract: <{color}>{extractWhat}</{color}>'
print_line(print_text, {'extractWhat': extractWhat})
sys.exit()
return list(line for line in localWords)
def get_web_page(webURL, containerParse, webExtract):
try:
r = requests.get(webURL)
if r.status_code == 200:
inputSoup = BeautifulSoup(r.text, 'html.parser')
if containerParse:
parseDict = create_dict(containerParse)
# See if we have a class, in which case, have to do more screening (1 to N classes)
if 'class' in parseDict:
classDict = {}
classDict['class'], whichNum = parseDict['class']
returnWords = []
fullSoup = inputSoup.find_all(attrs=classDict)
for counter, whichSoup in enumerate(fullSoup, start=1):
if whichNum == counter or whichNum == 0:
returnWords.extend(extract_from_web(webExtract, whichSoup, webURL))
return returnWords
else:
return extract_from_web(webExtract, inputSoup.find(attrs=parseDict), webURL)
else:
return extract_from_web(webExtract, inputSoup, webURL)
elif r.status_code == 403:
print_line('Unable to load webpage due to code 403: this usually means we are being blocked')
else:
print_text = 'Unable to load webpage, status code = {statusCode}'
print_line(print_text, {'statusCode': r.status_code})
except (requests.URLRequired, requests.RequestException):
return False
except AttributeError:
error_text = 'HTML attribute <{color}>{containerParse}</{color}> not found, check document and try again'
print_line(error_text, {'containerParse': containerParse})
sys.exit()
except Exception as e:
print_line('Web error {e}', {'e': e})
sys.exit()
def get_file_content(inputFile):
try:
with open(inputFile, 'r') as inputOpen:
return list(line.strip() for line in inputOpen)
except UnicodeDecodeError:
print_line('Exiting... only text files accepted')
sys.exit()
except FileNotFoundError:
error_text = 'Exiting... input file <{color}>{inputFile}</{color}> does not exist'
print_line(error_text, {'inputFile': inputFile})
sys.exit()
except Exception as e:
print_line('Exiting... file error {e}', {'e': e})
sys.exit()
def save_output(localOuput, localWords):
try:
with open(localOuput, 'w') as outputOpen:
outputOpen.writelines(str(line) + '\n' for line in localWords.myList)
print_text = 'New list saved to <{color}>{output}</{color}>'
print_line(print_text, {'output': outputOpen.name})
except Exception as e:
print_line('Exiting... file save error {e}', {'e': e})
sys.exit()
def setup_output(localArgs, otherArgs):
file_add = otherArgs['file_add'] if 'file_add' in otherArgs else GLOBAL_SETTINGS['file_add']
# Has an output file been specified? If not, create from either file or URL
if localArgs.output:
outputFile = localArgs.output
elif localArgs.input or localArgs.urllist:
fileName = localArgs.input if localArgs.input else localArgs.urllist
filePieces = os.path.splitext(fileName)
outputFile = '{}_{}{}'.format(filePieces[0], file_add, filePieces[1])
elif localArgs.webpage:
urlPieces = urllib.parse.urlparse(localArgs.webpage).hostname.split('.')
if len(urlPieces) > 1:
outputFile = '{}_{}_{}.txt'.format(urlPieces[-2], urlPieces[-1], file_add)
else:
outputFile = '{}_{}.txt'.format(urlPieces[0], file_add)
# See if a directory has been specified: don't need to check if valid by now
if localArgs.directory is not None:
outputFile = os.path.join(localArgs.directory, outputFile)
else:
# Nothing to set up
return
# Now check if the output file exists and prompt user if it does
if pathlib.Path(outputFile).is_file():
prompt_text = 'Output file named <{color}>{output}</{color}> already exists. Overwrite? (Y/N): '
text = prompt(HTML(prompt_text.format(color=IMPACT_COLOR, output=outputFile)))
if text != 'Y' and text != 'y':
print_line('Exiting... please enter a different file name at the command line')
sys.exit()
return outputFile
def setup_input(localArgs, otherArgs):
returnWords = []
# Load input(s)
if localArgs.input:
fileWords = get_file_content(localArgs.input)
if fileWords:
returnWords.extend(fileWords)
if localArgs.webpage:
webWords = get_web_page(localArgs.webpage, localArgs.container, localArgs.webextract)
if webWords:
returnWords.extend(webWords)
if localArgs.urllist:
urlList = get_file_content(localArgs.urllist)
if urlList:
urlLength = len(urlList)
for urlCount, oneUrl in enumerate(urlList, start=1):
print_text = 'Getting <{color}>{oneUrl}</{color}> ({urlCount} of {urlLength})'
arg_dict = {
'oneUrl': oneUrl,
'urlCount': urlCount,
'urlLength': urlLength,
}
print_line(print_text, arg_dict, endText='')
webWords = get_web_page(oneUrl, localArgs.container, localArgs.webextract)
if webWords:
returnWords.extend(webWords)
if urlCount < urlLength:
delay = int(otherArgs['urllist_delay']) if 'urllist_delay' in otherArgs else GLOBAL_SETTINGS['urllist_delay']
print_line(' ...done ...sleeping {delay} seconds', {'delay': delay})
time.sleep(delay)
else:
print_line(' ...done')
else:
print_line(' ...no content retrieved, was that a URL?')
if len(returnWords) == 0:
print_text = 'No input given, nothing to do (enter <{color}>{exec_name} -h</{color}> for help)'
print_line(print_text, {'exec_name': exec_name})
sys.exit()
return returnWords
def main():
# First set up configargparse
parser = configargparse.ArgumentParser(default_config_files=[config_name],
description='Crossword puzzle word list builder')
# Input and output options
parser.add_argument('-i', '--input', type=pathlib.Path, help='Input text file')
parser.add_argument('-w', '--webpage', help='Input web URL')
parser.add_argument('--urllist', type=pathlib.Path, help='Input multiple URLs in a document')
output_help = 'Output text file: if no name specified, a default name is created'
parser.add_argument('-o', '--output', type=pathlib.Path, help=output_help)
directory_help = 'Set directory for input, output and urllist files'
parser.add_argument('--directory', type=pathlib.Path, help=directory_help)
# Content parsing options
container_help = 'Further refines the text from a webpage by narrowing to any HTML entity(ies) specified,\
using tag=term syntax (e.g., id=main_content or class=lyrics).'
parser.add_argument('--container', nargs=1, help=container_help)
webextract_help = 'Specify whether to extract text, links or specific tags from web inputs'
parser.add_argument('--webextract', nargs='?', default='text', help=webextract_help)
convert_help = 'Convert a block of text to a word list. Default delimiter is a space but acccepts\
any number of characters in quotes (e.g., --convert " ;," will separate words delimited\
by a space, comma or semicolon). Be careful with back slashes acting as an escape character'
parser.add_argument('--convert', nargs='?', const=' ', help=convert_help)
parser.add_argument('--regex', nargs=1, help='Parse text based on regex')
# List transformation options
parser.add_argument('-a', '--alphabetize', action='store_true', help='Alphabetize the list')
case_help = ' {none (default) | lower | upper} Change the case of words in the list'
parser.add_argument('--case', nargs='?', const='none', help=case_help)
dedupe_help = '{nocase (default) | bycase} Remove duplicates from the word list. By default, ignores\
case: "apple" and "APPLE" are the same word and the first instance found is kept.\
Use --dedupe bycase to treat each as a different word'
parser.add_argument('-d', '--dedupe', nargs='?', const='nocase', help=dedupe_help)
min_ltrs = 3
minimum_help = 'Set minimum number of letters in a word (if not specified, default is {})'.format(min_ltrs)
parser.add_argument('-m', '--minimum', nargs='?', type=int, const=min_ltrs, help=minimum_help)
strip_help = '{diacritic (default) | keepdiacritic} Remove non-alphabetic characters (including spaces).\
By default, converts diacritical marks into English letters (e.g., Renée becomes Renee).\
Use --strip keepdiacritic to leave diacriticals in.'
parser.add_argument('-s', '--strip', nargs='?', const='diacritic', help=strip_help)
args = parser.parse_known_args()
confArgs = args[0]
envArgs = create_dict(args[1])
# See if conf file contains an impact color
if 'impact_color' in envArgs and 'ansi{}'.format(envArgs['impact_color']) in COLOR_OPTIONS:
global IMPACT_COLOR
IMPACT_COLOR = 'ansi{}'.format(envArgs['impact_color'])
# See if a default directory was specified and rewrite inputs and outputs as necessary
if confArgs.directory is not None:
if pathlib.Path(confArgs.directory).is_dir():
confArgs.input = os.path.join(confArgs.directory, confArgs.input) if confArgs.input else None
confArgs.urllist = os.path.join(confArgs.directory, confArgs.urllist) if confArgs.urllist else None
confArgs.output = os.path.join(confArgs.directory, confArgs.output) if confArgs.output else None
else:
print_text = 'Exiting... directory path <{color}>{directory}</{color}> does not exist'
print_line(print_text, {'directory': confArgs.directory})
sys.exit()
outputFile = setup_output(confArgs, envArgs)
inputWords = WordList(setup_input(confArgs, envArgs))
# Do any text parsing
if confArgs.regex is not None:
if confArgs.regex[0] == 'true':
regex_error = 'Exiting... no regex pattern given, please use format '
regex_error += '<{color}>regex PATTERN</{color}> in configuration file'
print_line(regex_error)
sys.exit()
inputWords.regex(confArgs.regex[0])
if confArgs.convert is not None:
inputWords.convert(confArgs.convert)
if confArgs.strip is not None:
inputWords.strip_nonalpha(confArgs.strip)
# Do any text transforms
if confArgs.minimum is not None:
inputWords.remove_min(confArgs.minimum)
if confArgs.case is not None:
inputWords.case_change(confArgs.case)
if confArgs.dedupe is not None:
inputWords.uniquify(confArgs.dedupe)
if confArgs.alphabetize:
inputWords.alphabetize()
# Now save the file
save_output(outputFile, inputWords)
if __name__ == '__main__':
main()