-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
88 lines (67 loc) · 2.06 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os, json, markdown, wikipedia
from flask import Flask, render_template, redirect, url_for
from random import sample
app = Flask('app')
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
# JINJA FUNCTIONS
@app.template_filter('md')
def md(text):
return markdown.markdown(text)
@app.template_filter('wiki2keywords')
def wiki(text):
return ", ".join(wikipedia.search(text))
# FETCH DATA
def getItems(path):
file = open(path)
data = file.read()
items = json.loads(data)
return items
CONTENT = 'comics.json'
JSON_PATH = os.path.join(app.static_folder, CONTENT)
ITEMS = getItems(JSON_PATH)
# 404
@app.errorhandler(404)
def page_not_found(error):
return render_template('404.jinja'), 404
# QR Gag
@app.route('/qr-comic/panel01.png')
def qr_01():
return redirect("/static/assets/panel01.png", code=302)
@app.route('/qr-comic/panel02.png')
def qr_02():
return redirect("/static/assets/panel02.png", code=302)
@app.route('/qr-comic/panel03.png')
def qr_03():
return redirect("/static/assets/panel03.png", code=302)
# HOME
@app.route('/')
def home():
# GET ANY 13 BEST-OFS. HERO WILL BE THE FIRST
items = sample([item for item in ITEMS if 'bestof' in item], 13)
return render_template('home.jinja', hero=items[0], items=items[1:13])
# ALL COMICS SHUFFLED
@app.route('/comics/')
def archives():
return render_template('archives.jinja', items=sample(ITEMS, len(ITEMS)) )
# RANDOMIZER URL
@app.route('/random/')
def random():
item=sample(ITEMS,1)[0]
return redirect('/comics/' + item['slug'] + '/')
# DETAIL PAGE
@app.route('/comics/<slug>/')
def detail(slug):
matches = [(index, row) for index, row in enumerate(ITEMS) if row['slug'] == slug]
if len(matches) == 0:
return render_template('404.jinja'), 404
idx, item = matches[0]
if idx < len(ITEMS) - 1:
next = ITEMS[idx + 1]
else:
next = ITEMS[0]
return render_template('detail.jinja', item=item, next=next)
# ABOUT
@app.route('/about/')
def about():
return render_template('about.jinja')