-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
81 lines (58 loc) · 2.32 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
import os
import re
from datetime import datetime
# from urllib.parse import urlparse
from flask import Flask, render_template, redirect, send_from_directory, request, make_response
import config
from utils import get_exif_data
app = Flask(__name__)
PROJECTS_PATH = config.PROJECTS_PATH
@app.route("/")
@app.route("/<string:project>")
def home(project=None):
project = project.lower() if project else ""
projects_lst = [p.lower() for p in os.listdir(PROJECTS_PATH)] or []
project_selected = project if project in projects_lst else projects_lst[0]
project_path = f"{PROJECTS_PATH}/{project_selected}"
images_lst = os.listdir(project_path) or []
rgx = '\$(.*?)\$' # regex searched
images_lst_dct = [
dict(
name=f"{i}",
mobile_align=f"{re.search(rgx, i).group(1)}%" if re.search(rgx, i) else None,
metadata=get_exif_data(f'{project_path}/{i}')
) for i in images_lst
]
return render_template(
"index.html",
projects=projects_lst,
project_selected=project_selected,
images_availables=images_lst_dct,
instagram_url=config.INSTAGRAM_URL,
linkedin_url=config.LINKEDIN_URL,
)
@app.route('/images/<path:project>/<path:image>')
def serve_project_image(project, image):
return send_from_directory(f'{PROJECTS_PATH}/{project}', image), 200
@app.route('/documents/<string:document>')
def serve_project_document(document):
return send_from_directory(f'static/documents', document), 200
@app.route('/sitemap.xml')
def sitemap():
# host = urlparse(request.base_url).hostname
# protocol = urlparse(request.base_url).scheme
projects_lst = [p.lower() for p in os.listdir(PROJECTS_PATH)] or []
xml_template = render_template("sitemap.xml",
dns=config.DNS,
projects=projects_lst,
lastmod=f"{datetime.today().strftime('%Y')}-01-01"
)
response = make_response(xml_template)
response.headers['Content-Type'] = 'application/xml; charset=utf-8'
return response
@app.errorhandler(404)
def page_not_found(error):
return redirect('/'), 302
if __name__ == "__main__":
# run app in debug mode on port 5000
app.run(debug=True, port=5000, host='0.0.0.0')