From 727722f39d399c9e8be8da8173b64ba8ad568179 Mon Sep 17 00:00:00 2001 From: Soitora Date: Sat, 9 Mar 2024 18:05:10 +0100 Subject: [PATCH] Keep line endings in Sort, and fix schema in Validate (#80) * Keep file line endings in sort.py * Auto-download Schema if missing --- .gitignore | 2 ++ requirements.txt | 1 + sort.py | 2 +- validate.py | 22 ++++++++++++++++++++-- 4 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a755fa9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# PlexAniSync files +custom_mappings_schema.json diff --git a/requirements.txt b/requirements.txt index df42c5c..c88ddb7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ jsonschema==4.6.1 ruamel.yaml==0.17.21 +requests==2.31.0 \ No newline at end of file diff --git a/sort.py b/sort.py index 377e66c..207ae6a 100644 --- a/sort.py +++ b/sort.py @@ -13,7 +13,7 @@ yaml.width = 1024 for file in glob.glob("*.yaml"): - with open(file, 'r+', encoding='utf-8') as f: + with open(file, 'r+b') as f: file_mappings = yaml.load(f) file_mappings["entries"].sort(key=lambda entry: entry["title"].lower()) # Jump to beginning of file and overwrite with sorted entries diff --git a/validate.py b/validate.py index ef58252..721d849 100644 --- a/validate.py +++ b/validate.py @@ -5,13 +5,31 @@ from jsonschema import validate from jsonschema.exceptions import ValidationError from ruamel.yaml import YAML +import requests logger = logging.getLogger("PlexAniSync") yaml = YAML(typ='safe') SUCCESS = True +schema_url = "https://raw.githubusercontent.com/RickDB/PlexAniSync/master/custom_mappings_schema.json" +local_schema_path = './custom_mappings_schema.json' -with open('./custom_mappings_schema.json', 'r', encoding='utf-8') as f: - schema = json.load(f) +# Try to load the schema from the local file +try: + with open(local_schema_path, 'r', encoding='utf-8') as f: + schema = json.load(f) +except FileNotFoundError: + # If the local file doesn't exist, fetch it from the remote URL + try: + response = requests.get(schema_url) + response.raise_for_status() + schema = response.json() + + # Save the fetched schema locally + with open(local_schema_path, 'w', encoding='utf-8') as f: + json.dump(schema, f, indent=4) + except requests.RequestException as e: + logger.error(f"Failed to fetch schema from {schema_url}: {e}") + sys.exit(1) for file in glob.glob("*.yaml"): # Create a Data object