-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwikipedia_api_length_check.py
75 lines (59 loc) · 1.55 KB
/
wikipedia_api_length_check.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
import requests
import time
username = 'FILLIN'
password = 'FILLIN' # see https://www.mediawiki.org/wiki/Manual:Bot_passwords
api_url = 'https://en.wikipedia.org/w/api.php'
# initiate the session
session = requests.Session()
# get login token
r1 = session.get(api_url, params={
'format': 'json',
'action': 'query',
'meta': 'tokens',
'type': 'login',
})
r1.raise_for_status()
# log in
r2 = session.post(api_url, data={
'format': 'json',
'action': 'login',
'lgname': username,
'lgpassword': password,
'lgtoken': r1.json()['query']['tokens']['logintoken'],
})
if r2.json()['login']['result'] != 'Success':
raise RuntimeError(r2.json()['login']['reason'])
print(r2.json())
# get the csrf token
PARAMS_2 = {
"action": "query",
"meta": "tokens",
"format": "json"
}
R = session.get(url=api_url, params=PARAMS_2)
DATA = R.json()
print(DATA)
CSRF_TOKEN = DATA['query']['tokens']['csrftoken']
length_check="A"*100000
PARAMS_3 = {
"action": "options",
"format": "json",
"token": CSRF_TOKEN,
"optionname": "userjs-arbitraryKeyName",
# commands start with "command:"
"optionvalue": length_check
}
# send the command
R = session.post(api_url, data=PARAMS_3)
DATA = R.json()
print(DATA)
PARAMS_4 = {
"action": "query",
"format": "json",
"meta": "userinfo",
"uiprop":"options"
}
R = session.post(api_url, data=PARAMS_4)
DATA = R.json()
result = DATA['query']['userinfo']['options']['userjs-arbitraryKeyName']
print(result)