aboutsummaryrefslogtreecommitdiff
path: root/rss.py
diff options
context:
space:
mode:
Diffstat (limited to 'rss.py')
-rw-r--r--rss.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/rss.py b/rss.py
new file mode 100644
index 0000000..fdf4b53
--- /dev/null
+++ b/rss.py
@@ -0,0 +1,64 @@
1import os
2import jinja2
3
4import glob
5from bs4 import BeautifulSoup
6
7
8class FilePathLoader(jinja2.BaseLoader):
9 def __init__(self, cwd):
10 self.cwd = cwd
11
12 def get_source(self, environment, template):
13 filename = os.path.join(self.cwd, template)
14
15 try:
16 with open(filename, 'r') as f:
17 contents = f.read()
18 except IOError:
19 raise jinja2.TemplateNotFound(filename)
20
21 return contents, filename, lambda: False
22
23
24def render_template(cwd, template_path, context):
25 env = jinja2.Environment(loader=FilePathLoader(cwd))
26 return env.get_template(template_path).render(context)
27
28
29def main():
30
31 filenames = glob.glob("./_build/html/_posts/*/*/*.html")
32 print(filenames)
33
34 posts = []
35
36 for p in filenames:
37 soup = BeautifulSoup(open(p), "html5lib")
38 body = soup.find_all("div", class_="body")[0].text
39
40 posts.append({
41 "title": soup.title.string,
42 "body": body,
43 "date_rss": body[body.find("Publish Date:")+13:body.find("Publish Date:")+23],
44 "permalink": "/".join(p.split("/")[3:])
45 })
46
47 context = {
48 "site": {
49 "name": "Hello World",
50 "url": "https://blog.jinwei.me",
51 "tagline": "Freedom is my birth right and I shall have it."
52 },
53 "posts": posts
54 }
55
56 return render_template(
57 ".",
58 "all.rss.xml",
59 context
60 )
61
62
63if __name__ == '__main__':
64 print(main())
Powered by cgit v1.2.3 (git 2.41.0)