mirror of
https://github.com/mza921/Plex-Auto-Collections
synced 2024-11-14 16:27:22 +00:00
To do: Create setting for image directory path
This commit is contained in:
parent
f32d360c00
commit
12a61627e8
5 changed files with 67 additions and 52 deletions
10
.gitignore
vendored
10
.gitignore
vendored
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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/<path:c_name>')
|
||||
@server.route('/images/<path:c_name>')
|
||||
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)
|
||||
|
||||
|
|
|
@ -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
|
Loading…
Reference in a new issue