-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
73 lines (61 loc) · 1.85 KB
/
main.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
# coding: utf-8
from flask import Flask, redirect, render_template, abort, Blueprint
from cache import cached
from config import USER_ID, CLIENT_SECRET, SERVER_ROOT
from posts import processPost
from storage import Storage
from fetcher import Fetcher
import re
import signal
app_main = Blueprint('blog', __name__, static_folder='static')
storage = Storage()
fetcher = Fetcher(USER_ID, CLIENT_SECRET, storage)
@app_main.route('/')
@cached('main')
def main():
posts = list(storage.getLatestPosts())
for post in posts:
processPost(post)
fetcher.maybe_post_fetch()
return render_template(
'main.html', posts=posts, archive_items=storage.getDates())
@app_main.route('/post/<activity_id>')
def get_post(activity_id):
post = storage.getPost(activity_id)
processPost(post)
fetcher.maybe_single_fetch(activity_id)
return render_template(
'single_entry.html', post=post, archive_items=storage.getDates())
@app_main.route('/archive/<datespec>')
def archive(datespec):
if not re.match(r'\d+-\d+', datespec):
abort(404)
posts = list(storage.getArchivedPosts(datespec))
if len(posts) == 0:
abort(404)
for post in posts:
processPost(post)
return render_template(
'main.html', posts=posts, archive_items=storage.getDates())
@app_main.route('/feed')
@cached('feed')
def atomFeed():
posts = list(storage.getLatestPosts())
global_updated = max([post['updated'] for post in posts])
for post in posts:
processPost(post)
return render_template(
'atom.xml', posts=posts, SERVER_ROOT=SERVER_ROOT,
global_updated=global_updated)
@app_main.route('/forcefetch')
def forcefetch():
fetcher.post_fetch()
return redirect('/')
if __name__ == '__main__':
try:
fetcher.fetch_all_posts()
app = Flask(__name__)
app.register_blueprint(app_main, url_prefix='/blog')
app.run(debug=True)
finally:
fetcher.finish_thread()