-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
267 lines (222 loc) · 6.9 KB
/
util.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
import base64
import json
import os.path
import requests
with open(os.path.dirname(__file__) + '/bot-credentials.json', 'r') as f:
credentials = json.load(f)
username = credentials['username']
password = base64.b64decode(credentials['password']).decode('utf-8')
def normalize_pagename(pagename):
return pagename.replace(' ', '_')
def get_edit_token(session, api_url):
login_token = session.get(api_url, params={
'format': 'json',
'action': 'query',
'meta': 'tokens',
'type': 'login',
})
login_token.raise_for_status()
login = session.post(api_url, data={
'format': 'json',
'action': 'login',
'lgname': username,
'lgpassword': password,
'lgtoken': login_token.json()['query']['tokens']['logintoken'],
})
if login.json()['login']['result'] != 'Success':
raise RuntimeError(login.json()['login']['reason'])
edit_token = session.get(api_url, params={
'format': 'json',
'action': 'query',
'meta': 'tokens',
})
return edit_token.json()['query']['tokens']['csrftoken']
def get_page(session, api_url, title):
page_info = session.get(api_url, params={
'format': 'json',
'action': 'query',
'assert': 'user',
'titles': title,
'prop': 'revisions',
'rvprop': 'content'
})
page = page_info.json()['query']['pages']
revisions = list(page.values())[0]['revisions'][0]
content = list(revisions.values())[2]
return content
def get_page_safe(session, api_url, title):
page_info = session.get(api_url, params={
'format': 'json',
'action': 'query',
'assert': 'user',
'titles': title,
'prop': 'revisions',
'rvprop': 'content'
})
page = page_info.json()['query']['pages']
if 'revisions' in list(page.values())[0]:
revisions = list(page.values())[0]['revisions'][0]
content = list(revisions.values())[2]
return content
return ''
def edit_page(session, api_url, edit_token, title, text, summary, prepend = False): # can also create pages
data={
'format': 'json',
'action': 'edit',
'assert': 'user',
'summary': summary,
'title': title,
'bot': True,
'token': edit_token,
}
if prepend:
data['prependtext'] = text
else:
data['text'] = text
return session.post(api_url, data=data)
def get_allpages(session, api_url, aplimit = '5000', apfilterredir = 'all', apnamespace = '0'):
allpages = session.get(api_url, params={
'format': 'json',
'action': 'query',
'assert': 'user',
'list': 'allpages',
'aplimit': aplimit,
'apfilterredir': apfilterredir,
'apnamespace' : apnamespace
})
return allpages.json()['query']['allpages']
def get_backlinks(session, api_url, bltitle, bllimit = '1000', blfilterredir = 'all'):
backlinks = session.get(api_url, params={
'format': 'json',
'action': 'query',
'assert': 'user',
'list': 'backlinks',
'bltitle': bltitle,
'bllimit': bllimit,
'blfilterredir' : blfilterredir
})
return backlinks.json()['query']['backlinks']
def get_imageusage(session, api_url, iutitle, iulimit = '1000', iufilterredir = 'all'):
imageusage = session.get(api_url, params={
'format': 'json',
'action': 'query',
'assert': 'user',
'list': 'imageusage',
'iutitle': iutitle,
'iulimit': iulimit,
'iufilterredir' : iufilterredir
})
return imageusage.json()['query']['imageusage']
def get_wantedpages(session, api_url, qpoffset = '0', qplimit = '5000'):
wantedpages = session.get(api_url, params={
'format': 'json',
'action': 'query',
'assert': 'user',
'list': 'querypage',
'qppage': 'Wantedpages',
'qplimit': qplimit,
'qpoffset': qpoffset
})
return wantedpages.json()['query']['querypage']['results']
def get_page_info(session, api_url, title):
response = session.get(api_url, params={
'format': 'json',
'action': 'query',
'assert': 'user',
'titles': title,
'prop': 'info'
})
pages = response.json()['query']['pages']
page_info = list(pages.values())[0]
return page_info
def get_categorymembers(session, api_url, cmtitle, cmlimit = '400'):
categorymembers = session.get(api_url, params={
'format': 'json',
'action': 'query',
'assert': 'user',
'list': 'categorymembers',
'cmtitle': cmtitle,
'cmlimit': cmlimit,
'cmprop' : 'title'
})
return categorymembers.json()['query']['categorymembers']
def get_user_groups(session, api_url):
user_groups = session.get(api_url, params={
'format': 'json',
'action': 'query',
'assert': 'user',
'meta': 'userinfo',
'uiprop': 'groups'
})
return user_groups.json()['query']['userinfo']['groups']
def upload_file(session, api_url, edit_token, filename, file, text):
data = {
'format': 'json',
'action': 'upload',
'assert': 'user',
'text': text,
'filename': filename, # no File: prefix
'text': text,
'token': edit_token,
'ignorewarnings': True
}
response = session.post(api_url, files = {'file': (filename, file)}, data=data)
return response
def update_file(session, api_url, edit_token, filename, file):
data = {
'format': 'json',
'action': 'upload',
'assert': 'user',
'filename': filename, # no File: prefix
'token': edit_token
}
response = session.post(api_url, files = {'file': (filename, file)}, data=data)
# file already exists and there are no other errors - the expected response
if response.json()['upload']['result'] == 'Warning' and ('exists' in response.json()['upload']['warnings']) and response.json()['upload']['warnings']['exists'] == normalize_pagename(filename) and len(response.json()['upload']['warnings']) == 1:
data['ignorewarnings'] = True
data['filekey'] = response.json()['upload']['filekey']
response = session.post(api_url, data=data)
elif response.json()['upload']['result'] == 'Warning':
print('Could not upload file ' + filename)
print(response.json()['upload']['warnings'])
return response
def move_page(session, api_url, edit_token, frm, to, summary, redirect = True):
data = {
'format': 'json',
'action': 'move',
'assert': 'user',
'from': frm,
'to': to,
'reason': summary,
'token': edit_token,
'movetalk': True,
'ignorewarnings': True
}
if not redirect:
data['noredirect'] = True
return session.post(api_url, data=data)
def protect_page(session, api_url, edit_token, title, summary):
data = {
'format': 'json',
'action': 'protect',
'assert': 'user',
'title': title,
'protections': 'edit=sysop|move=sysop',
'reason': summary,
'expiry': 'infinite',
'token': edit_token
}
return session.post(api_url, data=data)
class DictUtil:
@staticmethod
def get_optional_list(dict, key):
return dict.get(key, [])
@staticmethod
def get_optional_string(dict, key):
return dict.get(key, "")
@staticmethod
def get_optional_number(dict, key):
return dict.get(key, 0)
@staticmethod
def get_optional_dict(dict, key):
return dict.get(key, {})