-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
91 lines (63 loc) · 2.02 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
from datetime import date
import logging
import os
from bottle import Bottle, view, static_file, template, redirect
import config as cfg
logging.basicConfig(level=logging.DEBUG)
LOG = logging.getLogger(__name__)
app = Bottle()
def fullpath(path):
'''Prepends SCRIPT_NAME; to be used mostly in templates'''
return '/'.join((cfg.SCRIPT_NAME, path.lstrip('/')))
TPL_DEFAULTS = {
'fullpath': fullpath,
}
@app.get('/', name='index')
def index():
redirect(app.get_url('live'), 303)
@app.get('/today', name='today')
def today():
today = date.today()
return by_date(today.year, today.month, today.day)
@app.get('/live', name='live')
@view('live', **TPL_DEFAULTS)
def today():
return {'rtsp_address': cfg.RTSP_ADDRESS}
@app.get('/date/<year:int>/<month:int>/<day:int>', name='by_date')
@view('by_date', **TPL_DEFAULTS)
def by_date(year, month, day):
recs = []
# Sort of validation
d = date(year, month, day)
ymd = (str(d.year), '{:02d}'.format(d.month), '{:02d}'.format(d.day))
recs_subdir = os.path.join(*ymd)
recs_path = os.path.join(cfg.RECORDS_DIR, recs_subdir)
try:
for vid in os.listdir(recs_path):
if cfg.RECORDS_PAT.match(vid) is None:
continue
snap = 'snap_{}.jpg'.format(vid[4:23])
recs.append((vid, snap))
recs.sort()
msg = '{} videos'.format(len(recs))
except FileNotFoundError:
pass
if not recs:
msg = 'No videos for this date'
header = d.strftime('%a, %d %B %Y')
return {
'header': header,
'msg': msg,
'recs': recs,
'recs_subdir': recs_subdir,
}
@app.get('/video/<path:path>', name='video')
def video(path):
return static_file(path, root=cfg.RECORDS_DIR)
@app.get('/static/<path:path>', name='static')
def static(path):
return static_file(path, root=cfg.STATIC_ROOT)
@app.error(404)
@view('404', **TPL_DEFAULTS)
def error_404(error):
return None