forked from wikimedia/mediawiki-api-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_my_watchlist_feed.py
54 lines (41 loc) · 1.08 KB
/
get_my_watchlist_feed.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
#!/usr/bin/python3
"""
get_my_watchlist_feed.py
MediaWiki API Demos
Demo of `Feedwatchlist` module: Get the watchlist feed
for the account making the request.
MIT license
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
# Step 1: Retrieve a login token
PARAMS_1 = {
"action": "query",
"meta": "tokens",
"type": "login",
"format": "json"
}
R = S.get(url=URL, params=PARAMS_1)
DATA = R.json()
LOGIN_TOKEN = DATA['query']['tokens']['logintoken']
# Step 2: Send a post request to log in. For this login
# method, Obtain credentials by first visiting
# https://en.wikipedia.org/wiki/Special:BotPasswords/
# See https://www.mediawiki.org/wiki/API:Login for more
# information on log in methods.
PARAMS_2 = {
"action": "login",
"lgname": "user_name",
"lgpassword": "password",
"format": "json",
"lgtoken": LOGIN_TOKEN
}
R = S.post(URL, data=PARAMS_2)
# Step 3: Request the account's own watchlist feed
PARAMS_3 = {
"action": "feedwatchlist"
}
R = S.get(url=URL, params=PARAMS_3)
DATA = R.text
print(DATA)