This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
91 lines (76 loc) · 2.08 KB
/
app.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
82
83
84
85
86
87
88
89
90
91
from flask import Flask, request, abort, make_response
import yaml
from feedgen.feed import FeedGenerator
from feed import Feed
import pytz
from os import environ
_generator = {
'generator': 'python-web-feed',
'version': '0.1',
'uri': 'https://github.com/DavisGoglin/python-web-feed',
}
app = Flask(__name__)
config_file = environ.get('PY_WEB_FEED_CONFIG', 'example.yaml')
with open(config_file, 'r') as f:
config = yaml.load(f, Loader=yaml.BaseLoader)
timezone = pytz.timezone(config['timezone'])
def _round_date(dt, rounding):
if rounding == 'day':
return dt.replace(hour=0, minute=0, second=0, microsecond=0)
elif rounding == 'hour':
return dt.replace(minute=0, second=0, microsecond=0)
elif rounding == 'none' or rounding is None:
return dt
else:
raise Exception
@app.route('/')
def hello():
return 'Hello, World!'
@app.route('/<feed_name>')
def display_feed(feed_name):
if feed_name not in config['feeds']:
abort(404)
f = Feed(config['feeds'][feed_name])
f.load()
f.parse()
fg = FeedGenerator()
fg.generator(**_generator)
fg.id(
request.base_url
)
fg.link(
href=request.base_url,
rel='self',
)
fg.title(
f.properties.get('title', feed_name)
)
fg.author(
name=f.properties.get('author', '')
)
fg.updated(
timezone.localize(
_round_date(
max(
[e['updated'] for e in f.entries]
), config.get('date_rounding', None)
)
)
)
for entry in f.entries:
fe = fg.add_entry()
fe.id(entry['url'])
fe.title(entry['title'])
fe.link(href=entry['url'])
fe.updated(
timezone.localize(
_round_date(
entry['updated'], config.get('date_rounding', None)
)
)
)
fe.content(entry['content'])
atomfeed = fg.atom_str()
resp = make_response(atomfeed)
resp.headers['content-type'] = 'application/xml'
return resp