Back-end part of path mappings browser.

This commit is contained in:
Louis Vézina 2020-06-20 09:03:48 -04:00
parent e3f9030463
commit 487434c9fc
3 changed files with 113 additions and 0 deletions

View File

@ -33,6 +33,7 @@ from get_providers import get_providers, get_providers_auth, list_throttled_prov
from event_handler import event_stream
from scheduler import scheduler
from subsyncer import subsync
from filesystem import browse_bazarr_filesystem, browse_sonarr_filesystem, browse_radarr_filesystem
from subliminal_patch.core import SUBTITLE_EXTENSIONS
@ -1436,6 +1437,28 @@ class SyncSubtitles(Resource):
return '', 200
class BrowseBazarrFS(Resource):
@authenticate
def get(self):
path = request.args.get('path') or ''
result = browse_bazarr_filesystem(path)
return jsonify(result)
class BrowseSonarrFS(Resource):
@authenticate
def get(self):
path = request.args.get('path') or ''
return jsonify(browse_sonarr_filesystem(path))
class BrowseRadarrFS(Resource):
@authenticate
def get(self):
path = request.args.get('path') or ''
return jsonify(browse_radarr_filesystem(path))
api.add_resource(Shutdown, '/shutdown')
api.add_resource(Restart, '/restart')
@ -1490,3 +1513,7 @@ api.add_resource(SearchWantedSeries, '/search_wanted_series')
api.add_resource(SearchWantedMovies, '/search_wanted_movies')
api.add_resource(SyncSubtitles, '/sync_subtitles')
api.add_resource(BrowseBazarrFS, '/browse_bazarr_filesystem')
api.add_resource(BrowseSonarrFS, '/browse_sonarr_filesystem')
api.add_resource(BrowseRadarrFS, '/browse_radarr_filesystem')

View File

@ -20,6 +20,8 @@ def create_app():
app.route = prefix_route(app.route, base_url.rstrip('/'))
app.config["SECRET_KEY"] = 'test'
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
app.config['JSON_AS_ASCII'] = False
if args.dev:
app.config["DEBUG"] = True

84
bazarr/filesystem.py Normal file
View File

@ -0,0 +1,84 @@
import os
import requests
import logging
import string
from config import settings, url_sonarr, url_radarr
def browse_bazarr_filesystem(path=''):
if path == '' or path == '/':
if os.name == 'nt':
dir_list = []
for drive in string.ascii_uppercase:
drive_letter = drive + ':\\'
if os.path.exists(drive_letter):
dir_list.append(drive_letter)
else:
path = "/"
dir_list = [f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))]
else:
dir_list = [f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))]
data = []
for item in dir_list:
full_path = os.path.join(path, item, '')
item = {
"name": item,
"path": full_path
}
data.append(item)
parent = os.path.dirname(path)
result = {'directories': data}
if parent != path:
result.update({'parent': parent})
return result
def browse_sonarr_filesystem(path=''):
url_sonarr_api_filesystem = url_sonarr() + "/api/filesystem?path=" + path + \
"&allowFoldersWithoutTrailingSlashes=true&includeFiles=false&apikey=" + \
settings.sonarr.apikey
try:
r = requests.get(url_sonarr_api_filesystem, timeout=60, verify=False)
r.raise_for_status()
except requests.exceptions.HTTPError:
logging.exception("BAZARR Error trying to get series from Sonarr. Http error.")
return
except requests.exceptions.ConnectionError:
logging.exception("BAZARR Error trying to get series from Sonarr. Connection Error.")
return
except requests.exceptions.Timeout:
logging.exception("BAZARR Error trying to get series from Sonarr. Timeout Error.")
return
except requests.exceptions.RequestException:
logging.exception("BAZARR Error trying to get series from Sonarr.")
return
return r.json()
def browse_radarr_filesystem(path=''):
url_radarr_api_filesystem = url_radarr() + "/api/filesystem?path=" + path + \
"&allowFoldersWithoutTrailingSlashes=true&includeFiles=false&apikey=" + \
settings.radarr.apikey
try:
r = requests.get(url_radarr_api_filesystem, timeout=60, verify=False)
r.raise_for_status()
except requests.exceptions.HTTPError:
logging.exception("BAZARR Error trying to get series from Radarr. Http error.")
return
except requests.exceptions.ConnectionError:
logging.exception("BAZARR Error trying to get series from Radarr. Connection Error.")
return
except requests.exceptions.Timeout:
logging.exception("BAZARR Error trying to get series from Radarr. Timeout Error.")
return
except requests.exceptions.RequestException:
logging.exception("BAZARR Error trying to get series from Radarr.")
return
return r.json()