forked from hnrss/hnrss
/
hnrss.py
executable file
·130 lines (110 loc) · 3.78 KB
/
hnrss.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
from api import API
from flask import Flask, request, redirect
from rss import RSS
from urllib import urlencode
app = Flask(__name__)
@app.route('/newest')
def newest():
query = request.args.get('q')
api = API.using_request(request)
if query:
rss_title = 'Hacker News: "%s"' % query
else:
rss_title = 'Hacker News: Newest'
rss = RSS(api.newest(), rss_title, 'https://news.ycombinator.com/newest')
return rss.response()
@app.route('/newcomments')
def new_comments():
query = request.args.get('q')
api = API.using_request(request)
if query:
rss_title = 'Hacker News: "%s" comments' % query
else:
rss_title = 'Hacker News: New Comments'
rss = RSS(api.comments(), rss_title, 'https://news.ycombinator.com/newcomments')
return rss.response()
@app.route('/ask')
def ask():
api = API.using_request(request)
rss = RSS(api.ask_hn(), 'Hacker News: Ask HN', 'https://news.ycombinator.com/ask')
return rss.response()
@app.route('/show')
def show():
api = API.using_request(request)
rss = RSS(api.show_hn(), 'Hacker News: Show HN')
return rss.response()
@app.route('/polls')
def polls():
api = API.using_request(request)
rss = RSS(api.polls(), 'Hacker News: Polls')
return rss.response()
@app.route('/item')
def story_comments():
story_id = request.args.get('id')
api = API.using_request(request)
api_response = api.comments(story_id=story_id)
if api_response['hits']:
story = api_response['hits'][0]
rss_title = 'Hacker News: New Comments on "%s"' % story['story_title']
else:
rss_title = 'Hacker News: New Comments'
rss_link = 'https://news.ycombinator.com/item?id=%s' % story_id
rss = RSS(api_response, rss_title, rss_link)
return rss.response()
@app.route('/user')
def user():
username = request.args.get('id')
api = API.using_request(request)
api_response = api.user(username)
rss_title = 'Hacker News: %s RSS feed' % username
rss_link = 'https://news.ycombinator.com/user?id=%s' % username
rss = RSS(api_response, rss_title, rss_link)
return rss.response()
@app.route('/submitted')
def user_submitted():
username = request.args.get('id')
api = API.using_request(request)
api_response = api.user(username, 'submitted')
rss_title = 'Hacker News: %s submitted RSS feed' % username
rss_link = 'https://news.ycombinator.com/submitted?id=%s' % username
rss = RSS(api_response, rss_title, rss_link)
return rss.response()
@app.route('/threads')
def user_threads():
username = request.args.get('id')
api = API.using_request(request)
api_response = api.user(username, 'threads')
rss_title = 'Hacker News: %s threads RSS feed' % username
rss_link = 'https://news.ycombinator.com/threads?id=%s' % username
rss = RSS(api_response, rss_title, rss_link)
return rss.response()
# Redirect the old RSS endpoints to the new ones. Keep the query
# string intact when doing so.
@app.route('/feeds/<location>')
def feeds_redirects(location):
qs = '?' + urlencode(request.args)
redirect_map = {
'firehose.xml' : '/newest',
'comments.xml' : '/newcomments',
'askhn.xml' : '/ask',
'showhn.xml' : '/show',
'polls.xml' : '/polls',
'search.xml' : '/newest',
}
if location in redirect_map:
return redirect(redirect_map[location] + qs)
else:
return redirect('/newest')
@app.route('/feeds/author/<author>.xml')
def author_redirect(author):
args = request.args.copy()
args['id'] = author
qs = '?' + urlencode(args)
return redirect('/user' + qs)
@app.route('/feeds/')
@app.route('/')
def index():
return redirect('https://edavis.github.io/hnrss/')
if __name__ == '__main__':
app.run(debug=True)