From 12a61627e8902196e8c7a7868557077e98e25e63 Mon Sep 17 00:00:00 2001 From: burkasaurusrex Date: Fri, 3 Jul 2020 20:24:48 -0600 Subject: [PATCH] To do: Create setting for image directory path --- .gitignore | 10 +++--- README.md | 3 +- app/config_tools.py | 65 ++++++++++++++++++++++++++------------ app/image_server.py | 38 ++++++++-------------- config/config.yml.template | 3 +- 5 files changed, 67 insertions(+), 52 deletions(-) diff --git a/.gitignore b/.gitignore index be62312..0785dfb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ -/config/config.yml -/dist/plex_auto_collections -/build -/app/plex_auto_collections.spec -/__pycache__/ +config.yml +**/dist +**/build +*.spec +**/__pycache__ /.vscode diff --git a/README.md b/README.md index e9f6fdc..3a6db3d 100644 --- a/README.md +++ b/README.md @@ -145,8 +145,9 @@ trakt: scope: created_at: image-server: - host: 192.168.1.41 + host: 192.168.1.1 port: 5000 + poster-directory: /config/posters ``` # Usage diff --git a/app/config_tools.py b/app/config_tools.py index 542f545..544d28f 100644 --- a/app/config_tools.py +++ b/app/config_tools.py @@ -3,6 +3,7 @@ import os import yaml import requests import socket +import urllib from plexapi.server import PlexServer from plexapi.video import Movie from plexapi.video import Show @@ -23,12 +24,15 @@ class Config: self.config_path = config_path with open(self.config_path, 'rt', encoding='utf-8') as yml: self.data = yaml.load(yml, Loader=yaml.FullLoader) + self.collections = self.data['collections'] self.plex = self.data['plex'] self.tmdb = self.data['tmdb'] self.trakt = self.data['trakt'] self.radarr = self.data['radarr'] - self.collections = self.data['collections'] - self.image_server = self.data['image-server'] + if 'image-server' in self.data: + self.image_server = self.data['image-server'] + else: + self.image_server = {} class Plex: @@ -93,16 +97,44 @@ class TraktClient: class ImageServer: - def __init__(self, config_path): + def __init__(self, config_path, mode="server"): config = Config(config_path).image_server - try: + # Best defaults for "host" are 0.0.0.0 for server and 127.0.0.1 for client + # Set respective defaults in server and client + if 'host' in config: self.host = config['host'] - except: - a = 1 - try: + else: + if mode == "server": + self.host = "0.0.0.0" + else: + self.host = "127.0.0.1" + # Set default port + if 'port' in config: self.port = config['port'] - except: - a = 1 + else: + self.port = 5000 + # Test and set default folder path + if mode == "server": + if 'poster-directory' in config: + self.posterdirectory = config['poster-directory'] + else: + app_dir = os.path.dirname(os.path.realpath(__file__)) + + # Test separate config folder with nested 'posters' folder + if os.path.exists(os.path.join(app_dir, "..", "config", "posters")): + self.posterdirectory = os.path.join("..", "config", "posters") + # Test separate config folder with nested 'images' folder + elif os.path.exists(os.path.join(app_dir, "..", "config", "images")): + self.posterdirectory = os.path.join("..", "config", "images") + # Test nested posters folder + elif os.path.exists(os.path.join(app_dir, "posters")): + self.posterdirectory = "posters" + # Test nested images folder + elif os.path.exists(os.path.join(app_dir, "images")): + self.posterdirectory = "images" + else: + raise RuntimeError("Invalid poster-directory setting") + def update_from_config(config_path, plex): config = Config(config_path) @@ -209,19 +241,12 @@ def update_from_config(config_path, plex): if not poster: # try to pull image from image_server. File is Collection name.png # Setup connection to image_server - try: - host = config.image_server["host"] - except AttributeError: - host = "127.0.0.1" - try: - port = config.image_server["port"] - except AttributeError: - port = "5000" + config_client = ImageServer(config_path, "client") - # Replace spaces in collection name with %20 - c_name = c.replace(" ", "%20") + # Url encode collection name + c_name = urllib.parse.quote(c, safe='') # Create url to where image would be if exists - poster = "http://" + host + ":" + str(port) + "/images/" + c_name + poster = "http://" + config_client.host + ":" + str(config_client.port) + "/images/" + c_name try: r = requests.request("GET", poster) if not r.status_code == 404: diff --git a/app/image_server.py b/app/image_server.py index 977754b..fc7a724 100644 --- a/app/image_server.py +++ b/app/image_server.py @@ -7,47 +7,35 @@ import logging import os import time -class Server: - def __init__(self, config_path): - self.server = ImageServer(config_path) - try: - self.host = self.server.host - except AttributeError: - self.host = "0.0.0.0" - try: - self.port = self.server.port - except AttributeError: - self.port = "5000" - - def check_running(config_path): time.sleep(1) - srv = Server(config_path) + config_client = ImageServer(config_path, "client") try: - r = requests.get("http://" + srv.host + ":" + str(srv.port), verify=False, timeout=1) - return "IMAGE SERVER RUNNING ON http://{}:{}/images/".format(srv.host, srv.port) + r = requests.get("http://" + config_client.host + ":" + str(config_client.port), verify=False, timeout=1) + return "IMAGE SERVER RUNNING ON http://{}:{}/images/".format(config_client.host, config_client.port) except (requests.exceptions.ConnectTimeout, requests.exceptions.ConnectionError): return "IMAGE SERVER NOT RUNNING" def start_srv(config_path): - server = Server(config_path) - app = Flask(__name__) - app.upload_folder = "images" + config_server = ImageServer(config_path, "server") + server = Flask(__name__) + server.upload_folder = config_server.posterdirectory log = logging.getLogger("werkzeug") os.environ['WERKZEUG_RUN_MAIN'] = 'true' log.setLevel(logging.ERROR) - @app.route('/images/') + @server.route('/images/') def send_file(c_name): - cwd = os.getcwd() - images = os.listdir(os.path.join(cwd, "images")) - for img in images: + app_dir = os.path.dirname(os.path.realpath(__file__)) + poster_dir = os.path.join(app_dir, config_server.posterdirectory) + posters = os.listdir(poster_dir) + for img in posters: if (c_name + ".") in img: - return send_from_directory(app.upload_folder, img) + return send_from_directory(server.upload_folder, img) return abort(404) try: - app.run(host=server.host, port=server.port) + server.run(host=config_server.host, port=config_server.port) except (OSError, TypeError) as e: print(e) diff --git a/config/config.yml.template b/config/config.yml.template index 26cb7dc..922fca7 100644 --- a/config/config.yml.template +++ b/config/config.yml.template @@ -51,5 +51,6 @@ trakt: scope: created_at: image-server: - host: 0.0.0.0 + host: 192.168.1.1 port: 5000 + poster-directory: /config/posters \ No newline at end of file