-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23andme.py
143 lines (124 loc) · 5.52 KB
/
23andme.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
import logging
import re
import progressbar
import psycopg2
import requests
logging.basicConfig(level=logging.INFO)
# reload(sys)
# sys.setdefaultencoding("utf-8")
login = ''
password = ''
class meand23():
def __init__(self, login, password):
self.s = requests.Session()
self.login = login
self.password = password
self.current_profile = None
self.all_profiles = []
database = "dbname=gis user=gis"
a = psycopg2.connect(database)
a.autocommit = True
self.cursor = a.cursor()
self.logon()
def logon(self):
logging.info('logging in')
login_page = self.s.get('https://www.23andme.com/cas/signin/')
payload = {
'username': login,
'password': password,
'redirect': None,
'source_flow': None,
'__source_node__': 'start',
'__form__': 'login',
'__context__': re.search('name="__context__" value="(.+?)" />', login_page.text).groups()[0]
}
login_page = self.s.post('https://www.23andme.com/cas/signin/', data=payload)
self.current_profile = re.search('"profile_id": "(.+?)"', login_page.text).groups()[0]
self.all_profiles = list(set(re.findall('profile-id=(.+?)&', login_page.text)))
self.all_profiles.insert(0, self.current_profile)
logging.info('logged in, found profiles %s' % str(self.all_profiles))
def change_profile(self, profile):
if profile not in self.all_profiles:
raise 'unknown profile %s' % profile
if profile != self.current_profile:
logging.info('changing profile to %s' % profile)
self.s.get('https://you.23andme.com/switch-profile/?profile-id=%s&redirect-uri=/' % profile)
self.current_profile = profile
def sync_all_humans(self, ):
for profile in self.all_profiles:
self.change_profile(profile)
# sync humans
matches = []
logging.info('syncing human profiles')
profiles = self.s.get(
'https://you.23andme.com/tools/relatives/ajax/?offset=0&limit=99999').json()
for human in profiles['relatives']:
if human['human_id']:
self.store_human(
{
'hid': human['human_id'],
'first_name': human['first_name'],
'last_name': human['last_name'],
'profile_image_url': human['img'],
'is_open_sharing': human['new_share_status'] == 'OPEN_SHARING',
'sex': human['sex']
}
)
self.store_relation(
profile,
human['human_id'],
float(human['pct'].strip('%')),
human['rel_alg']
)
matches.append(human['match_id'])
# sync relations of match
logging.info('syncing human relations')
bar = progressbar.ProgressBar(widgets=[progressbar.Percentage(), ' ',
progressbar.Bar(marker=">", left='[', right=']'), ' ',
progressbar.ETA()])
for match in bar(matches):
try:
relations = self.s.get(
'https://you.23andme.com/tools/compare/match/relatives_in_common/?remote_id=%s&limit=1000000&offset=0' % match,
timeout=20).json()[
'relatives_in_common']
for r in relations:
if r['owner_ehid'] and r['remote_ehid']:
self.store_relation(
r['remote_ehid'],
r['owner_ehid'],
float(r['remote_percentage'].strip('%')),
r['remote_rel_alg_label']
)
self.store_relation(
r['local_ehid'],
r['owner_ehid'],
float(r['local_percentage'].strip('%')),
r['local_rel_alg_label']
)
except requests.exceptions.Timeout:
pass
def store_human(self, human):
if not human['hid']:
logging.warning('bad human row write attempt - %s' % str(human))
return
self.cursor.execute(
'''insert into humans_23andme
(hid, first_name, last_name, profile_image_url, is_open_sharing, sex) values
(%s, %s, %s, %s, %s, %s)
on conflict (hid) do nothing;
''', (human['hid'], human['first_name'], human['last_name'], human['profile_image_url'], human['is_open_sharing'],
human['sex']))
def store_relation(self, hid1, hid2, percentage, label):
if not hid1 or not hid2:
logging.warning('bad relative row write attempt -(%s, %s, %s, %s)' % (hid1, hid2, percentage, label))
return
self.cursor.execute(
'''insert into relatives_23andme
(hid1, hid2, percentage, label) values
(%s, %s, %s, %s)
on conflict (hid1, hid2) do update set percentage = EXCLUDED.percentage, label = EXCLUDED.label;
''', (hid1, hid2, percentage, label))
if __name__ == "__main__":
meconnector = meand23(login, password)
meconnector.sync_all_humans()