-
Notifications
You must be signed in to change notification settings - Fork 1
/
archive.py
224 lines (203 loc) · 7.83 KB
/
archive.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
from dotenv import dotenv_values
from typing import List, Dict, Any
import requests
import sqlite3
from contextlib import closing
from time import sleep
from random import random
from datetime import datetime
import xml2epub
class Article:
def __init__(
self,
id: int,
slug: str,
url: str,
title: str = None,
subtitle: str = None,
authors: str = None,
published: str = None,
content_html: str = None,
):
self.id = id
self.slug = slug
self.url = url
self.title = title
self.subtitle = subtitle
self.authors = authors
self.published = published
self.content_html = content_html
def get_archive(
base_url: str, sid_cookie: str, limit: int = 50, offset: int = 0
) -> List[dict]:
base_url_with_slash = base_url if base_url.endswith("/") else base_url + "/"
archive_url = (
base_url_with_slash + f"api/v1/archive?sort=new&limit={limit}&offset={offset}"
)
headers = {"cookie": f"substack.sid={sid_cookie}"}
response = requests.get(archive_url, headers=headers)
try:
body = response.json()
return body
except requests.JSONDecodeError as e:
raise Exception(response.text)
def get_article_urls(base_url: str, sid_cookie: str) -> Dict[int, Article]:
articles = {}
limit = 50
offset = 0
is_last_page = False
while not is_last_page:
archive = get_archive(base_url, sid_cookie, offset=offset, limit=limit)
is_last_page = len(archive) < limit
for article in archive:
parsed_article = Article(
id=article["id"],
slug=article["slug"],
url=article["canonical_url"],
title=article["title"],
subtitle=article["subtitle"],
authors=", ".join(
[author["name"] for author in article["publishedBylines"]]
),
published=article["post_date"],
)
if parsed_article.id not in articles:
articles[parsed_article.id] = parsed_article
offset += limit
return articles
def get_article_contents(article_slug: str, base_url: str, sid_cookie: str) -> str:
base_url_with_slash = base_url if base_url.endswith("/") else base_url + "/"
article_url = base_url_with_slash + f"api/v1/posts/{article_slug}"
headers = {"cookie": f"substack.sid={sid_cookie}"}
response = requests.get(article_url, headers=headers)
try:
body_html = response.json()["body_html"]
except requests.JSONDecodeError as e:
raise Exception(response.text)
return body_html
def make_article_into_webpage(article: Article) -> str:
# published is something like "2024-01-08T11:00:19.386Z"
published = datetime.strptime(article.published, "%Y-%m-%dT%H:%M:%S.%fZ")
return f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{published.strftime("%Y-%m-%d")}: {article.title}</title>
</head>
<body>
<article>
<h1><a href="{article.url}">{article.title}</a></h1>
<h2>{article.subtitle}</h2>
<p>{article.authors} | {published.strftime("%Y-%m-%d")}</p>
<hr>
<div>{article.content_html}</div>
</article>
</body>
</html>
"""
def main():
env = dotenv_values(".env")
articles = get_article_urls(env["SUBSTACK_BASE_URL"], env["SUBSTACK_SID_COOKIE"])
print(f"Found {len(articles)} articles on {env['SUBSTACK_BASE_URL']}")
# create an sqlite database
db_location = "./parentdata.sqlite3"
with closing(sqlite3.connect(db_location)) as conn:
with closing(conn.cursor()) as c:
c.execute("PRAGMA journal_mode=WAL")
conn.commit()
# add the article schema
c.execute(
"""CREATE TABLE IF NOT EXISTS articles (
id integer PRIMARY KEY NOT NULL,
slug text NOT NULL,
url text NOT NULL,
title text ,
subtitle text,
authors text,
published text,
content_html text
)"""
)
conn.commit()
# save basic article info into sqlite database
c.executemany(
"""INSERT INTO articles
(id, slug, url, title, subtitle, authors, published)
VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO NOTHING""",
[
(
article.id,
article.slug,
article.url,
article.title,
article.subtitle,
article.authors,
article.published,
)
for article in articles.values()
],
)
conn.commit()
# get full article text
ids_of_articles_without_content = [
res[0]
for res in c.execute(
"SELECT id FROM articles WHERE content_html IS NULL"
)
]
for id in ids_of_articles_without_content:
article = articles[id]
try:
article.content_html = get_article_contents(
article.slug,
env["SUBSTACK_BASE_URL"],
env["SUBSTACK_SID_COOKIE"],
)
c.execute(
"""UPDATE articles SET content_html = ? WHERE id = ?""",
(article.content_html, article.id),
)
conn.commit()
print(f"Added content for article {article.url}")
# pause for a random amount of time to avoid rate limiting
sleep((random() + 0.5))
except Exception as e:
print(f"Failed to get content for article {article.url}: {e}")
print(
f"Added the contents of {len(ids_of_articles_without_content)} articles to the database"
)
first_article_date = c.execute(
"SELECT published FROM articles ORDER BY published ASC LIMIT 1"
).fetchone()[0]
first_article_date = datetime.strptime(
first_article_date, "%Y-%m-%dT%H:%M:%S.%fZ"
).strftime("%Y-%m-%d")
last_article_date = c.execute(
"SELECT published FROM articles ORDER BY published DESC LIMIT 1"
).fetchone()[0]
last_article_date = datetime.strptime(
last_article_date, "%Y-%m-%dT%H:%M:%S.%fZ"
).strftime("%Y-%m-%d")
newsletter_name = env["SUBSTACK_NEWSLETTER_NAME"]
book_title = f"{newsletter_name}: {first_article_date}–{last_article_date}"
book = xml2epub.Epub(
title=book_title,
creator=env["SUBSTACK_NEWSLETTER_AUTHOR"],
publisher=env["SUBSTACK_BASE_URL"],
)
for res in c.execute(
"SELECT id, slug, url, title, subtitle, authors, published, content_html FROM articles WHERE content_html IS NOT NULL ORDER BY published ASC"
):
article = Article(*res)
webpage = make_article_into_webpage(article)
chapter = xml2epub.create_chapter_from_string(
html_string=webpage, title=article.title, url=article.url
)
book.add_chapter(chapter)
print(f"Added article \"f{article.published}: {article.title}\" to ebook")
book.create_epub("./", book_title)
print(f"Created ebook './{book_title}.epub'")
if __name__ == "__main__":
main()