-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
70 lines (65 loc) · 2.04 KB
/
app.py
File metadata and controls
70 lines (65 loc) · 2.04 KB
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
import flask
import feedparser
import pytz
import datetime
import time
def get_posts_by_subscription(s, _start=False):
utc = pytz.utc
if not _start:
# I want only posts from "today,"
# where the day lasts until 2 AM.
homeTZ = pytz.timezone('US/Arizona')
dt = datetime.datetime.now(homeTZ)
if dt.hour < 2:
dt = dt - timedelta(hours=24)
start = dt.replace(hour=0, minute=0, second=0, microsecond=0)
start = start.astimezone(utc)
else:
start = datetime.datetime.strptime(_start, "%Y-%m-%d").replace(hour=0, minute=0, second=0, microsecond=0)
start = start.astimezone(utc)
_posts = []
f = feedparser.parse(s)
try:
blog = f['feed']['title']
except KeyError:
blog = ""
for e in f['entries']:
try:
when = e['published_parsed']
except KeyError:
when = e['updated_parsed']
when = utc.localize(datetime.datetime.fromtimestamp(time.mktime(when)))
if when > start:
title = e['title']
try:
body = storage(e['content'][0])['value']
except:
body = e['summary']
link = e['link']
_posts.append(dict(when=when, blog=blog, title=title, link=link, body=body))
return _posts
app = flask.Flask(__name__)
@app.route('/', methods=['GET','POST'])
def posts():
if flask.request.method == 'POST':
i = flask.request.get_json()
if i.get('start', False):
return flask.jsonify(data=get_posts_by_subscription(i['subscription'], i['start']))
else:
return flask.jsonify(data=get_posts_by_subscription(i['subscription']))
else:
resp = flask.Response("""
Usage
~~~~~
curl -X POST \\
http://rssapi.pancubs.org/ \\
-H 'Content-Type: application/json' \\
-d '{
"subscription": "https://www.stabroeknews.com/feed",
"start": "2019-11-29"
}'
""")
resp.headers['content-type'] = 'text/plain'
return resp
if __name__ == '__main__':
app.run()