-
Notifications
You must be signed in to change notification settings - Fork 3
/
taiseipage.py
executable file
·183 lines (129 loc) · 3.95 KB
/
taiseipage.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python3
from flask import (
Flask,
Response,
redirect,
render_template,
request,
url_for,
)
from markupsafe import Markup
from urllib.parse import (
urljoin,
)
from feedgen.feed import (
FeedGenerator,
)
import datetime
import markdown
import os
app = Flask(__name__)
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
news_dir = 'news'
screens_dir = 'static/screenshots'
def load_news_file(filename):
f = open(os.path.join(news_dir, filename), 'r')
date = f.readline().strip('\n')
title = f.readline().strip('\n')
content = f.read()
content = Markup(markdown.markdown(content))
f.close()
return (date, title, content, filename)
def load_news():
news = []
l = os.listdir(news_dir)
for fn in l:
news.append(load_news_file(fn))
news.sort(key=lambda x: x[0])
news.reverse()
return news
def load_screendirs():
dirs = []
for fn in os.listdir(screens_dir):
if fn == 'thumb':
continue
num, caption = fn.split("_")
screens = os.listdir(os.path.join(screens_dir, fn))
dirs.append((int(num), caption, fn, screens))
dirs.sort(key=lambda x: x[0])
dirs.reverse()
return dirs
def make_external(url):
return urljoin(request.url_root, url)
def render_template_page(page, *args, **kwargs):
return render_template(page + '.html', *args, page=page, **kwargs)
@app.route('/')
def index():
return render_template_page('home')
@app.route('/news')
def news():
return render_template_page('news', news=load_news())
@app.route('/news.atom')
def newsfeed():
def makedate(strdate):
dt = datetime.datetime.strptime(strdate, '%Y-%m-%d %H:%M')
return datetime.datetime(
year=dt.year,
month=dt.month,
day=dt.day,
hour=dt.hour,
minute=dt.minute,
tzinfo=datetime.timezone.utc,
)
news = load_news()
feed = FeedGenerator()
feed.icon(make_external('static/favicon.ico'))
feed.id(request.url)
feed.language('en-US')
feed.link(href=make_external('news'))
feed.link(href=request.url, rel='self')
feed.title('The State of Taisei')
feed.updated(makedate(news[0][0]))
for article in news:
date = makedate(article[0])
url = make_external("/news/" + article[3])
entry = feed.add_entry()
entry.author(name='Taisei team')
entry.content(article[2], type='html')
entry.id(url)
entry.link(href=make_external("/news/" + article[3]))
entry.published(date)
entry.title(article[1])
entry.updated(date)
return Response(feed.atom_str(), mimetype='text/xml')
@app.route('/news/<filename>')
def news_entry(filename):
news = load_news_file(filename)
return render_template_page('news_entry', content=news)
@app.route('/media')
def media():
dirs = load_screendirs()
return render_template_page('media', dirs=dirs)
@app.route('/download')
def download(filename=None):
return render_template_page('download')
@app.route('/play')
def play():
return redirect("https://play.taisei-project.org/", code=301)
@app.route('/discord')
def discord():
return redirect("https://discord.gg/JEHCMzW", code=302)
@app.route('/code')
def code():
return redirect("https://github.com/taisei-project/taisei", code=301)
@app.route('/license')
def license():
return render_template_page('license')
@app.route('/.well-known/org.flathub.VerifiedApps.txt')
def flathub_token():
return Response("4cbf22b7-6cc8-4cc8-b12f-d60b3df9d05b", mimetype='text/plain')
@app.template_filter('indent')
def indent_filter(s, width, *args):
# incomplete reimplementation of the default 'indent' filter
# because apparently it doesn't work with 'safe' correctly
first, *rest = s.split('\n')
return ('\n').join([first] + [' ' * width + s for s in rest])
if __name__ == "__main__":
app.debug = True
app.run()