-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiki_important.py
executable file
·75 lines (56 loc) · 1.87 KB
/
wiki_important.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
#!/usr/bin/python3
import re, sys
import time, threading
import MwApiInterface
from concurrent.futures import ThreadPoolExecutor
import logging
api = MwApiInterface.MwApiInterface('http://fr.wikipedia.org/w/api.php')
def get_article():
data = api.raw_query_request( {'generator':'random', 'action':'query', 'prop':'categories', 'cllimit':500, 'clshow':'!hidden', 'grnnamespace':1} )
(page_id, page_data) = data['query']['pages'].popitem()
title = page_data['title']
art_title = re.sub("^Discussion:", "", title)
try:
categories = [ categ['title'] for categ in page_data['categories'] ]
except KeyError:
categories = []
imp = 0;
for categ in categories:
if re.search("d'importance", categ):
if re.search("inconnue$", categ):
pass
elif re.search("faible$", categ):
imp += 1
elif re.search("moyenne$", categ):
imp += 10
elif re.search("élevée$", categ):
imp += 100
elif re.search("maximum$", categ):
imp += 1000
else:
raise Exception('Unknown importance: {}'.format(categ))
return (art_title, imp)
def get_a_page(debug=False):
ts = time.time()
executor = ThreadPoolExecutor(max_workers=10)
futures = []
for ifut in range(100):
futures.append(executor.submit(get_article))
max_score = 0
max_title = None
for future in futures:
(title, score) = future.result()
if score > max_score:
max_score = score
max_title = title
te = time.time()
logging.debug("{} ({})".format(max_title, max_score))
logging.debug("total time:{}".format(te-ts))
return (max_title, max_score)
def get_link(debug=False):
from urllib.parse import quote
page = get_a_page()[0]
return "<a href=http://fr.wikipedia.org/wiki/%s>%s</a>" % (quote(page), page)
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
get_a_page(debug=True)