-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
73 lines (57 loc) · 2.38 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
import json
import os
import logging
import subprocess
from flask import Flask, abort, request
CFG_DIR = '/config/'
if not os.path.isdir(CFG_DIR):
CFG_DIR = 'config/'
if not os.path.isdir(CFG_DIR):
os.makedirs(CFG_DIR)
CFG_FILE = CFG_DIR + 'arguments.txt'
SUBLIMINAL_LOG = CFG_DIR + 'subliminal_output.log'
if not os.path.exists(CFG_FILE):
with open(CFG_FILE, 'w') as outfile:
data = {'default':r'--cache-dir /config --addic7ed <user> <pass> --opensubtitles <user> <pass> download -p addic7ed -p opensubtitles -l en -m 85 -v "#FILE#"',
'sonarr':None,
'radarr':None}
outfile.write(json.dumps(data, indent=4))
logging.basicConfig(filename=CFG_DIR + 'web.log',level=logging.INFO if not os.environ.get('DEBUG') else logging.DEBUG)
app = Flask(__name__)
@app.route('/', methods=['POST'])
def index():
if not request.json:
logging.debug('Skipping, request is not JSON.')
abort(400)
logging.debug('Got json request: ' + request.data.decode("utf-8"))
eventType = request.json.get('eventType')
if eventType == 'Test':
return 'Test'
mediaFile = ''
isMovie = False
if request.json.get('movieFile') and request.json.get('movie'):
isMovie = True
mediaFile = os.path.join(request.json['movie']['folderPath'], request.json['movieFile']['relativePath'])
if request.json.get('episodeFile') and request.json.get('series'):
mediaFile = os.path.join(request.json['series']['path'], request.json['episodeFile']['relativePath'])
if not mediaFile:
logging.warn('No media file found in request')
abort(400)
logging.info('Got ' + ('movie' if isMovie else 'episode') + ' download request! Running Subliminal for file: ' + mediaFile)
data = json.load(open(CFG_FILE))
cmd = data.get('radarr') if isMovie else data.get('sonarr')
if not cmd:
cmd = data.get('default')
if not cmd:
logging.warn('Parameter line is empty, please check your config..')
abort(400)
cmd = 'subliminal ' + cmd.replace('#FILE#', mediaFile)
logging.debug('CMD: ' + cmd)
try:
with open(SUBLIMINAL_LOG, 'a') as outfile:
subprocess.call(cmd, stdout=outfile, stderr=outfile, shell=True)
except Exception as e:
logging.error(e)
return 'Ok :)'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8978)