forked from chirpradio/chirpradio-webcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
73 lines (62 loc) · 2.97 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
from flask import Flask, jsonify, render_template
from werkzeug import SharedDataMiddleware
import sys
import datetime
import desub
import os
from settings_local import *
PROCESS_ARGUMENTS = {
"dump-new-artists-in-dropbox": ['do_dump_new_artists_in_dropbox'],
"actually-dump-new-artists-in-dropbox": ['do_dump_new_artists_in_dropbox', '--rewrite'],
"do-periodic-import": ['do_periodic_import'],
"actually-do-periodic-import": ['do_periodic_import', '--actually-do-import'],
"generate-collection-nml": ['do_generate_collection_nml'],
"push-artists-to-chirpradio": ['do_push_artists_to_chirpradio'],
"push-albums-and-tracks-to-chirpradio": ['do_push_to_chirpradio'],
"diff-artist-whitelist": ['git', 'diff', 'chirp/library/data/artist-whitelist'],
"commit-artist-whitelist": ['git', 'commit', 'chirp/library/data/artist-whitelist', 'm', 'Adding new artists'],
"push-artist-whitelist": ['git', 'push'],
"install-collection-nml": ['install', '-m', '0775', '-g', 'traktor', 'output.nml', TRAKTOR_PATH + '/new-collection.nml']
}
app = Flask(__name__)
app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
'/': os.path.join(os.path.dirname(__file__), 'static')
})
@app.route('/')
def index():
return render_template("index.html", DEBUG=DEBUG, LIVE_RUN_IS_AVAILABLE=LIVE_RUN_IS_AVAILABLE)
@app.route('/<process_name>')
def start_process(process_name):
args = PROCESS_ARGUMENTS.get(process_name, False)
if args:
if process_name == 'push-albums-and-tracks-to-chirpradio':
# this is the only case that requires a run-time variable
timestamp = ''.join(datetime.date.today().isoformat().split('-')) + '-000000'
args.append('--start-at=' + timestamp)
proc = desub.join(args)
if proc.is_running():
return jsonify({'started': False, 'err': 'process is already running'})
else:
proc.start()
return jsonify({'started': True})
else:
return jsonify({'started': False, 'err': 'unrecognized process name: ' + process_name})
@app.route('/polling/<process_name>')
def poll(process_name):
args = PROCESS_ARGUMENTS.get(process_name, False)
if args:
if process_name == 'push-albums-and-tracks-to-chirpradio':
# this could potentially backfire if you happen to start the process in
# the moments before midnight. I would be open to a better way.
timestamp = ''.join(datetime.date.today().isoformat().split('-')) + '-000000'
args.append('--start-at=' + timestamp)
proc = desub.join(args)
return jsonify({
'err': proc.stderr.read(),
'out': proc.stdout.read(),
'is_done': not proc.is_running()
})
else:
return jsonify({'server_error': 'unrecognized process name: ' + process_name})
if __name__ == '__main__':
app.run(debug=DEBUG)