forked from Screenly/Anthias
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
277 lines (203 loc) · 8.34 KB
/
server.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python
# -*- coding: utf8 -*-
__author__ = "Viktor Petersson"
__copyright__ = "Copyright 2012, WireLoad Inc"
__license__ = "Dual License: GPLv2 and Commercial License"
__version__ = "0.1.2"
__email__ = "[email protected]"
from Config import Config, asset as Asset
from sys import platform, stdout
from requests import head as req_head
from os import path, getloadavg, statvfs
from hashlib import md5
from json import dumps, loads
from datetime import datetime, timedelta
from bottle import route, run, debug, template, request, validate, error, static_file, get
from urlparse import urlparse
from hurry.filesize import size
from subprocess import check_output
# Get config
config = Config()
def get_playlist():
assets = config.getassets()
playlist = []
for asset in assets:
if (asset.start_date and asset.end_date) and (asset.start_date < config.time_lookup() and asset.end_date > config.time_lookup()):
playlist.append(asset.playlistitem())
return dumps(playlist)
def get_assets():
assets = config.getassets()
playlist = []
for asset in assets:
playlist.append(asset.playlistitem(""))
return dumps(playlist)
@route('/process_asset', method='POST')
def process_asset():
if (request.POST.get('name','').strip() and
request.POST.get('uri','').strip() and
request.POST.get('mimetype','').strip()
):
asset = Asset()
asset.name = request.POST.get('name','').decode('UTF-8')
asset.uri = request.POST.get('uri','').strip()
asset.mimetype = request.POST.get('mimetype','').strip()
# Make sure it's a valid resource
uri_check = urlparse(asset.uri)
#Support local assets both as /home/pi/image path #1.png and file:///home/pi/url%20path.png , Note special chars in absolute path.
local_and_exists = ((uri_check.scheme == "" and path.exists(asset.uri)) or (uri_check.scheme == "file" and path.exists(uri_check.path)))
if not (uri_check.scheme == "http" or uri_check.scheme == "https" or local_and_exists):
header = "Ops!"
message = "URL must be HTTP or HTTPS or absolute path to local file."
return template('message', header=header, message=message)
if not local_and_exists:
file = req_head(asset.uri)
# Only proceed if fetch was successful.
if local_and_exists or file.status_code == 200:
asset.asset_id = md5(asset.name + asset.uri).hexdigest()
asset.start_date = ""
asset.end_date = ""
asset.duration = ""
asset.INSERT(config)
header = "Yay!"
message = "Added asset (" + asset.asset_id + ") to the database."
return template('message', header=header, message=message)
else:
header = "Ops!"
message = "Unable to fetch file."
return template('message', header=header, message=message)
else:
header = "Ops!"
message = "Invalid input."
return template('message', header=header, message=message)
@route('/process_schedule', method='POST')
def process_schedule():
if (request.POST.get('asset','').strip() and
request.POST.get('start','').strip() and
request.POST.get('end','').strip()
):
asset_id = request.POST.get('asset','').strip()
input_start = request.POST.get('start','').strip()
input_end = request.POST.get('end','').strip()
asset = config.getasset(asset_id)
asset.start_date = datetime.strptime(input_start, '%Y-%m-%d @ %H:%M')
asset.end_date = datetime.strptime(input_end, '%Y-%m-%d @ %H:%M')
if "image" or "web" in asset.mimetype:
try:
asset.duration = request.POST.get('duration','').strip()
except:
header = "Ops!"
message = "Duration missing. This is required for images and web-pages."
return template('message', header=header, message=message)
else:
asset.duration = "N/A"
asset.UPDATE(config)
header = "Yes!"
message = "Successfully scheduled asset."
return template('message', header=header, message=message)
else:
header = "Ops!"
message = "Failed to process schedule."
return template('message', header=header, message=message)
@route('/update_asset', method='POST')
def update_asset():
if (request.POST.get('asset_id','').strip() and
request.POST.get('name','').strip() and
request.POST.get('uri','').strip() and
request.POST.get('mimetype','').strip()
):
asset = Asset()
asset.asset_id = request.POST.get('asset_id','').strip()
asset.name = request.POST.get('name','').decode('UTF-8')
asset.uri = request.POST.get('uri','').strip()
asset.mimetype = request.POST.get('mimetype','').strip()
try:
asset.duration = request.POST.get('duration','').strip()
except:
asset.duration = None
try:
input_start = request.POST.get('start','')
asset.start_date = datetime.strptime(input_start, '%Y-%m-%d @ %H:%M')
except:
asset.start_date = None
try:
input_end = request.POST.get('end','').strip()
asset.end_date = datetime.strptime(input_end, '%Y-%m-%d @ %H:%M')
except:
asset.end_date = None
asset.UPDATE(config)
header = "Yes!"
message = "Successfully updated asset."
return template('message', header=header, message=message)
else:
header = "Ops!"
message = "Failed to update asset."
return template('message', header=header, message=message)
@route('/delete_asset/:asset_id')
def delete_asset(asset_id):
try:
config.delete_asset(asset_id)
header = "Success!"
message = "Deleted asset."
return template('message', header=header, message=message)
except Exception as e:
header = "Ops!"
message = "Failed to delete asset. %s" % e
return template('message', header=header, message=message)
@route('/')
def viewIndex():
return template('index')
@route('/system_info')
def system_info():
viewer_log_file = '/tmp/screenly_viewer.log'
if path.exists(viewer_log_file):
viewlog = check_output(['tail', '-n', '20', viewer_log_file]).split('\n')
else:
viewlog = ["(no viewer log present -- is only the screenly server running?)\n"]
loadavg = getloadavg()[2]
resolution = check_output(['tvservice', '-s']).strip()
# Calculate disk space
slash = statvfs("/")
free_space = size(slash.f_bsize * slash.f_bavail)
# Get uptime
with open('/proc/uptime', 'r') as f:
uptime_seconds = float(f.readline().split()[0])
uptime = str(timedelta(seconds = uptime_seconds))
return template('system_info', viewlog=viewlog, loadavg=loadavg, free_space=free_space, uptime=uptime, resolution=resolution)
@route('/splash_page')
def splash_page():
url = config.get_conf_url();
ip_lookup = url is not None
if not ip_lookup:
url = "Unable to lookup IP from eth0."
return template('splash_page', ip_lookup=ip_lookup, url=url)
@route('/view_playlist')
def view_node_playlist():
nodeplaylist = loads(get_playlist())
return template('view_playlist', nodeplaylist=nodeplaylist)
@route('/view_assets')
def view_assets():
nodeplaylist = loads(get_assets())
return template('view_assets', nodeplaylist=nodeplaylist)
@route('/add_asset')
def add_asset():
return template('add_asset')
@route('/schedule_asset')
def schedule_asset():
assets = loads(get_assets())
return template('schedule_asset', assets=assets)
@route('/edit_asset/:asset_id')
def edit_asset(asset_id):
asset_info = config.getasset(asset_id).playlistitem()
return template('edit_asset', asset_info=asset_info)
# Static
@route('/static/:path#.+#', name='static')
def static(path):
return static_file(path, root='static')
@error(403)
def mistake403(code):
return 'The parameter you passed has the wrong format!'
@error(404)
def mistake404(code):
return 'Sorry, this page does not exist!'
#Starting the server listen on configured address and port
run(host=config.listen, port=config.port, reloader=True)