forked from sohomdatta1/dabfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikiinteractor.py
112 lines (104 loc) · 3.8 KB
/
wikiinteractor.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
import requests as r
from replicadb import get_conn
import re
import random
def get_raw_text(pagename, proj):
resp = r.post(f'https://{proj}.wikipedia.org/w/api.php', data={
"action": "query",
"format": "json",
"prop": "revisions",
"titles": pagename,
"formatversion": "2",
"rvprop": "content",
"rvslots": "*"
}, timeout=50, headers={'X-Api-User-Agent': 'toolforge-dabfix/1.0'})
data = resp.json()
if 'missing' in data['query']['pages'][0]:
return {
"success": False
}
return {
"text": data["query"]['pages'][0]["revisions"][0]["slots"]["main"],
"success": True
}
def get_parsed_wikitext(pagename, proj):
resp = r.post(f'https://{proj}.wikipedia.org/w/api.php', data={
"action": "parse",
"format": "json",
"page": pagename,
"prop": "text",
"formatversion": "2"
}, timeout=50, headers={'X-Api-User-Agent': 'toolforge-dabfix/1.0'})
data = resp.json()
if 'parse' not in data:
return {
"success": False
}
return {
"text": data["parse"]["text"],
"success": True
}
def getdabs_w(proj: str, pagename: str):
with get_conn(f'{proj}wiki') as conn:
with conn.cursor() as cursor:
pagename=pagename.replace(' ', '_')
cursor.execute("select page_id from page where page_title = %s and page_namespace = 0", (pagename))
res = cursor.fetchall()
pageid = res[0][0]
print(pageid)
cursor.execute("""SELECT page_title, pp_propname
FROM page LEFT
JOIN page_props ON pp_page = page_id
AND pp_propname = 'disambiguation'
WHERE page_namespace = 0
AND page_title IN
(select pl_title
from pagelinks l
where l.pl_namespace = 0
and l.pl_from = %s)
AND pp_propname = 'disambiguation'""", (str(pageid)))
rows = cursor.fetchall()
resp = []
for i in rows:
resp.append(i[0].decode('utf-8'))
return resp
def makesenseofedits(proj, pagename, edits):
text = get_raw_text(pagename, proj)['text']['content']
dabs = getdabs_w(proj, pagename)
print(dabs)
for i in range(len(dabs)):
if i < len(edits) and edits[i]:
text = re.sub( f"\\[\\[{dabs[i].replace('_', '[ _]')}", f'[[{edits[i]}', text )
print(text)
return text
def editwikitext(proj: str, pagename: str, auth, edits, summary: str):
text = makesenseofedits(proj, pagename, edits)
R = r.get(url=f'https://{proj}.wikipedia.org/w/api.php', params={
"action": "query",
"meta": "tokens",
"format": "json"
}, auth=auth, timeout=50)
DATA = R.json()
CSRF_TOKEN = DATA['query']['tokens']['csrftoken']
resp = r.post(f'https://{proj}.wikipedia.org/w/api.php', data={
"action": "edit",
"title": pagename,
"text": text,
"token": CSRF_TOKEN,
"summary": summary + ' (dabfix)',
"format": "json",
},
auth=auth,
timeout=50)
return resp.json()
def generaterandomdisambigs(proj):
resp = r.post(f'https://{proj}.wikipedia.org/w/api.php', data={
"action": "query",
"format": "json",
"list": "querypage",
"formatversion": "2",
"qppage": "DisambiguationPageLinks",
"qplimit": "500"
}, timeout=50, headers={'X-Api-User-Agent': 'toolforge-dabfix/1.0'})
data = resp.json()
return random.choice(data['query']['querypage']["results"])["title"]