-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
70 lines (54 loc) · 2.46 KB
/
Copy pathscraper.py
File metadata and controls
70 lines (54 loc) · 2.46 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
from datetime import datetime
from pprint import pprint
from os import getenv
import requests, traceback
# an attempt at an api scraper to grab my matches from ancestry DNA
# need to copy all the cookies to this env var.
COOKIES = getenv('MY_COOKIES')
MY_UUID = getenv('REFERENCE_USER_UUID')
if not COOKIES or not MY_UUID:
raise Exception("please set the environmental variables in env.sh")
def get_page(page_index):
# ancestry paging is weird:
other_url = ''
if page_index > 0:
other_url = 'bookmarkdata={%22moreMatchesAvailable%22:true,%22lastMatchesServicePageIdx%22:' + str(page_index) + '}'
page = 1 + page_index * 4
unix_time = int(datetime.now().timestamp())
url = f'https://www.ancestry.com/discoveryui-matchesservice/api/samples/{MY_UUID}/matches/list?page={page}&sortby=RELATIONSHIP&_t={unix_time}&{other_url}'
# debug information for the url:
# print(url)
headers = {
'cookie': COOKIES,
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
}
response = requests.get(url, headers=headers)
if response.status_code != 200:
raise Exception(f"Failed to get a 200 response\n: {response}")
return response.json()
if __name__ == '__main__':
results = {}
page_index = 0
all_matches = []
try:
# CSV header
print("match index, shared centimorgans, display name, user uuid, ")
while True:
page_json = get_page(page_index)
page_index += 1
# failure criteria, json response either doesn't have a matchGroup, or its empty:
if not (page_json.get('matchGroups', False) and len(page_json['matchGroups']) > 0):
break
# append a match:
for match_group in page_json['matchGroups']:
for match_user in match_group['matches']:
# display the raw data
# print(match_user)
all_matches.append(match_user)
print(f'{len(all_matches)}, {match_user["relationship"]["sharedCentimorgans"]},{match_user["publicDisplayName"]},{match_user["testGuid"]}')
# below a minimum relationship rate, you dont care about the results anymore:
if match_user["relationship"]["sharedCentimorgans"] < 25:
exit(0)
# pprint(all_matches)
except Exception:
traceback.print_exc()