-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
121 lines (93 loc) · 3.63 KB
/
client.py
File metadata and controls
121 lines (93 loc) · 3.63 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
import requests
import json
import tarfile
import io
import time
import os
from config import *
def list_notebooks():
res = requests.get(
API_BASE_URL + "/notes",
cookies = COOKIES,
headers = {"User-Agent": USER_AGENT},
proxies=PROXIES,
verify=not DEBUG,
)
if res.status_code != 200:
raise Exception("Unable to list notes")
res_json = json.loads(res.text)
for i, item in enumerate(res_json["itemsList"]):
print(i, item["title"], item["id"])
return res_json["itemsList"]
def get_render_token(notebook_id):
get_params = f"notebookId={notebook_id}&marketplaceId={MARKETPLACE_ID}"
res = requests.get(
BASE_URL + "/openNotebook?" + get_params,
cookies = COOKIES,
headers = {"User-Agent": USER_AGENT},
proxies=PROXIES,
verify=not DEBUG,
)
if res.status_code != 200:
raise Exception("Unable to open notebook")
res_json = json.loads(res.text)
return res_json
def is_notebook_updated(notebook, notebook_id):
SAVE_DIR = os.path.join(NOTEBOOK_DIR, notebook_id)
metadata_file_path = os.path.join(SAVE_DIR, "metadata")
if not os.path.isfile(metadata_file_path):
return True
try:
cached_metadata = json.load(open(metadata_file_path, 'r'))
except:
raise Exception("Error loading cached metadata")
if notebook["metadata"]["modificationTime"] > cached_metadata["modificationTime"]:
return True
else:
return False
def download_notebook(notebook, notebook_id, start_page=0, end_page=-1):
if not is_notebook_updated(notebook, notebook_id):
return True
SAVE_DIR = os.path.join(NOTEBOOK_DIR, notebook_id)
if not os.path.isdir(SAVE_DIR):
os.mkdir(SAVE_DIR)
if end_page == -1:
end_page = notebook["metadata"]["totalPages"] - 1
get_params = f"startPage={start_page}&endPage={end_page}&width={IMAGE_WIDTH}&height={IMAGE_HEIGHT}&dpi={IMAGE_DPI}"
res = requests.get(
BASE_URL + "/renderPage?" + get_params,
cookies = COOKIES,
headers = {
"User-Agent": USER_AGENT,
"X-Amzn-Karamel-Notebook-Rendering-Token": notebook["renderingToken"],
},
proxies=PROXIES,
verify=not DEBUG,
)
if res.status_code != 200:
raise Exception("Unable to open notebook")
tardata = io.BytesIO(res.content)
extracted_files = {}
with tarfile.open(fileobj=tardata) as tar:
for member in tar.getmembers():
if member.isfile(): # Skip directories
extracted_files[member.name] = tar.extractfile(member).read()
# clear out old data
listing = os.listdir(SAVE_DIR)
for item in listing:
if item.endswith(".png") or item == "metadata":
os.remove(os.path.join(SAVE_DIR, item))
image_filenames = [name for name in extracted_files.keys() if name.endswith(".png")]
for image_filename in image_filenames:
with open(os.path.join(SAVE_DIR, image_filename), "wb") as f:
f.write(extracted_files[image_filename])
json.dump(notebook["metadata"], open(os.path.join(SAVE_DIR, "metadata"), "w"))
if __name__ == "__main__":
notebooks = list_notebooks()
selection = input(">")
notebook_id = notebooks[int(selection)]["id"]
notebook = get_render_token(notebook_id)
try:
download_notebook(notebook, notebook_id, 0, -1)
except:
download_notebook(notebook, notebook_id, 0, -1)