-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
135 lines (111 loc) · 4.06 KB
/
app.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
import datetime
from flask import Flask, render_template, make_response, url_for
from glob import glob
import markdown
import myEmail
import myFiles
from os import getenv, path, walk
from dotenv import load_dotenv
import time
load_dotenv()
staticUrl = "/static"
staticFolder = "./static"
app = Flask(__name__, static_url_path=staticUrl)
def getYears():
result = sorted(next(walk(staticFolder))[1], reverse=True)
return result
def getPosts(year):
yearFolder = path.join(staticFolder, f"{year}")
postFolders = glob(path.join(yearFolder,"*"), recursive = False)
# We want to show posts newest first.
# If we just call sorted on posts, the posts on one day will be sorted in alphabetical order.
# So we have to get the modified datetime of the folders and sort on that.
result = {}
for postFolder in postFolders:
modified = path.getmtime(postFolder)
monthDay = path.basename(path.normpath(postFolder))[:5]
key = monthDay+"_"+f"{modified:020.7f}"
result[key] = postFolder.replace(path.join(yearFolder,""), "")
result = dict(sorted(result.items(), reverse=True))
result = list(result.values())
return result
def getPost(year, post):
# These are the file types supported by most browsers:
imgTypes = [
".apng",
".avif",
".gif",
".jpg",
".jpeg",
".jfif",
".pjpeg",
".pjp",
".png",
".svg",
".webp"
]
videoTypes = [
".ogg",
".mp4",
".webm"
]
content = ""
try:
postFileName = path.join(staticFolder,str(year),post,post)
with open(postFileName) as f:
content = f.read()
except:
pass
# content="File not found '"+postFileName+"'"
content = markdown.markdown(content)
images = []
videos = []
files = myFiles.findFiles("*", where=path.join(staticFolder,str(year),post))
if files:
files = sorted(files)
for file in files:
if not file in content:
file_name, file_extension = path.splitext(file)
ext = file_extension.lower()
if ext in imgTypes:
images.append(file)
elif ext in videoTypes:
videos.append(file)
return content, images, videos
@app.route("/posts/")
@app.route("/")
def mainPage():
years = getYears()
thisYear = datetime.date.today().year
newPosts = 0
# Only show the "New Posts" blurb if it would result in the user seeing something new...
if years and int(years[0]) < thisYear:
newPosts = myEmail.newMailCount()
response = make_response(render_template('index.html', MyName=getenv("MyName"), years=years, newPosts=newPosts))
# If we didn't check for new posts above, do it now.
# processEmails is something we only want to do if there are actually new emails.
if not (years and int(years[0]) < thisYear):
newPosts = myEmail.newMailCount()
if newPosts:
myEmail.processEmails(staticUrl, staticFolder, response)
return response
@app.route("/posts/<int:year>/")
def posts(year=None):
posts = getPosts(year)
thisYear = datetime.date.today().year
newPosts = 0
# Only show the "New Posts" blurb if it would result in the user seeing something new...
if year == thisYear:
newPosts = myEmail.newMailCount()
response = make_response(render_template('year.html', MyName=getenv("MyName"),
year=year, posts=posts, newPosts=newPosts))
if newPosts:
myEmail.processEmails(staticUrl, staticFolder, response)
return response
@app.route("/posts/<int:year>/<post>/")
def onePost(year, post):
textContent, images, videos = getPost(year, post)
return render_template('post.html', MyName=getenv("MyName"),
yearUrl=url_for('posts', year=year).replace("/index.cgi", ''),
year=year, title=post, staticUrl=staticUrl,
textContent=textContent, images=images, videos=videos)