forked from l3uddz/plex_autoscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.py
executable file
·243 lines (198 loc) · 9.58 KB
/
scan.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
#!/usr/bin/env python2.7
import json
import logging
import os
import sys
import time
from logging.handlers import RotatingFileHandler
from flask import Flask
from flask import abort
from flask import request
# Get config
import config
import threads
############################################################
# INIT
############################################################
# Logging
logFormatter = logging.Formatter('%(asctime)24s - %(levelname)8s - %(name)9s [%(thread)5d]: %(message)s')
rootLogger = logging.getLogger()
rootLogger.setLevel(logging.INFO)
# Decrease modules logging
logging.getLogger('requests').setLevel(logging.ERROR)
logging.getLogger('werkzeug').setLevel(logging.ERROR)
logging.getLogger("peewee").setLevel(logging.ERROR)
# Console logger, log to stdout instead of stderr
consoleHandler = logging.StreamHandler(sys.stdout)
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)
# Load initial config
conf = config.Config()
# File logger
fileHandler = RotatingFileHandler(
conf.settings['logfile'],
maxBytes=1024 * 1024 * 5,
backupCount=5
)
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)
# Set configured log level
rootLogger.setLevel(conf.settings['loglevel'])
# Load config file
conf.load()
# Scan logger
logger = rootLogger.getChild("AUTOSCAN")
# Multiprocessing
thread = threads.Thread()
scan_lock = threads.PriorityLock()
resleep_paths = []
# local imports
import db
import plex
import utils
############################################################
# QUEUE PROCESSOR
############################################################
def queue_processor():
logger.info("Starting queue processor in 10 seconds")
try:
time.sleep(10)
logger.info("Queue processor started")
db_scan_requests = db.get_all_items()
items = 0
for db_item in db_scan_requests:
thread.start(plex.scan, args=[conf.configs, scan_lock, db_item['scan_path'], db_item['scan_for'],
db_item['scan_section'],
db_item['scan_type'], resleep_paths])
items += 1
time.sleep(2)
logger.info("Restored %d scan requests from database", items)
except Exception:
logger.exception("Exception while processing scan requests from database: ")
return
############################################################
# FUNCS
############################################################
def start_scan(path, scan_for, scan_type):
section = utils.get_plex_section(conf.configs, path)
if section <= 0:
return False
else:
logger.debug("Using section id: %d for '%s'", section, path)
if conf.configs['SERVER_USE_SQLITE']:
db_exists, db_file = db.exists_file_root_path(path)
if not db_exists and db.add_item(path, scan_for, section, scan_type):
logger.info("Added '%s' to database, proceeding with scan", path)
else:
logger.info(
"Already processing '%s' from same folder, aborting adding an extra scan request to the queue", db_file)
resleep_paths.append(db_file)
return False
thread.start(plex.scan, args=[conf.configs, scan_lock, path, scan_for, section, scan_type, resleep_paths])
return True
def start_queue_reloader():
thread.start(queue_processor)
return True
############################################################
# SERVER
############################################################
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
@app.route("/%s" % conf.configs['SERVER_PASS'], methods=['GET'])
def manual_scan():
if not conf.configs['SERVER_ALLOW_MANUAL_SCAN']:
return abort(401)
page = '<html><body>' \
'<form action="" method="post"> File to be scanned:<br>' \
'<input type="text" name="filepath" value=""> ' \
'<input type="hidden" name="eventType" value="Manual"> ' \
'<br><br><input type="submit" value="Submit"></form> ' \
'<p>Clicking submit will add this file to the scan backlog.</p></body></html>'
return page, 200
@app.route("/%s" % conf.configs['SERVER_PASS'], methods=['POST'])
def client_pushed():
if request.content_type == 'application/json':
data = request.get_json(silent=True)
else:
data = request.form.to_dict()
if not data:
logger.error("Invalid scan request from: %r", request.remote_addr)
abort(400)
logger.debug("Client %r request dump:\n%s", request.remote_addr, json.dumps(data, indent=4, sort_keys=True))
if ('eventType' in data and data['eventType'] == 'Test') or ('EventType' in data and data['EventType'] == 'Test'):
logger.info("Client %r made a test request, event: '%s'", request.remote_addr, 'Test')
elif 'eventType' in data and data['eventType'] == 'Manual':
logger.info("Client %r made a manual scan request for: '%s'", request.remote_addr, data['filepath'])
final_path = utils.map_pushed_path(conf.configs, data['filepath'])
# ignore this request?
ignore, ignore_match = utils.should_ignore(final_path, conf.configs)
if ignore:
logger.info("Ignored scan request for '%s' because '%s' was matched from SERVER_IGNORE_LIST", final_path,
ignore_match)
return "Ignoring scan request because %s was matched from your SERVER_IGNORE_LIST" % ignore_match
if start_scan(final_path, 'Manual', 'Manual'):
return "'%s' was added to scan backlog." % final_path
else:
return "Error adding '%s' to scan backlog." % data['filepath']
elif 'series' in data and 'eventType' in data and data['eventType'] == 'Rename' and 'path' in data['series']:
# sonarr Rename webhook
logger.info("Client %r scan request for series: '%s', event: '%s'", request.remote_addr, data['series']['path'],
"Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType'])
final_path = utils.map_pushed_path(conf.configs, data['series']['path'])
start_scan(final_path, 'Sonarr',
"Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType'])
elif 'movie' in data and 'eventType' in data and data['eventType'] == 'Rename' and 'folderPath' in data['movie']:
# radarr Rename webhook
logger.info("Client %r scan request for movie: '%s', event: '%s'", request.remote_addr,
data['movie']['folderPath'],
"Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType'])
final_path = utils.map_pushed_path(conf.configs, data['movie']['folderPath'])
start_scan(final_path, 'Radarr',
"Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType'])
elif 'movie' in data and 'movieFile' in data and 'folderPath' in data['movie'] and \
'relativePath' in data['movieFile'] and 'eventType' in data:
# radarr download/upgrade webhook
path = os.path.join(data['movie']['folderPath'], data['movieFile']['relativePath'])
logger.info("Client %r scan request for movie: '%s', event: '%s'", request.remote_addr, path,
"Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType'])
final_path = utils.map_pushed_path(conf.configs, path)
start_scan(final_path, 'Radarr',
"Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType'])
elif 'series' in data and 'episodeFile' in data and 'eventType' in data:
# sonarr download/upgrade webhook
path = os.path.join(data['series']['path'], data['episodeFile']['relativePath'])
logger.info("Client %r scan request for series: '%s', event: '%s'", request.remote_addr, path,
"Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType'])
final_path = utils.map_pushed_path(conf.configs, path)
start_scan(final_path, 'Sonarr',
"Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType'])
elif 'artist' in data and 'trackFile' in data and 'eventType' in data:
# lidarr download/upgrade webhook
path = os.path.join(data['artist']['path'], data['trackFile']['relativePath'])
logger.info("Client %r scan request for album track: '%s', event: '%s'", request.remote_addr, path,
"Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType'])
final_path = utils.map_pushed_path(conf.configs, path)
start_scan(final_path, 'Lidarr',
"Upgrade" if ('isUpgrade' in data and data['isUpgrade']) else data['eventType'])
else:
logger.error("Unknown scan request from: %r", request.remote_addr)
abort(400)
return "OK"
############################################################
# MAIN
############################################################
if __name__ == "__main__":
if conf.args['cmd'] == 'sections':
plex.show_sections(conf.configs)
elif conf.args['cmd'] == 'server':
if conf.configs['SERVER_USE_SQLITE']:
start_queue_reloader()
logger.info("Starting server: http://%s:%d/%s",
conf.configs['SERVER_IP'],
conf.configs['SERVER_PORT'],
conf.configs['SERVER_PASS']
)
app.run(host=conf.configs['SERVER_IP'], port=conf.configs['SERVER_PORT'], debug=False, use_reloader=False)
logger.info("Server stopped")
else:
logger.error("Unknown command...")