-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
93 lines (75 loc) · 2.32 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
89
90
91
92
93
from flask import Flask, render_template, abort
import grammar as gr
import random
import time
import os
app = Flask(__name__)
def uniqid():
return hex(int(time.time() * 10000000))[2:]
def clean_poem(poetry):
poetry = poetry.strip()
poetry = poetry.replace("\n\n\n", "\n\n")
# remove some common duplicates
poetry = poetry.replace("一样一样", "一样")
poetry = poetry.replace("了了", "了")
lines = poetry.split("\n")
trimmed_lines = []
for line in lines:
if line == "\n" or len(line) <= 20:
trimmed_lines.append(line)
else: # line too long, probably doesn't make sense
continue
# line_length = len(line)
# first_len = random.randint(line_length // 4, 3 * line_length // 4)
# trimmed_lines.append(line[:first_len])
# trimmed_lines.append(line[first_len:])
poetry = "\n".join(trimmed_lines)
if poetry[-1] in [","]: # ends with non-terminating punctuation
terminating = ["。", "!", "?", ""]
poetry = poetry[:-1] + random.choice(terminating)
return poetry
@app.route('/')
def home():
poetry = clean_poem(gr.top())
poetry_html = poetry.replace("\n", "<br>")
poemid = uniqid()
# write to file
os.makedirs("poems", exist_ok=True)
with open(os.path.join("poems", poemid + ".txt"), "w", encoding="utf-8") as f:
f.write(poetry)
return render_template(
'home.html',
poetry=poetry_html,
poem_id=poemid,
single=False,
)
@app.route('/poem/<poemid>')
def get_poem(poemid):
filename = os.path.join("poems", poemid + ".txt")
try:
with open(filename, "r", encoding="utf-8") as f:
poetry = f.read()
except FileNotFoundError:
abort(404)
poetry_html = poetry.replace("\n", "<br>")
return render_template(
"home.html",
poetry=poetry_html,
poem_id=poemid,
single=True,
)
@app.route('/poems')
def list_poems():
poemids = []
for file in os.listdir("poems"):
poemid = file.rstrip(".txt")
poemids.append(poemid)
return render_template(
"list.html",
poem_ids=poemids
)
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
if __name__ == "__main__":
app.run(debug=True)