-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_tools.py
34 lines (26 loc) · 931 Bytes
/
api_tools.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
"""
Tools for dealing with WikiTree API.
"""
import json
import re
import urllib.parse
import urllib.request
def api_req(**params):
encoded_params = urllib.parse.urlencode(params)
resp = urllib.request.urlopen("https://api.wikitree.com/api.php",
data=encoded_params.encode("utf-8"))
return json.loads(resp.read())
class RedirectInfo:
def __init__(self, json_response):
self.status = json_response[0]["status"]
# Save Redirection info
self.redirects_to = None
if self.status == 0:
m = re.fullmatch(r"#REDIRECT \[\[(.*)\]\]", json_response[0]["bio"])
if m:
self.redirects_to = m.group(1)
def redirect_info(profile_num_or_id):
"""Lookup a profile by # or id and figure out it is a redirect or not.
If it is, return the id of the profile it now redirects to."""
resp = api_req(action="getBio", key=profile_num_or_id)
return RedirectInfo(resp)