forked from Screenly/Anthias
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·866 lines (735 loc) · 24.4 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
#!/usr/bin/env python
# -*- coding: utf8 -*-
__author__ = "Screenly, Inc"
__copyright__ = "Copyright 2012-2017, Screenly, Inc"
__license__ = "Dual License: GPLv2 and Commercial License"
from datetime import timedelta
from functools import wraps
from hurry.filesize import size
from os import path, makedirs, statvfs, mkdir, system, getenv
from sh import git
import sh
from subprocess import check_output
import json
import os
import traceback
import uuid
from flask import Flask, request, render_template, make_response, send_from_directory
from flask_restful_swagger_2 import swagger, Resource, Api, Schema
from flask_swagger_ui import get_swaggerui_blueprint
from flask_cors import CORS
from gunicorn.app.base import Application
from lib import db
from lib import queries
from lib import assets_helper
from lib import diagnostics
from lib import backup_helper
from lib.utils import json_dump, download_video_from_youtube
from lib.utils import get_node_ip
from lib.utils import validate_url
from lib.utils import url_fails
from lib.utils import get_video_duration
from dateutil import parser as date_parser
from mimetypes import guess_type
from settings import settings, DEFAULTS, CONFIGURABLE_SETTINGS, auth_basic
from werkzeug.wrappers import Request
app = Flask(__name__)
CORS(app)
api = Api(app, api_version="v1", title="Screenly OSE API")
################################
# Utilities
################################
@api.representation('application/json')
def output_json(data, code, headers=None):
response = make_response(json_dump(data), code)
response.headers.extend(headers or {})
return response
def api_error(error):
return make_response(json_dump({'error': error}), 500)
def is_up_to_date():
"""
Determine if there is any update available.
Used in conjunction with check_update() in viewer.py.
"""
sha_file = os.path.join(settings.get_configdir(), 'latest_screenly_sha')
# Until this has been created by viewer.py,
# let's just assume we're up to date.
if not os.path.exists(sha_file):
return True
try:
with open(sha_file, 'r') as f:
latest_sha = f.read().strip()
except:
latest_sha = None
if latest_sha:
branch_sha = git('rev-parse', 'HEAD')
return branch_sha.stdout.strip() == latest_sha
# If we weren't able to verify with remote side,
# we'll set up_to_date to true in order to hide
# the 'update available' message
else:
return True
def template(template_name, **context):
"""Screenly template response generator. Shares the
same function signature as Flask's render_template() method
but also injects some global context."""
# Add global contexts
context['up_to_date'] = is_up_to_date()
context['default_duration'] = settings['default_duration']
context['default_streaming_duration'] = settings['default_streaming_duration']
context['use_24_hour_clock'] = settings['use_24_hour_clock']
context['template_settings'] = {
'imports': ['from lib.utils import template_handle_unicode'],
'default_filters': ['template_handle_unicode'],
}
return render_template(template_name, context=context)
################################
# Models
################################
class AssetModel(Schema):
type = 'object'
properties = {
'asset_id': {'type': 'string'},
'name': {'type': 'string'},
'uri': {'type': 'string'},
'start_date': {
'type': 'string',
'format': 'date-time'
},
'end_date': {
'type': 'string',
'format': 'date-time'
},
'duration': {'type': 'string'},
'mimetype': {'type': 'string'},
'is_active': {'type': 'boolean'},
'is_enabled': {
'type': 'integer',
'format': 'int64',
},
'is_processing': {
'type': 'integer',
'format': 'int64',
},
'nocache': {
'type': 'integer',
'format': 'int64',
},
'play_order': {
'type': 'integer',
'format': 'int64',
}
}
################################
# API
################################
def prepare_asset(request):
req = Request(request.environ)
data = None
# For backward compatibility
try:
data = json.loads(req.data)
except ValueError:
data = json.loads(req.form['model'])
except TypeError:
data = json.loads(req.form['model'])
def get(key):
val = data.get(key, '')
if isinstance(val, unicode):
return val.strip()
elif isinstance(val, basestring):
return val.strip().decode('utf-8')
else:
return val
if not all([get('name'), get('uri'), get('mimetype')]):
raise Exception("Not enough information provided. Please specify 'name', 'uri', and 'mimetype'.")
asset = {
'name': get('name'),
'mimetype': get('mimetype'),
'asset_id': get('asset_id'),
'is_enabled': get('is_enabled'),
'is_processing': get('is_processing'),
'nocache': get('nocache'),
}
uri = get('uri')
if uri.startswith('/'):
if not path.isfile(uri):
raise Exception("Invalid file path. Failed to add asset.")
else:
if not validate_url(uri):
raise Exception("Invalid URL. Failed to add asset.")
if not asset['asset_id']:
asset['asset_id'] = uuid.uuid4().hex
if uri.startswith('/'):
os.rename(uri, path.join(settings['assetdir'], asset['asset_id']))
uri = path.join(settings['assetdir'], asset['asset_id'])
if 'youtube_asset' in asset['mimetype']:
uri, asset['name'], asset['duration'] = download_video_from_youtube(uri, asset['asset_id'])
asset['mimetype'] = 'video'
asset['is_processing'] = 1
asset['uri'] = uri
if "video" in asset['mimetype']:
if get('duration') == 'N/A' or int(get('duration')) == 0:
asset['duration'] = int(get_video_duration(uri).total_seconds())
else:
# Crashes if it's not an int. We want that.
asset['duration'] = int(get('duration'))
# parse date via python-dateutil and remove timezone info
if get('start_date'):
asset['start_date'] = date_parser.parse(get('start_date')).replace(tzinfo=None)
else:
asset['start_date'] = ""
if get('end_date'):
asset['end_date'] = date_parser.parse(get('end_date')).replace(tzinfo=None)
else:
asset['end_date'] = ""
return asset
# api view decorator. handles errors
def api_response(view):
@wraps(view)
def api_view(*args, **kwargs):
try:
return view(*args, **kwargs)
except Exception as e:
traceback.print_exc()
return api_error(unicode(e))
return api_view
class Assets(Resource):
method_decorators = [auth_basic]
@swagger.doc({
'responses': {
'200': {
'description': 'List of assets',
'schema': {
'type': 'array',
'items': AssetModel
}
}
}
})
def get(self):
with db.conn(settings['database']) as conn:
assets = assets_helper.read(conn)
return assets
@api_response
@swagger.doc({
'parameters': [
{
'name': 'model',
'in': 'formData',
'type': 'string',
'description':
'''
Yes, that is just a string of JSON not JSON itself it will be parsed on the other end.
Content-Type: application/x-www-form-urlencoded
model: "{
"name": "Website",
"mimetype": "webpage",
"uri": "http://example.com",
"is_active": false,
"start_date": "2017-02-02T00:33:00.000Z",
"end_date": "2017-03-01T00:33:00.000Z",
"duration": "10",
"is_enabled": 0,
"is_processing": 0,
"nocache": 0,
"play_order": 0
}"
'''
}
],
'responses': {
'201': {
'description': 'Asset created',
'schema': AssetModel
}
}
})
def post(self):
asset = prepare_asset(request)
if url_fails(asset['uri']):
raise Exception("Could not retrieve file. Check the asset URL.")
with db.conn(settings['database']) as conn:
return assets_helper.create(conn, asset), 201
class Asset(Resource):
method_decorators = [api_response, auth_basic]
@swagger.doc({
'parameters': [
{
'name': 'asset_id',
'type': 'string',
'in': 'path',
'description': 'id of an asset'
}
],
'responses': {
'200': {
'description': 'Asset',
'schema': AssetModel
}
}
})
def get(self, asset_id):
with db.conn(settings['database']) as conn:
return assets_helper.read(conn, asset_id)
@swagger.doc({
'parameters': [
{
'name': 'asset_id',
'type': 'string',
'in': 'path',
'description': 'id of an asset'
},
{
'name': 'model',
'in': 'formData',
'type': 'string',
'description':
'''
Content-Type: application/x-www-form-urlencoded
model: "{
"asset_id": "793406aa1fd34b85aa82614004c0e63a",
"name": "Website",
"mimetype": "webpage",
"uri": "http://example.com",
"is_active": false,
"start_date": "2017-02-02T00:33:00.000Z",
"end_date": "2017-03-01T00:33:00.000Z",
"duration": "10",
"is_enabled": 0,
"is_processing": 0,
"nocache": 0,
"play_order": 0
}"
'''
}
],
'responses': {
'200': {
'description': 'Asset updated',
'schema': AssetModel
}
}
})
def put(self, asset_id):
with db.conn(settings['database']) as conn:
return assets_helper.update(conn, asset_id, prepare_asset(request))
@swagger.doc({
'parameters': [
{
'name': 'asset_id',
'type': 'string',
'in': 'path',
'description': 'id of an asset'
},
],
'responses': {
'204': {
'description': 'Deleted'
}
}
})
def delete(self, asset_id):
with db.conn(settings['database']) as conn:
asset = assets_helper.read(conn, asset_id)
try:
if asset['uri'].startswith(settings['assetdir']):
os.remove(asset['uri'])
except OSError:
pass
assets_helper.delete(conn, asset_id)
return '', 204 # return an OK with no content
class AssetsNewVersion(Resource):
method_decorators = [auth_basic]
@swagger.doc({
'responses': {
'200': {
'description': 'List of assets',
'schema': {
'type': 'array',
'items': AssetModel
}
}
}
})
def get(self):
with db.conn(settings['database']) as conn:
assets = assets_helper.read(conn)
return assets
@api_response
@swagger.doc({
'parameters': [
{
'in': 'body',
'name': 'model',
'description': 'Adds a asset',
'schema': AssetModel,
'required': True
}
],
'responses': {
'201': {
'description': 'Asset created',
'schema': AssetModel
}
}
})
def post(self):
asset = prepare_asset(request)
if url_fails(asset['uri']):
raise Exception("Could not retrieve file. Check the asset URL.")
with db.conn(settings['database']) as conn:
return assets_helper.create(conn, asset), 201
class AssetNewVersion(Resource):
method_decorators = [api_response, auth_basic]
@swagger.doc({
'parameters': [
{
'name': 'asset_id',
'type': 'string',
'in': 'path',
'description': 'id of an asset'
}
],
'responses': {
'200': {
'description': 'Asset',
'schema': AssetModel
}
}
})
def get(self, asset_id):
with db.conn(settings['database']) as conn:
return assets_helper.read(conn, asset_id)
@swagger.doc({
'parameters': [
{
'name': 'asset_id',
'type': 'string',
'in': 'path',
'description': 'id of an asset',
'required': True
},
{
'in': 'body',
'name': 'model',
'description': 'Adds a asset',
'schema': AssetModel,
'required': True
}
],
'responses': {
'200': {
'description': 'Asset updated',
'schema': AssetModel
}
}
})
def put(self, asset_id):
with db.conn(settings['database']) as conn:
return assets_helper.update(conn, asset_id, prepare_asset(request))
@swagger.doc({
'parameters': [
{
'name': 'asset_id',
'type': 'string',
'in': 'path',
'description': 'id of an asset',
'required': True
},
],
'responses': {
'204': {
'description': 'Deleted'
}
}
})
def delete(self, asset_id):
with db.conn(settings['database']) as conn:
asset = assets_helper.read(conn, asset_id)
try:
if asset['uri'].startswith(settings['assetdir']):
os.remove(asset['uri'])
except OSError:
pass
assets_helper.delete(conn, asset_id)
return '', 204 # return an OK with no content
class FileAsset(Resource):
method_decorators = [api_response, auth_basic]
@swagger.doc({
'parameters': [
{
'name': 'file_upload',
'type': 'file',
'in': 'formData',
'description': 'File to be sent'
}
],
'responses': {
'200': {
'description': 'File path',
'schema': {
'type': 'string'
}
}
}
})
def post(self):
req = Request(request.environ)
file_upload = req.files.get('file_upload')
filename = file_upload.filename
file_path = path.join(settings['assetdir'], filename) + ".tmp"
if 'Content-Range' in request.headers:
range_str = request.headers['Content-Range']
start_bytes = int(range_str.split(' ')[1].split('-')[0])
with open(file_path, 'a') as f:
f.seek(start_bytes)
f.write(file_upload.read())
else:
file_upload.save(file_path)
return file_path
class PlaylistOrder(Resource):
method_decorators = [api_response, auth_basic]
@swagger.doc({
'parameters': [
{
'name': 'ids',
'in': 'formData',
'type': 'string',
'description':
'''
Content-Type: application/x-www-form-urlencoded
ids: "793406aa1fd34b85aa82614004c0e63a,1c5cfa719d1f4a9abae16c983a18903b,9c41068f3b7e452baf4dc3f9b7906595"
comma separated ids
'''
},
],
'responses': {
'204': {
'description': 'Sorted'
}
}
})
def post(self):
with db.conn(settings['database']) as conn:
assets_helper.save_ordering(conn, request.form.get('ids', '').split(','))
class Backup(Resource):
method_decorators = [api_response, auth_basic]
@swagger.doc({
'responses': {
'200': {
'description': 'Backup filename',
'schema': {
'type': 'string'
}
}
}
})
def post(self):
filename = backup_helper.create_backup()
return filename, 201
class Recover(Resource):
method_decorators = [api_response, auth_basic]
@swagger.doc({
'parameters': [
{
'name': 'backup_upload',
'type': 'file',
'in': 'formData'
}
],
'responses': {
'200': {
'description': 'Recovery successful'
}
}
})
def post(self):
req = Request(request.environ)
file_upload = (req.files['backup_upload'])
filename = file_upload.filename
if guess_type(filename)[0] != 'application/x-tar':
raise Exception("Incorrect file extension.")
location = path.join("static", filename)
file_upload.save(location)
backup_helper.recover(location)
return "Recovery successful."
class AssetsControl(Resource):
method_decorators = [api_response, auth_basic]
@swagger.doc({
'parameters': [
{
'name': 'command',
'type': 'string',
'in': 'path',
'description': 'Control command ("next" or "previous")'
}
],
'responses': {
'200': {
'description': 'Asset switched'
}
}
})
def get(self, command):
if command == "next":
system('pkill -SIGUSR1 -f viewer.py')
return "Asset switched"
if command == "previous":
system('pkill -SIGUSR2 -f viewer.py')
return "Asset switched"
api.add_resource(Assets, '/api/v1/assets')
api.add_resource(Asset, '/api/v1/assets/<asset_id>')
api.add_resource(AssetsNewVersion, '/api/v1.1/assets')
api.add_resource(AssetNewVersion, '/api/v1.1/assets/<asset_id>')
api.add_resource(FileAsset, '/api/v1/file_asset')
api.add_resource(PlaylistOrder, '/api/v1/assets/order')
api.add_resource(Backup, '/api/v1/backup')
api.add_resource(Recover, '/api/v1/recover')
api.add_resource(AssetsControl, '/api/v1/assets/control/<command>')
try:
my_ip = get_node_ip()
except:
pass
else:
SWAGGER_URL = '/api/docs'
swagger_address = getenv("SWAGGER_HOST", my_ip)
if swagger_address == my_ip:
swagger_address += ":{}".format(settings.get_listen_port())
if settings.get_listen_ip() == '127.0.0.1':
API_URL = 'https://{}/api/swagger.json'.format(swagger_address)
else:
API_URL = "http://{}/api/swagger.json".format(swagger_address)
swaggerui_blueprint = get_swaggerui_blueprint(
SWAGGER_URL,
API_URL,
config={
'app_name': "Screenly API"
}
)
app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL)
################################
# Views
################################
@app.route('/')
@auth_basic
def viewIndex():
player_name = settings['player_name']
my_ip = get_node_ip()
resin_uuid = getenv("RESIN_UUID", None)
ws_addresses = []
# If we bind on 127.0.0.1, `enable_ssl.sh` has most likely been executed
if settings.get_listen_ip() == '127.0.0.1':
ws_addresses.append('wss://' + my_ip + '/ws/')
else:
ws_addresses.append('ws://' + my_ip + ':' + settings['websocket_port'])
if resin_uuid:
ws_addresses.append('wss://{}.resindevice.io/ws/'.format(resin_uuid))
return template('index.html', ws_addresses=ws_addresses, player_name=player_name)
@app.route('/settings', methods=["GET", "POST"])
@auth_basic
def settings_page():
context = {'flash': None}
if request.method == "POST":
for field, default in CONFIGURABLE_SETTINGS.items():
value = request.form.get(field, default)
if isinstance(default, bool):
value = value == 'on'
settings[field] = value
try:
settings.save()
system('pkill -SIGHUP -f viewer.py')
context['flash'] = {'class': "success", 'message': "Settings were successfully saved."}
except IOError as e:
context['flash'] = {'class': "error", 'message': e}
except OSError as e:
context['flash'] = {'class': "error", 'message': e}
else:
settings.load()
for field, default in DEFAULTS['viewer'].items():
context[field] = settings[field]
return template('settings.html', **context)
@app.route('/system_info')
@auth_basic
def system_info():
viewlog = None
try:
viewlog = [line.decode('utf-8') for line in
check_output(['sudo', 'systemctl', 'status', 'screenly-viewer.service', '-n', '20']).split('\n')]
except:
pass
loadavg = diagnostics.get_load_avg()['15 min']
display_info = diagnostics.get_monitor_status()
display_power = diagnostics.get_display_power()
# Calculate disk space
slash = statvfs("/")
free_space = size(slash.f_bavail * slash.f_frsize)
# Get uptime
uptime_in_seconds = diagnostics.get_uptime()
system_uptime = timedelta(seconds=uptime_in_seconds)
# Player name for title
player_name = settings['player_name']
return template(
'system_info.html',
player_name=player_name,
viewlog=viewlog,
loadavg=loadavg,
free_space=free_space,
uptime=system_uptime,
display_info=display_info,
display_power=display_power
)
@app.route('/splash_page')
def splash_page():
url = None
try:
my_ip = get_node_ip()
except Exception as e:
ip_lookup = False
error_msg = e
else:
ip_lookup = True
# If we bind on 127.0.0.1, `enable_ssl.sh` has most likely been
# executed and we should access over SSL.
if settings.get_listen_ip() == '127.0.0.1':
url = 'https://{}'.format(my_ip)
else:
url = "http://{}:{}".format(my_ip, settings.get_listen_port())
msg = url if url else error_msg
return template('splash_page.html', ip_lookup=ip_lookup, msg=msg)
@app.errorhandler(403)
def mistake403(code):
return 'The parameter you passed has the wrong format!'
@app.errorhandler(404)
def mistake404(code):
return 'Sorry, this page does not exist!'
################################
# Static
################################
@app.route('/static_with_mime/<string:path>')
def static_with_mime(path):
mimetype = request.args['mime'] if 'mime' in request.args else 'auto'
return send_from_directory(directory='static', filename=path, mimetype=mimetype)
if __name__ == "__main__":
# Make sure the asset folder exist. If not, create it
if not path.isdir(settings['assetdir']):
mkdir(settings['assetdir'])
# Create config dir if it doesn't exist
if not path.isdir(settings.get_configdir()):
makedirs(settings.get_configdir())
with db.conn(settings['database']) as conn:
with db.cursor(conn) as cursor:
cursor.execute(queries.exists_table)
if cursor.fetchone() is None:
cursor.execute(assets_helper.create_assets_table)
config = {
'bind': '{}:{}'.format(settings.get_listen_ip(), int(settings.get_listen_port())),
'threads': 2,
'timeout': 20
}
class GunicornApplication(Application):
def init(self, parser, opts, args):
return config
def load(self):
return app
GunicornApplication().run()