-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsstack.py
301 lines (273 loc) · 11.8 KB
/
sstack.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
import requests
import pickle
import os
import urllib.parse
import json
import time
import subprocess
from playwright.sync_api import sync_playwright
login_failures = 0
login_successes = 0
class Substack:
def __init__(self, cookie_file=None, login_url=None):
self.s = requests.Session()
self.cookie_file = cookie_file
if login_url:
print('Using Substack login_url')
self.login(login_url)
else:
print(f'Using existing substack cookie file {cookie_file=}')
self.read_cookies()
def write_cookies(self):
if not self.cookie_file:
return
with open(self.cookie_file, 'wb') as f:
pickle.dump(self.s.cookies, f)
def read_cookies(self):
if not self.cookie_file:
return
if not os.path.exists(self.cookie_file):
return
with open(self.cookie_file, 'rb') as f:
self.s.cookies.update(pickle.load(f))
def login(self, login_url):
r = self.s.get(login_url, allow_redirects=True)
print(r.status_code, r)
self.write_cookies()
def get_posts(self, inbox_type='inbox', limit=12, after=None): # max limit enforced by substack: 20
url = f'https://substack.com/api/v1/reader/posts?inboxType={inbox_type}&limit={limit}'
if after:
url += f'&after={after}'
r = self.s.get(url)
if r.status_code//100 != 2:
raise RuntimeError(f'{r.status_code}: {r.text}')
return r.json()
def get_archive(self, domain, limit=12, offset=None): # max limit enforced by substack: 20
url = f'https://{domain}/api/v1/archive?sort=new&search=&limit={limit}'
if offset:
url += f'&offset={offset}'
r = self.s.get(url)
if r.status_code == 429:
time.sleep(5)
if r.status_code//100 != 2:
raise RuntimeError(f'{r.status_code}: {r.text}')
return r.json()
def get_full_archive(self, domain):
out = []
offset = None
while True:
ret = self.get_archive(domain, limit=12, offset=offset)
print(f'get_archive({offset=})')
if not ret:
return out
out += ret
if offset:
offset += 12
else:
offset = 12
time.sleep(1)
def get_subscriptions(self):
r = self.s.get(f'https://substack.com/api/v1/subscriptions')
if r.status_code//100 != 2:
raise RuntimeError(f'{r.status_code}: {r.text}')
return r.json()
def playwright_cookies(self):
return [{'name': k.name, 'value': k.value, 'port': k.port, 'domain': k.domain, 'path': k.path, 'secure': k.secure, 'expires': k.expires} for k in self.s.cookies]
relogin_command_run = False
def download_pdf(self, *args, **kwargs):
global login_successes
global login_failures
for i in range(3):
try:
ret = self._download_pdf(*args, retry=i, **kwargs)
if ret:
login_successes += 1
print(f'STATUS {login_failures=} {login_successes=}')
return ret
except Exception as e:
print('download_pdf call', i+1, 'swallowed exception', e)
print('Retrying download_pdf()')
ret = self._download_pdf(*args, retry=3, **kwargs)
if not ret:
login_failures += 1
if kwargs.get('relogin_command') and not self.relogin_command_run and login_successes == 0:
print(f'STATUS {login_failures=} {login_successes=}')
subprocess.run(['/bin/bash', '-c', kwargs.get('relogin_command')])
self.relogin_command_run = True
else:
login_successes += 1
print(f'STATUS {login_failures=} {login_successes=}')
return ret
def _download_pdf(self, url, output_file, headless=True, relogin_command=None, retry=0):
print('Opening playwright:', url)
with sync_playwright() as p:
chromium = p.chromium
browser = chromium.launch(headless=headless)
context = browser.new_context()
context.add_cookies(self.playwright_cookies())
page = context.new_page()
print('Opening https://substack.com/home')
page.goto('https://substack.com/home')
page.wait_for_load_state()
page.wait_for_timeout(5000)
print('Opened https://substack.com/home')
try:
page.locator('svg.lucide-plus').wait_for(timeout=1000)
except Exception as e:
print('Unable to ensure logged-in on substack homepage (no lucide-plus icon), you need to relogin', e)
return None
print('Found logged-in session on substack.com')
page.goto(url)
try:
page.wait_for_load_state(timeout=5000)
except:
print('load state ignored')
print('Ensuring logged-in session carries to article details')
try:
page.locator('svg.lucide-bell').wait_for(timeout=2000)
except Exception as e:
print('try 1: unable to ensure logged-in to', url, '\n - error:', e)
try:
page.locator('a[href*="sign-in"]').first.click()
except:
page.locator('[data-href*="sign-in"]').first.click()
try:
page.wait_for_load_state(timeout=5000)
except:
print('load state ignored')
page.wait_for_timeout(1000)
print('Opening https://substack.com/home again')
page.goto('https://substack.com/home')
try:
page.wait_for_load_state(timeout=5000)
except:
print('load state ignored')
page.wait_for_timeout(2000)
print("Reloading page after signin carryover")
page.goto(url)
try:
page.wait_for_load_state(timeout=5000)
except:
print('load state ignored')
page.wait_for_timeout(1000)
print("Looking for login session")
try:
page.locator('svg.lucide-bell').wait_for(timeout=2000)
print("Logged in!")
except Exception as e:
print('try 2: unable to ensure logged-in to', url, '\n - error:', e)
try:
page.locator('a[href*="sign-in"]').first.click()
except:
page.locator('[data-href*="sign-in"]').first.click()
try:
page.wait_for_load_state(timeout=5000)
except:
print('load state ignored')
page.wait_for_timeout(2000)
print('Opening https://substack.com/home again')
page.goto('https://substack.com/home')
try:
page.wait_for_load_state(timeout=5000)
except:
print('load state ignored')
page.wait_for_timeout(2000)
print("Reloading original page after signin carryover")
page.goto(url)
try:
page.wait_for_load_state(timeout=5000)
except:
print('load state ignored')
page.wait_for_timeout(2000)
print("Looking for login session")
try:
page.locator('svg.lucide-bell').wait_for(timeout=3000)
print("Logged in!")
except Exception as e2:
print('TIMED OUT: unable to ensure logged-in to', url, '\n - error:', e2)
return None
page.wait_for_timeout(1000)
page.emulate_media(media="print")
page.wait_for_timeout(1000)
page.add_style_tag(content='''
@page {
size: A4;
margin: 20mm !important;
}
@media all {
@page {
size: A4;
margin: 20mm !important;
}
article {
margin: 0 20mm !important;
}
div.pencraft {
display: none !important;
}
html, body {
width: 210mm;
height: 297mm;
}
}
''')
page.wait_for_timeout(1000)
print("Starting scroll...")
lastScrollY = -1000
curScrollY = page.evaluate('(document.scrollingElement || document.body).scrollTop')
scrolled = 0
while curScrollY > lastScrollY:
N = 250
page.mouse.wheel(0, N)
scrolled += N
page.wait_for_timeout(50)
lastScrollY = curScrollY
curScrollY = page.evaluate('(document.scrollingElement || document.body).scrollTop')
print("Resetting to top")
page.mouse.wheel(0, -1 * scrolled)
page.wait_for_timeout(1000)
print("Done scrolling")
page.pdf(path=output_file, prefer_css_page_size=True)
browser.close()
return True
if __name__ == '__main__':
import argparse
a = argparse.ArgumentParser(description="Writes recent Substack articles to reMarkable cloud")
a.add_argument('--download-url', help='URL to download PDF for')
a.add_argument('--download-domain', help='Substack domain to download all PDFs for')
a.add_argument('--config-folder', help='Configuration folder for remarkable-substack', default='')
a.add_argument('--substack-login-url', help='For initial authentication with Substack: the URL from the email received from Substack when entering your email on the login page')
a.add_argument('--non-headless', help='Debug by not having headless browser', action='store_true')
a.add_argument('--output-folder', help='Output folder', default='out')
a.add_argument('--relogin-command', help='Command to run when relogin is required (e.g. send a notification)', default=None)
args = a.parse_args()
if not args.config_folder:
args.config_folder = os.path.join(os.path.expanduser('~'), '.config', 'remarkable-substack')
if not os.path.exists(args.config_folder):
os.makedirs(args.config_folder)
print(f'Set --config-folder to {args.config_folder}')
cookie_file = os.path.join(args.config_folder, '.substack-cookie')
ss = Substack(cookie_file=cookie_file, login_url=args.substack_login_url)
if args.download_url:
path = f'{args.output_folder}/article.pdf'
print(f'Downloading {args.download_url=} {path=}')
ret = ss.download_pdf(args.download_url, path, headless=not args.non_headless)
print(f'Result: {ret}')
if args.download_domain:
archive = ss.get_full_archive(args.download_domain)
with open(f'{args.output_folder}/{args.download_domain}.json','w') as f:
f.write(json.dumps(archive, indent=4))
print(f'{len(archive)=}')
root = f'{args.output_folder}/{args.download_domain}'
if not os.path.exists(root):
os.makedirs(root, exist_ok=True)
for item in archive:
date = item['post_date'].split('T')[0]
title = item['title'].replace('/','-')
path = os.path.join(root, f'{date} - {title}.pdf')
if os.path.exists(path):
print(f'File {path=} already exists, skipping')
continue
print(f'Downloading {date=} {title=} {path=}')
ret = ss.download_pdf(item['canonical_url'], path, headless=not args.non_headless, relogin_command=args.relogin_command)
print(f'Result: {ret}')