-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_downloader.py
355 lines (301 loc) · 14 KB
/
api_downloader.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import json
import os
import random
import time
from typing import List, TypedDict
import dotenv
import requests
import sqlalchemy.engine
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from models.collection import Base, Collection, Section
API_SEARCH = "https://www.ancestry.com/search/collections/catalog/api/search"
USER_DATA = "https://www.ancestry.com/app-api/header/userdata/2.1"
class InvalidInputError(ValueError):
"""Invalid collection id or other input is passed"""
class CollectionEntryDescription(TypedDict):
DbId: str
CultureId: str
TextType: int
Value: str
class CollectionEntry(TypedDict):
dbId: str
nativeCultureId: str
categoryId: str
title: str
recordCount: str
collection: str
activeDate: str
updatedDate: str
activity: str
collectionFeature: str
description: CollectionEntryDescription
class User(TypedDict):
name: str
image: bool
subscribeUrl: str
subscribeText: str
isFullAccessFreeTrialer: bool
class UserData(TypedDict):
user: User
mostRecentlyViewedTreeId: int
hintcount: int
messagecount: int
def format_pagination_body(page: int, size: int, paging_token=""):
"""
:param page:
:param size:
:param paging_token:
:return:
"""
return {
"queryTerms": {},
"sortByKey": "ACTIVE_DATE",
"cultureId": "en-US",
"pagingInfo": {
"PageNumber": page,
"PagingToken": paging_token,
"RecordsPerPage": size
}
}
def random_sleep(min_sec: float = 1, max_sec: float = 30, log=True, factor=1):
"""Sleep for a random number of seconds
:param min_sec:
:param max_sec:
:param log:
:param factor:
:return:
"""
min_sec = min_sec * factor
max_sec = max_sec * factor
sleep_time = random.randint(int(min_sec * 1000), int(max_sec * 1000)) / 1000
if log:
print(f"Waiting for {sleep_time}s")
time.sleep(sleep_time)
class Controller(object):
_authenticated: bool = False
_engine_initialized: bool = False
_session: requests.Session
_db_engine: sqlalchemy.engine.Engine
_sqlite_file: str = "data/database.db"
_username: str
_password: str
def __init__(self, user: str, pas: str):
"""
:param user: Ancestry.com username
:param pas: Ancestry.com password
"""
self._session = requests.Session()
self._session.headers.update({
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/39.0.2171.95 Safari/537.36 '
})
self._username = user
self._password = pas
self._login()
def _login(self):
"""Authenticate with the ancestry API"""
if not self._authenticated:
print("Logging in")
login_body = {"username": self._username, "password": self._password}
response = self._session.post("https://www.ancestry.com/account/signin/frame/authenticate",
data=login_body,
headers={"referer": "https://www.ancestry.com/account/signin/frame?"})
self._authenticated = response.status_code == 200
def _init_db_engine(self):
self._db_engine = create_engine(f'sqlite:///{self._sqlite_file}', echo=True)
Base.metadata.create_all(self._db_engine)
self._engine_initialized = True
def _get_db_session(self, autocommit=False) -> Session:
if not self._engine_initialized:
self._init_db_engine()
return sessionmaker(bind=self._db_engine, autocommit=autocommit)()
def _get_metadata_target(self) -> int:
""" Selects next target for updating collection metadata
:return: Collection ID to target
"""
session = self._get_db_session()
collection = session.query(Collection).order_by(Collection.time_updated.asc(),
Collection.collection_feature.asc()).filter_by(
database_name=None).first()
return collection.collection_id
def get_user_data(self) -> UserData:
res = self._session.get(USER_DATA)
return res.json()
def save_collection_metadata(self, dbid):
""" Save metadata to the collection record from endpoints other than search
:param dbid: Optional, which collection id to save metadata for. Defaults to the least recently updated record.
:return: None
"""
if not dbid:
dbid = self._get_metadata_target()
url: str = f"https://www.ancestry.com/imageviewer/api/media/info-by-id?dbId={dbid}"
url2: str = f"https://www.ancestry.com/imageviewer/api/collection/id?dbId={dbid}"
db_session = self._get_db_session()
exists_query = db_session.query(Collection).filter_by(collection_id=dbid)
if exists_query.scalar():
collection = exists_query.first()
if collection.collection_feature != "indexOnly":
parsed = self._session.get(url)
if parsed.ok:
image_info = parsed.json()['imageInfo']
collection.database_name = str(image_info["collectionInfo"]['databaseName'])
collection.set_levels(list(image_info['structureType'].values()))
collection.category_name = str(image_info["collectionInfo"]['primaryCategoryName'])
collection.category_id = str(image_info["collectionInfo"]['primaryCategoryId'])
collection.publication_year = int(image_info['collectionInfo']['publicationYear'])
parsed = self._session.get(url2)
collection.collection_title = parsed.json()['collectionTitle']
collection.source_info = parsed.json()['onlineSourceInfo']
collection.is_yearbook_collection = parsed.json()['isYearbookCollection']
db_session.commit()
def save_collections_to_disk(self, n: int = 1000):
def search_post(page: int, size: int, paging_token=""):
return self._session.post(
API_SEARCH,
json=format_pagination_body(page, size, paging_token=paging_token)
)
def get_total_results() -> int:
return search_post(1, 1).json()['TotalResults']
# get total collection count
total_results = get_total_results()
def log_page(page_num: int, retry=False):
if retry:
print(f"rate limit hit on page {page_num}. trying again\n")
print(f"getting page {page_num} of {total_results}")
# Initialize loop variables
pagination_token = [""]
loop_count = (total_results // n) + 1
# get n results at a time
for i in range(loop_count):
log_page(i)
random_sleep()
with open(f"data/temp/collections{i}.json", "w") as f:
res = search_post(i + 1, n, paging_token=pagination_token[0])
if res.ok:
pagination_token[0] = res.json()['PagingInfo']['PagingToken']
f.write(res.text)
else:
log_page(i, retry=True)
random_sleep(factor=3)
res = search_post(i + 1, n, paging_token=pagination_token[0])
if not res.ok:
raise ResourceWarning(f"rate limit critical on page {i}")
else:
pagination_token[0] = res.json()['PagingInfo']['PagingToken']
f.write(res.text)
def load_collections_into_db_from_disk(self):
db_session = self._get_db_session()
for file in os.scandir("data/temp"):
file_name: str = file.name
if file.is_file() and "collections" in file_name and file_name.endswith(".json"):
print(f"parsing {file_name}")
with open(file.path) as file_io:
file_data = json.loads(file_io.read())
entries: List[CollectionEntry] = file_data['gridData']
for entry in entries:
exists_query = db_session.query(Collection).filter_by(collection_id=int(entry['dbId']))
if exists_query.scalar():
collection = exists_query.first()
collection.collection_title = entry['title']
collection.collection_created = entry['activeDate']
collection.collection_updated = entry['updatedDate']
collection.collection_feature = entry['collectionFeature']
collection.native_culture_id = entry['nativeCultureId']
collection.category_id = entry['categoryId']
collection.record_count = int(entry['recordCount'].replace(",", ""))
collection.collection_collection = entry['collection']
collection.description = entry['description']['Value']
else:
collection = Collection(
collection_id=int(entry['dbId']),
collection_title=entry['title'],
collection_created=entry['activeDate'],
collection_updated=entry['updatedDate'],
collection_feature=entry['collectionFeature'],
native_culture_id=entry['nativeCultureId'],
category_id=entry['categoryId'],
record_count=int(entry['recordCount'].replace(",", "")),
collection_collection=entry['collection'],
description=entry['description']['Value']
)
db_session.add(collection)
db_session.commit()
def get_metadata_loop(self, limit_seconds=600):
started = time.time()
while True:
self.save_collection_metadata(self._get_metadata_target())
random_sleep(.25, .75)
if time.time() - started > limit_seconds:
break
def get_browse_values(self, dbid: int):
self.save_collection_metadata(dbid=dbid)
db_session = self._get_db_session()
exists_query = db_session.query(Collection).filter_by(collection_id=dbid)
if exists_query.scalar():
collection = exists_query.first()
def format_url(path: List[str] = None) -> str:
url = f"https://www.ancestry.com/imageviewer/api/media/browse-elements?dbId={dbid}"
if path is not None:
url += f"&path={'|'.join(path)}"
return url
# FIXME: When path has a repeated last item, the results are the same
def get_children(path: List[str] = None) -> List[Section]:
children: List[Section] = []
if path is None:
path = []
print(path)
url = format_url(path=path)
response = self._session.get(url).json()["browseElement"]
for x2 in response["BrowseSubElements"]:
value2, locale_value2, description2 = x2["PathValue"], x2["LocalizedPathValue"], None
if "PathDescription" in x2.keys():
description2 = x2["PathDescription"]
child_section = Section(value=value2, locale_value=locale_value2,
has_child_levels=response["ContainsChildLevels"],
collection=collection.id)
if description2:
child_section.description = description2
if response["ContainsChildLevels"]:
new_path = path.copy()
new_path.append(value2)
child_section.children = get_children(new_path)
children.append(
child_section
)
return children
incompatible_collections = ["Dictionaries, Encyclopedias & Reference"]
collection.sections = []
res = self._session.get(format_url())
print(res.text)
browse_element = res.json()["browseElement"]
has_children = browse_element["ContainsChildLevels"]
index = 1
for x in browse_element["BrowseSubElements"]:
value, locale_value, description = x["PathValue"], x["LocalizedPathValue"], None
if "PathDescription" in x.keys():
description = x["PathDescription"]
print(f"section {index} of {len(browse_element['BrowseSubElements'])}")
index += 1
section = Section(value=value, locale_value=locale_value, has_child_levels=has_children)
if description:
section.description = description
if has_children:
print("getting children for top section")
section.children = get_children([section.value])
collection.sections.append(section)
db_session.commit()
db_session.commit()
db_session.close()
if __name__ == '__main__':
dotenv.load_dotenv()
username: str = os.getenv("ANCESTRY_USERNAME")
password: str = os.getenv("ANCESTRY_PASSWORD")
if username is None or password is None:
raise EnvironmentError("Username and/or password environment variables not set")
controller = Controller(username, password)
# controller.save_collections_to_disk()
# controller.load_collections_into_db_from_disk()
# controller.get_metadata_loop()
print(controller.get_browse_values(int(input(">>"))))
print(controller.get_user_data())