From 487434c9fc807d30995bec973fb994c10fcdf65b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Sat, 20 Jun 2020 09:03:48 -0400 Subject: [PATCH] Back-end part of path mappings browser. --- bazarr/api.py | 27 ++++++++++++++ bazarr/app.py | 2 ++ bazarr/filesystem.py | 84 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 bazarr/filesystem.py diff --git a/bazarr/api.py b/bazarr/api.py index 98b9ee035..0377c08a6 100644 --- a/bazarr/api.py +++ b/bazarr/api.py @@ -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') diff --git a/bazarr/app.py b/bazarr/app.py index cfdd0c746..31eb6be3e 100644 --- a/bazarr/app.py +++ b/bazarr/app.py @@ -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 diff --git a/bazarr/filesystem.py b/bazarr/filesystem.py new file mode 100644 index 000000000..c7a4d1bf6 --- /dev/null +++ b/bazarr/filesystem.py @@ -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()