This repository has been archived by the owner on Jan 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
64 lines (47 loc) · 1.8 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
import json
from urllib import request
from flask import Flask
from feedgen.feed import FeedGenerator
app = Flask(__name__)
class Api:
base_url = 'http://zhuanlan.zhihu.com'
base_api_url = base_url + '/api/columns'
def __init__(self, column_id):
self.column_id = column_id
self.info = self.base_api_url + '/%s' % self.column_id
self.posts = self.base_api_url + '/%s/posts?limit=10' % self.column_id
@app.route('/favicon.ico')
def favicon():
return '', 404
@app.route('/<string:column_id>', strict_slashes=False)
def feed(column_id):
api = Api(column_id)
with request.urlopen(api.info) as stream:
result = stream.read().decode('utf-8')
if not result:
return '', 404
info = json.loads(result)
with request.urlopen(api.posts) as stream:
result = stream.read().decode('utf-8')
entries = json.loads(result)
fg = FeedGenerator()
fg.id(str(entries[0]['slug']))
fg.title(info['name'])
fg.language('zh_CN')
fg.icon(info['avatar']['template'].replace('{id}', info['avatar']['id']).replace('{size}', 's'))
fg.logo(info['avatar']['template'].replace('{id}', info['avatar']['id']).replace('{size}', 'l'))
fg.description(info['intro'])
fg.author(dict(name=info['creator']['name']))
fg.link(href=api.base_url + info['url'], rel='alternate')
for entry in entries:
fe = fg.add_entry()
fe.id(entry['url'])
fe.title(entry['title'])
fe.published(entry['publishedTime'])
fe.updated(entry['publishedTime'])
fe.author(dict(name=entry['author']['name']))
fe.link(href=api.base_url + entry['url'], rel='alternate')
fe.content(entry['content'])
return fg.atom_str(pretty=True)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=False)