-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
build_cats.py
executable file
·591 lines (447 loc) · 18.3 KB
/
build_cats.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#!/usr/bin/env python3
import argparse
from pprint import pprint
from datetime import datetime
import re
from dcache import DescriptionCache
import scrapscrap
import main_page
import textrender
import globs
# sys.setdefaultencoding() does not exist, here!
#reload(sys) # Reload does the trick!
#sys.setdefaultencoding('UTF8')
class DuckStuff:
#the host
url = 'https://duckduckgo.com'
def __init__(self, enable_http_client, enable_selenium, text_renderer):
self.soup_builder = scrapscrap.BSoup()
self.desc_cache = DescriptionCache(enable_http_client, enable_selenium)
self.desc_cache.read_description_cache()
self.clean_tag = re.compile('<.*?>')
self.enable_http_client = enable_http_client
self.map_url_to_id = {}
self.out_file = None
self.text_renderer = text_renderer
def build_cache(self):
json_data = self.soup_builder.get_json(DuckStuff.url + "/bang.js")
failed_lookups = []
cur_item = 0
total_items = len(json_data)
for entry in json_data:
url = entry.get("d")
before_lookup = datetime.now()
description, cache_hit = self.desc_cache.cache_lookup_load_if_missing(url) #, self.soup_builder )
after_lookup = datetime.now()
if description is None or description.description == "":
failed_lookups.append( url )
else:
print(f"description: {repr(description)}")
time_diff = after_lookup - before_lookup
self.desc_cache.show(prefix="", suffix=f"cache_hit: {cache_hit} current: {cur_item+1}/{total_items} lookup_time: {repr(time_diff)}")
cur_item += 1
self.desc_cache.show("Total:")
if len(failed_lookups) != 0:
with open(globs.Globals.failed_lookups, "w") as log_file:
print("failed lookups:\n", "\n".join(failed_lookups) )
log_file.write("\n".join(failed_lookups))
def get_all(self):
json_data = self.soup_builder.get_json(DuckStuff.url + "/bang.js")
all_bangs = {}
set_of_bangs = {}
num_entries = 0
for entry in json_data:
#print("Category {} SubCategory {}".format(entry.get("c"), entry.get("sc")))
sub_cat = entry.get("sc")
cat = entry.get("c")
if not cat is None and not sub_cat is None:
cat_obj = all_bangs.get(cat)
if cat_obj is None:
all_bangs[cat] = {}
cat_obj = all_bangs.get(cat)
sub_cat_obj = cat_obj.get(sub_cat)
if sub_cat_obj is None:
cat_obj[sub_cat] = []
sub_cat_obj = cat_obj.get(sub_cat)
url = entry.get("d")
entry = (entry.get("t"), entry.get("s"), url)
sub_cat_obj.append(entry)
num_entries = num_entries + 1
set_of_bangs[entry[0]] = 1
if scrapscrap.Global.trace_on:
pprint(all_bangs)
return all_bangs, num_entries, len(set_of_bangs.keys()), json_data
@staticmethod
def write_hdr_pc(out_file):
out_file.write("""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
<link rel="stylesheet" href="template/github-markdown.css">
</head>
<script>
function onBang(bangText) {
//document.location = "https://duckduckgo.com/?q=!" + bangText;
window.parent.postMessage( '{ "cmd": "set-bang", "bang": "' + bangText + '"}', '*')
}
</script>
<body class="markdown-body">
<a id="top"/>
""")
@staticmethod
def write_hdr_mobile(out_file):
out_file.write("""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
<link rel="stylesheet" href="template/github-markdown-mobile.css">
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0" />
<meta name="apple-mobile-web-app-capable" content="yes" />
</head>
<script>
function onBang(bangText) {
//document.location = "https://duckduckgo.com/?q=!" + bangText;
window.parent.postMessage( '{ "cmd": "set-bang", "bang": "' + bangText + '"}', '*')
}
</script>
<body class="markdown-body">
<a id="top"/>
""")
def build_toolip_help_list(self, json_data, out_file):
next_id = 0
self.map_url_to_id = {}
out_file.write("""
<script>
tool_tip_array=[
""")
for entry in json_data:
url = entry.get("d")
id_value = self.map_url_to_id.get(url)
if id_value is None:
self.map_url_to_id[url] = next_id
next_id += 1
out_file.write(f"\"{self.get_title(url)}\",\n")
out_file.write(r"""
];
</script>
""")
@staticmethod
def write_js_pc( out_file):
out_file.write("""
<script>
function h(elem) {
var isNumber = /^\d+$/.test(elem.title);
if (isNumber) {
elem.title = tool_tip_array[ elem.title ];
}
}
</script>
""")
@staticmethod
def write_js_mobile(out_file):
out_file.write("""
<script>
function h(elem) {
elem.classList.toggle('down');
next_element_id = parseInt(elem.id) + 1
divelm = document.getElementById(next_element_id);
console.log("elm " + elem.id + " " + next_element_id + " inner" + divelm. innerHTML);
if (divelm.innerHTML == "") {
divelm.innerHTML = tool_tip_array[ elem.title ];
} else {
divelm.innerHTML = "";
}
}
</script>
""")
def get_country_tag(self, url):
description = self.desc_cache.cache_get(url)
if description is None:
return ""
country = self.desc_cache.get_country(description)
if country != "":
return f'<img src="images/{country}.png">'
return ""
def get_title(self, url):
description = self.desc_cache.cache_get(url)
if description is None or description.description == "":
return url
if self.text_renderer.is_show_all_languages():
desc = description.description
else:
desc = description.translations.get( self.text_renderer.translation_lang )
if desc is None:
desc = description.description
if desc.find('"') != -1:
#print(f"Warning: description for {url} contains a quotation mark, desc: {desc}")
desc = desc.replace('"', """)
desc = desc.replace('\n', "\\n")
desc = re.sub(self.clean_tag, '', desc)
if desc.find(url) != -1:
return desc
return url + " - " + desc
def show_translated_cats(self, output_file_name, output_file_name_mobile):
self.text_renderer.set_translation_mode()
for lan in globs.Globals.supported_languages:
self.text_renderer.translation_lang = lan
# render static pages,
self.show_cats_static()
self.show_cats_pc(output_file_name)
self.show_cats_mobile(output_file_name_mobile)
def show_cats(self, output_file_name, output_file_name_mobile):
# render static pages,
self.show_cats_static()
# render the search catalogs.
self.show_cats_pc(output_file_name)
self.show_cats_mobile(output_file_name_mobile)
# show ui text strings
self.text_renderer.write_text()
def show_cats_mobile(self, output_file_name):
if self.text_renderer.translation_lang is not None and self.text_renderer.translation_lang != "":
output_file_name += "_" + self.text_renderer.translation_lang
output_file_name += ".html"
all_bangs, num_entries, unique_bangs, json_data = self.get_all()
with open(output_file_name, "w") as out_file:
self.out_file = out_file
self.text_renderer.out_file = out_file
DuckStuff.write_hdr_mobile(out_file)
self.build_toolip_help_list(json_data, out_file)
DuckStuff.write_js_mobile(out_file)
out_file.write("<table><tr><th>")
self.text_renderer.show_text("Category")
out_file.write("</th><th>")
self.text_renderer.show_text("Sub categories")
out_file.write("</th></tr>\n")
link_num = 1
all_bangs_keys = list(all_bangs)
all_bangs_keys.sort()
for cat in all_bangs_keys:
is_first = True
all_bangs_cat = list(all_bangs[cat])
all_bangs_cat.sort()
out_file.write("<tr><td>")
self.text_renderer.show_text(f"{cat}")
out_file.write("</td><td>")
for catentry in all_bangs_cat:
if not is_first:
out_file.write(",")
out_file.write(" ")
is_first = False
out_file.write(f"<a href=\"#{link_num}\">")
self.text_renderer.show_text(f"{catentry}")
out_file.write("</a>")
link_num = link_num + 1
out_file.write("</td>\n")
out_file.write("</table>\n")
id_item = link_num+1
link_num = 1
for cat in all_bangs_keys:
all_bangs_cat = list(all_bangs[cat])
all_bangs_cat.sort()
for catentry in all_bangs_cat:
out_file.write(f"<hr/><p/><a id=\"{link_num}\"/>\n")
link_num += 1
out_file.write("<hr>")
self.text_renderer.show_text(f"{cat}")
out_file.write(" / ")
self.text_renderer.show_text(f"{catentry}")
out_file.write("</h3><p></p>\n")
pos = 0
out_file.write("<table>\n")
cat_content = sorted(all_bangs[cat][catentry], key=lambda entry : entry[1])
for bang in cat_content:
entry_url = bang[2]
bang_name = bang[0]
bang_title = bang[1]
country_img_tag = self.get_country_tag(entry_url)
if country_img_tag != "":
country_img_tag += " "
out_file.write("<tr><td>")
out_file.write(f"<div id=\"{id_item}\" title=\"{self.map_url_to_id[entry_url]}\" onclick=\"h(this)\" class='arrow'></div> <span><a href=\"javascript:onBang('{bang_name}')\">{country_img_tag}")
self.text_renderer.show_text(f"{bang_title}")
out_file.write(f"</a></span> <span style=\"float: right\">!<a href=\"javascript:onBang('{bang_name}')\">{bang_name}</a></span> ")
id_item += 1
out_file.write(f"<br><div id=\"{id_item}\"></div>")
id_item += 1
out_file.write("</td>")
out_file.write("</tr>")
pos = pos + 1
out_file.write("</table>")
out_file.write("\n<table width='100%'><tr><td>")
self.text_renderer.show_text("Generated on ")
out_file.write(f"{datetime.now()}; ")
self.text_renderer.show_text("number of entries ")
out_file.write(f"{num_entries} ")
self.text_renderer.show_text("unique bangs")
out_file.write(f"{unique_bangs}</td></tr></table>\n<p><p><p>***eof***\n")
def show_cats_static(self):
render_static = main_page.RenderStatic(self.text_renderer)
render_static.render(self.text_renderer.translation_lang)
def show_cats_pc(self, output_file_name):
all_bangs, num_entries, unique_bangs, json_data = self.get_all()
#pprint(all_bangs)
if self.text_renderer.translation_lang is not None and self.text_renderer.translation_lang != "":
output_file_name += "_" + self.text_renderer.translation_lang
output_file_name += ".html"
with open(output_file_name, "w") as out_file:
self.out_file = out_file
self.text_renderer.out_file = out_file
DuckStuff.write_hdr_pc(out_file)
self.build_toolip_help_list(json_data, out_file)
DuckStuff.write_js_pc(out_file)
out_file.write("<table><tr><th>")
self.text_renderer.show_text("Category")
out_file.write("</th><th>")
self.text_renderer.show_text("Sub categories")
out_file.write("</th></tr>\n")
link_num = 1
all_bangs_keys = list(all_bangs)
all_bangs_keys.sort()
for cat in all_bangs_keys:
entry_links = ""
is_first = True
all_bangs_cat = list(all_bangs[cat])
all_bangs_cat.sort()
for catentry in all_bangs_cat:
if not is_first:
entry_links = entry_links + ","
entry_links = entry_links + " "
is_first = False
entry_links = entry_links + f"<a href=\"#{link_num}\">{self.text_renderer.show_text_str(catentry)}</a>"
link_num = link_num + 1
out_file.write(f"<tr><td>{self.text_renderer.show_text_str(cat)}</td><td>{entry_links}</td>\n")
out_file.write("</table>\n")
link_num = 1
num_columns = 3
for cat in all_bangs_keys:
all_bangs_cat = list(all_bangs[cat])
all_bangs_cat.sort()
for catentry in all_bangs_cat:
out_file.write(f"<hr/><p/><a id=\"{link_num}\"/>\n")
link_num += 1
out_file.write(f"<h3>{self.text_renderer.show_text_str(cat)} / {self.text_renderer.show_text_str(catentry)}</h3><p></p>\n")
pos = 0
out_file.write("<table width=\"100%\"><tr>\n")
cat_content = sorted(all_bangs[cat][catentry], key=lambda entry : entry[1])
for bang in cat_content:
if pos % num_columns == 0:
out_file.write("</tr><tr>")
out_file.write("<td>")
entry_url = bang[2]
bang_name = bang[0]
bang_title = bang[1]
country_img_tag = self.get_country_tag(entry_url)
if country_img_tag != "":
country_img_tag += " "
out_file.write(f"<span align=\"left\"><a title=\"{self.map_url_to_id[entry_url]}\" onmouseenter=\"h(this)\" href=\"javascript:onBang('{bang_name}')\">{country_img_tag}{self.text_renderer.show_text_str(bang_title)}</a></span> <span style=\"float: right\">!<a title=\"{self.map_url_to_id[entry_url]}\" onmouseenter=\"h(this)\" href=\"javascript:onBang('{bang_name}')\">{bang_name}</a></span> ")
out_file.write("</td>")
pos = pos + 1
while True:
if pos % num_columns == 0:
break
out_file.write("<td></td>")
pos = pos + 1
out_file.write("</tr></table>")
out_file.write("\n<table width='100%'><tr><td>")
self.text_renderer.show_text("Generated on ")
out_file.write(f"{datetime.now()}; ")
self.text_renderer.show_text("number of entries ")
out_file.write(f"{num_entries} ")
self.text_renderer.show_text("unique bangs")
out_file.write(f"{unique_bangs}</td></tr></table>\n<p><p><p>***eof***\n")
def _parse_cmd_line():
usage = """
Build the html for the duckduckbang meta search tool.
"""
parse = argparse.ArgumentParser(
description=usage, formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
group = parse.add_argument_group("build cache")
group.add_argument(
"--cache",
"-c",
default=False,
action="store_true",
dest="build_cache",
help="build json cache of site descriptions (long process)"
)
group.add_argument(
"--selenium",
"-s",
default=False,
action="store_true",
dest="selenium",
help="enable selenium download"
)
group.add_argument(
"--no-http-client",
"-l",
default=True,
action="store_false",
dest="http_client",
help="disable default python http client http.client (default: enabled)"
)
group.add_argument(
"--timeout",
"-w",
type=int,
default=None,
dest="timeout",
help="timeout in seconds for https client (does not effect selenium)",
)
group = parse.add_argument_group("build html pages")
group.add_argument(
"--html",
"-t",
default=False,
action="store_true",
dest="build_html",
help="building of html files (default off)"
)
group.add_argument(
"--translate",
"-r",
default=False,
action="store_true",
dest="build_translate_html",
help="building translation of html files, note this requires additional preparation steps. (default off)"
)
group.add_argument(
"--verbose",
"-v",
default=False,
action="store_true",
dest="verbose",
help="verbose debug output"
)
group.add_argument(
"--debug",
"-d",
default=False,
action="store_true",
dest="debug",
help="show content of downloaded page"
)
return parse.parse_args()
def _run_cmd():
cmd = _parse_cmd_line()
duck = DuckStuff(cmd.http_client, cmd.selenium, textrender.TextRenderer() )
if cmd.build_cache:
print("Building descriptions...")
duck.build_cache()
if cmd.timeout is not None:
scrapscrap.Global.timeout_sec = cmd.timeout
if cmd.build_html:
print("Building html file...")
duck.show_cats("all_cats", "all_cats_mobile")
if cmd.build_translate_html:
duck.show_translated_cats("all_cats", "all_cats_mobile")
if __name__ == '__main__':
_run_cmd()
#for link in BeautifulSoup(data, parse_only=SoupStrainer('a')):
# if link.has_attr('href'):
# print(link['href'])