forked from wikitree/wikitree-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
executable file
·145 lines (119 loc) · 5.19 KB
/
python.py
File metadata and controls
executable file
·145 lines (119 loc) · 5.19 KB
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
#!/usr/bin/python
#
# This is a bare-bones example of authenticating as a "known" (not interactive through a browser)
# WikiTree member for use with the WikiTree API. In order to carry the authentication through,
# a Session is maintained with https://api.wikitree.com which holds the session state via
# cookie, as a browser would.
#
#####################################################################
import requests
import re
import sys
from getpass import getpass
# Define the WikiTree API endpoint
API_URL = "https://api.wikitree.com/api.php"
# Optionally define the WikiTree member to use for API requests.
# These values should never be committed to a repository, and could be instead loaded
# from a settings file, etc. If these are left blank, then they are queried for on the
# command line.
LOGIN_EMAIL = ''
LOGIN_PASSWORD = ''
#####################################################################
##########
# If we don't have the login information, query the user for it.
##########
if (LOGIN_EMAIL):
email = LOGIN_EMAIL
else:
email = raw_input("Email:")
if (LOGIN_PASSWORD):
password = LOGIN_PASSWORD
else:
password = getpass("Password for "+email+":")
##########
# We use a Session to hold the state (via cookie jar) for the API queries
##########
print "Starting Session.\n"
apiSession = requests.Session()
##########
# Step 1 - POST the clientLogin action with our member credentials.
#
# We post with allow_redirect = False because we don't want the
# Python request to automatically follow redirects. We want the initial response (still on api.wikitree.com)
# and not the redirected-to destination.
#
# Note that the apiResponse here will, on successful login, be a redirection.
# In a browser interaction we'd include a returnURL value in postData to send the browser
# back to our app. Here we're just capturing the authcode from the Location redirection
# directly.
##########
print "POSTing clientLogin(email,password)"
postData = { 'action':'clientLogin', 'doLogin': 1, 'wpEmail': email, 'wpPassword': password }
apiResponse = apiSession.post(API_URL, data=postData, allow_redirects=False, auth=('wikitree','wikitree'))
##########
# If we have a "Location" redirection with an authcode value as our response, then
# the login was successful. Otherwise, we failed.
##########
if ((apiResponse.status_code != 302) or (apiResponse.headers['Location'] is None)):
print "clientLogin POST did not return expected 302 Redirect."
# On failure, the response content will be a redisplay of the web page with the
# API Client Login form.
#print "Response status_code = "+str(apiResponse.status_code)
#print ("Header: "+str(apiResponse.headers))
#print ("Content:"+apiResponse.text)
print "\nDie\n";
sys.exit()
matches = re.search('authcode=(.*)', apiResponse.headers['Location'])
if (matches == None):
print "clientLogin POST failed - couldn't find authcode"
print "Response status_code = "+str(apiResponse.status_code)
print ("Header: "+str(apiResponse.headers))
print "\nDie\n"
sys.exit()
authcode = matches.group(1)
print "clientLogin succeeded: authcode="+authcode
##########
# Step 2 - POST back the authcode we got. This completes the login/session setup
# at api.wikitree.com. Since we use the same Session for the post, the cookies are all
# saved. A success here is a 200 and we'll have WikiTree session cookies.
##########
print "POSTing clientLogin(authcode)"
postData = { 'action': 'clientLogin', 'authcode': authcode }
apiResponse = apiSession.post(API_URL, data=postData, allow_redirects=False)
if (apiResponse.status_code != 200):
print "clientLogin(authcode) failed."
print "Response status_code = "+str(apiResponse.status_code)
print ("Header: "+str(apiResponse.headers))
print "\nDie\n"
sys.exit()
# The cookies set by the API look like:
# {'wikitree_wtb__session': '<<session_id>>', 'wikitree_wtb_Token': '<<token>>', 'wikidb_wtb_UserName': '<<WikiTree ID>>', 'wikitree_wtb_UserID': '<<WikiTree user_id>>'}
wtCookie = apiSession.cookies.get_dict()
if (wtCookie is None):
print "clientLogin(authcode) failed -- No Cookies."
print "\nDie\n"
sys.exit()
print wtCookie
#if ('wikidb_wtb_UserName' not in wtCookie):
# print "clientLogin(authcode) failed -- No WikiTree Session Info found in Cookies."
# print "\nDie\n"
# sys.exit()
print "clientLogin(authcode) succeeded: "+wtCookie['wikidb_wtb_UserName']
##########
# At this point the member should be logged into the API site, with their
# session data stored in a CookieJar in our Session. We can now query the
# API for data that is restricted to profiles with that member on the Trusted List.
# As an example, get the logged-in member's profile data itself.
##########
key = wtCookie['wikidb_wtb_UserName']
fields = 'Id,Name,FirstName,LastNameAtBirth,LastNameCurrent,BirthDate'
print "\ngetProfile("+key+")"
apiResponse = apiSession.post(API_URL, {'action':'getProfile', 'key':key, 'fields':fields}, auth=('wikitree','wikitree'))
data = apiResponse.json()
print "Data from JSON:"
print data
print "\n"
print "First Name: "+ data[0]['profile']['FirstName']
print "Last Name: "+ data[0]['profile']['LastNameAtBirth']
print "Birth Date: "+ data[0]['profile']['BirthDate']
print "\n\nDone\n"