-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
81 lines (63 loc) · 2.06 KB
/
build.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
#!/usr/bin/python3
#
# Static web page builder tool from Reddit post data
# Author: beucismis <[email protected]>
#
from time import ctime
from json import loads
from typing import List, Dict
from datetime import datetime as dt
from markdown import markdown
from urllib3 import PoolManager
from jinja2 import Environment, FileSystemLoader
LIMIT = 100
SUBREDDIT = "kopyamakarna"
IGNORE_FLAIRS = ["META", "DUYURU"]
API_BASE_URL = "https://api.pushshift.io/reddit/search/submission"
after = 1540846800 # Subreddit created_utc
all_submissions = []
http = PoolManager()
class StaticPageGenerator:
def __init__(self):
self.env = Environment(loader=FileSystemLoader("templates"))
def render_page(self, submissions: List[Dict]):
template = self.env.get_template("post.html")
with open("index.html", "w+") as file:
file.write(
template.render(
last_build_date=ctime(), submissions=submissions
)
)
while True:
response = http.request(
"GET", API_BASE_URL, fields={
"subreddit": SUBREDDIT, "after": after, "limit": LIMIT
}
)
if response.status != 200:
break
submissions = loads(response.data.decode("utf-8"))["data"]
if not len(submissions):
break
after = submissions[-1]["created_utc"]
for submission in submissions:
if submission.get("link_flair_text", "") in IGNORE_FLAIRS:
continue
selftext = markdown(
submission.get("selftext", "")
)
if selftext.strip() in "<p>[removed]</p>":
continue
all_submissions.append(
{
"title": submission.get("title"),
"selftext": selftext,
"full_link": submission.get("full_link"),
"created": dt.fromtimestamp(
submission.get("created_utc")
).ctime(),
}
)
if __name__ == "__main__":
generator = StaticPageGenerator()
generator.render_page(all_submissions[::-1])