This repository has been archived by the owner on May 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.py
executable file
·113 lines (88 loc) · 3.02 KB
/
index.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
#!/usr/bin/env python2
import feedparser
import pickle
import time
from flask import Flask
from flask import render_template, abort
MAX_PKGS = 8
app = Flask(__name__)
app.config['PROPAGATE_EXCEPTIONS'] = True
def format_pkg_name(pkg):
max_length = 24
name, ver = pkg.split(' ')[:2]
ver, rel = ver.split('-')
full_name = '{0} {1}-{2}'.format(name, ver, rel)
if len(full_name) <= max_length:
return (full_name, full_name)
short_name = '{0} {1}'.format(name, ver)
if len(short_name) <= max_length:
return (full_name, short_name)
short_name = name
if len(short_name) <= max_length:
return (full_name, short_name)
return (full_name, short_name[:max_length])
@app.route('/feed/<name>')
def fetch_feed(name):
if name == 'news':
rss_feed = 'https://www.archlinux.org/feeds/news/'
cache_file = './cache/news'
elif name == 'pkgs_x64':
rss_feed = 'https://www.archlinux.org/feeds/packages/x86_64/'
cache_file = './cache/pkgs_x64'
elif name == 'pkgs_x86':
rss_feed = 'https://www.archlinux.org/feeds/packages/i686/'
cache_file = './cache/pkgs_x86'
else:
abort(404)
def write_feed(cache_file, feed):
data = (time.time(), feed)
pickle.dump(data, open(cache_file, "wb"))
return
expire_cache = 3600
mtime = None
try:
cache = pickle.load(open(cache_file, 'rb'))
except (IOError, KeyError) as e:
# nothing to do, this is expecte
pass
else:
mtime, cached_feed = cache
# we must have a valid cache file
if mtime:
now = time.time()
diff = int(now - mtime)
# if cache file is stale
if diff > expire_cache:
feed = feedparser.parse(rss_feed, etag=cached_feed.etag)
# feed has not been modified, update mtime so we don't check again
if feed.status == 304:
feed = cached_feed
# write updated mtime to pickle file
write_feed(cache_file, feed)
# feed has been modified, lets update our cache
else:
# write new data pickle file
write_feed(cache_file, feed)
# cache is not stale, use cache
else:
feed = cached_feed
# no cache file, that is okay
else:
feed = feedparser.parse(rss_feed)
# write data to pickle file
write_feed(cache_file, feed)
if name == 'news':
items = [(x.title, x.link) for x in feed.entries]
return render_template('news.html', news=items)
elif 'pkgs' in name:
pkgs = []
for x in feed.entries[:MAX_PKGS]:
full, short = format_pkg_name(x.title)
pkgs.append({'name':full, 'short_name':short, 'cat':x.category, 'link':x.link, 'summary':x.summary})
return render_template('pkgs.html', pkgs=pkgs)
return 'None'
@app.route('/')
def index():
return render_template("index.html")
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)