-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate_rss.py
executable file
·61 lines (47 loc) · 1.84 KB
/
aggregate_rss.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
#!/usr/bin/python3
import feedparser
import html
def get_title(entry):
if 'letterboxd_filmtitle' in entry:
return f"Review: {html.escape(entry['letterboxd_filmtitle'])}"
return html.escape(entry['title'])
def get_description(entry):
return html.escape(entry['title']) if '<p>' in entry['description'] else entry['description']
def stringify_entry(entry):
return f"""
<item>
<title>{get_title(entry)}</title>
<description>{get_description(entry)}</description>
<link>{entry['link']}</link>
<guid>{entry['link']}</guid>
<pubDate>{entry['published']}</pubDate>
</item>
"""
base = feedparser.parse('html/rss.xml')
lxd = feedparser.parse('https://letterboxd.com/purkka/rss', agent='purkkafi-rss-bot')
entries = []
entries.extend(base['entries'])
entries.extend(lxd['entries'])
entries.sort(key=lambda e: e['published_parsed'], reverse=True)
entries = entries[:20]
entries_string = ''.join(stringify_entry(e) for e in entries)
with open('html/rss.xml', 'w') as f:
f.write(f"""<?xml version="1.0"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>{base['feed']['title']}</title>
<link>{base['feed']['link']}</link>
<description>{base['feed']['description']}</description>
<atom:link href="https://purkka.fi/rss.xml" rel="self" type="application/rss+xml" />
{entries_string}
</channel>
</rss>
""")
def get_date(entry):
date = entry['published_parsed']
return f'{date.tm_mday}.{date.tm_mon}.{date.tm_year}'
def beagicify_entry(entry):
return f"\\external{{{get_title(entry)}}}{{{entry['link']}}}{{{get_date(entry)}}}{{{entry['published']}}}{{{get_description(entry)}}}"
new_content = '\n'.join(beagicify_entry(e) for e in entries)
with open('beagic/new_content.bgc', 'w') as f:
f.write(new_content)