-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedreverser.py
73 lines (65 loc) · 2.27 KB
/
feedreverser.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
#!/usr/bin/env python3
import os
import sys
import yaml
from dateutil import parser
import feedparser
from datetime import datetime, timezone
from feedgen.feed import FeedGenerator
from bs4 import BeautifulSoup
def main():
config_file = get_config_file()
if not os.path.isfile(config_file):
setup(config_file)
config = read_config(config_file)
feed = feedparser.parse(config['feed_url'])
consumed = config['consumed']
fg = FeedGenerator();
fg.title('Reversed feed')
fg.link(href='http://localhost/reversed', rel='alternate')
fg.description('Snudd feed')
for entry in reversed(feed.entries):
if entry.id not in consumed:
fe = fg.add_entry()
fe.id(entry.id)
titleSoup = BeautifulSoup(entry.title, features="lxml")
fe.title(titleSoup.get_text())
summarySoup = BeautifulSoup(entry.summary, features="lxml")
fe.summary(summarySoup.get_text())
for tag in entry.tags:
if 'uncategorized' != tag.term.lower():
fe.category(term = tag.term.replace(' ', '_').replace('.', ''))
now = datetime.now(tz=timezone.utc)
fe.pubDate(now)
fe.updated(now)
consumed.append(entry.id)
break
fg.rss_file(config['reversed_file'])
save_config(config, config_file)
def get_config_file():
if __name__ == "__main__" and len(sys.argv) > 1:
config_file = sys.argv[1]
else:
config_file = os.path.join(os.path.expanduser("~"), ".feedreverser")
return config_file
def save_config(config, config_file):
copy = dict(config)
copy['updated'] = datetime.now(tz=timezone.utc).isoformat()
with open(config_file, 'w') as fh:
fh.write(yaml.dump(copy, default_flow_style=False))
def read_config(config_file):
config = {}
with open(config_file) as fh:
config = yaml.load(fh, Loader=yaml.FullLoader)
return config
def setup(config_file):
feed_url = input('RSS/Atom feed URL to watch: ')
reversed_file = input('Path to file to save the reversed feed: ')
config = {
'feed_url': feed_url,
'reversed_file': reversed_file,
'consumed': [],
}
save_config(config, config_file)
if __name__ == "__main__":
main()