-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate.py
62 lines (40 loc) · 1.57 KB
/
generate.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
#!/usr/bin/python3
# SPDX-License-Identifier: MIT
from datetime import datetime
from jinja2 import Environment, FileSystemLoader
import sqlite3
import time
MAX_ENTRIES_PER_FEED = 8
def filter_date(value, format='%d %B %Y'):
return time.strftime(format, time.strptime(value, '%Y-%m-%d %H:%M:%S'))
conn = sqlite3.connect('feeds.db')
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute('''SELECT name, url, etag, modified, id, blog_url
FROM feeds
ORDER BY name''')
feeds = c.fetchall()
c.execute('''SELECT f.name, f.blog_url, f.title as blog_title,
p.feed_id, p.title, p.url, p.post, p.published_date,
p.author
FROM posts p, feeds f
WHERE f.id = p.feed_id
ORDER by p.published_date DESC
LIMIT 20''')
posts = c.fetchall()
conn.close()
file_loader = FileSystemLoader('templates')
env = Environment(loader=file_loader)
env.filters['date'] = filter_date
template = env.get_template('index.html.j2')
output = template.render(generate_time=datetime.strftime(datetime.utcnow(),
"%d %B %Y %H:%M"),
posts=posts, feeds=feeds)
with open('index.html', 'w') as html:
html.write(output)
template = env.get_template('rss20.xml.j2')
output = template.render(generate_time=datetime.strftime(datetime.utcnow(),
"%d %B %Y %H:%M"),
posts=posts, feeds=feeds)
with open('rss20.xml', 'w') as rss:
rss.write(output)