From 21bbc6c6b29ab31f7013f498d3256ff5d2a2fc8a Mon Sep 17 00:00:00 2001 From: JoYo <> Date: Wed, 15 Dec 2021 18:06:20 -0500 Subject: [PATCH] update live addons --- .vscode/tasks.json | 27 ------------------------- Dockerfile | 1 + banana/compare.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++ banana/parsing.py | 37 ++++++++++++++++++++++++---------- banana/scripts.py | 21 +++++++++---------- pyproject.toml | 3 ++- setup.py | 6 +++++- 7 files changed, 95 insertions(+), 50 deletions(-) delete mode 100644 .vscode/tasks.json create mode 100644 banana/compare.py diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index 394b744..0000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - // See https://go.microsoft.com/fwlink/?LinkId=733558 - // for the documentation about the tasks.json format - "version": "2.0.0", - "tasks": [ - { - "label": "build", - "type": "shell", - "command": "docker-compose build", - "problemMatcher": [], - "group": { - "kind": "build", - "isDefault": true - } - }, - { - "label": "test", - "type": "shell", - "command": "docker-compose up", - "problemMatcher": [], - "group": { - "kind": "test", - "isDefault": true - } - }, - ] -} diff --git a/Dockerfile b/Dockerfile index 9916642..c38a07f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,7 @@ FROM ubuntu:20.04 ENV DEBIAN_FRONTEND noninteractive RUN apt-get update && apt-get install -y \ + python3-packaging \ python3-pip \ python3-requests \ python3-yaml \ diff --git a/banana/compare.py b/banana/compare.py new file mode 100644 index 0000000..64eba8b --- /dev/null +++ b/banana/compare.py @@ -0,0 +1,50 @@ +from io import BytesIO +from pathlib import Path +from shutil import rmtree, copytree +from tempfile import TemporaryDirectory +from zipfile import ZipFile +import logging +import requests + +from . import parsing + + +def live_to_esoui(*, path: Path, esoui_uris: list): + live_name, live_version, live_path = parsing.live(path) + + if not live_path: + return + + esoui_name, esoui_version, esoui_uri = None, None, None + + for name, version, uri in esoui_uris: + if name in live_name: + esoui_name, esoui_version, esoui_uri = name, version, uri + break + + if not esoui_name: + rmtree(live_path) + logging.info(f"{live_name} addon removed from: {live_path}") + return + + if esoui_version == live_version: + logging.info(f"{live_name} is already up to date.") + return + + response = requests.get(esoui_uri) + response.raise_for_status() + + temp_dir = TemporaryDirectory() + temp_path = Path(temp_dir.name) + + zip_file = ZipFile(BytesIO(response.content)) + zip_file.extractall(temp_path) + + rmtree(live_path) + + for each in temp_path.iterdir(): + copytree(each, live_path) + + logging.info( + f"{live_name} updated from {live_version} to {esoui_version} at {live_path}" + ) diff --git a/banana/parsing.py b/banana/parsing.py index ba1fe07..74175bc 100644 --- a/banana/parsing.py +++ b/banana/parsing.py @@ -6,6 +6,7 @@ import requests esoui_prefix = re.compile("https://www.esoui.com/downloads/info[0-9]+\-") esoui_version_html = re.compile('Version:\s+[^<]+') esoui_version_split = re.compile('Version:\s+') +esoui_download = re.compile('https://cdn.esoui.com/downloads/file[^"]*') live_title = re.compile("##\s+Title:\s+.*") live_title_split = re.compile("##\s+Title:\s+") live_version = re.compile("##\s+Version:\s+.*") @@ -17,24 +18,38 @@ def esoui(url: str): addon_name = addon_name.split(".html")[0] response = requests.get(url) - version_line = esoui_version_html.search(response.text) - version = esoui_version_split.split(version_line.group(0))[1] + response.raise_for_status() - esoui_dowload_uri = url.replace("info", "download") + version_line = esoui_version_html.search(response.text).group(0) + version = esoui_version_split.split(version_line)[1] + + esoui_page_url = url.replace("info", "download").replace(".html", "") + + response = requests.get(esoui_page_url) + response.raise_for_status() + + esoui_dowload_uri = esoui_download.search(response.text).group(0) response = requests.head(esoui_dowload_uri) response.raise_for_status() return addon_name, version, esoui_dowload_uri -def live_addon(path: Path): +def live(path: Path): for meta in path.glob("*.txt"): - with meta.open("r") as file_open: - meta_data = file_open.read() - title = live_title.search(meta_data) - title = live_title_split.split(title.group(0))[1] - version = live_version.search(meta_data) - version = live_version_split.split(version.group(0))[1] + try: + with meta.open("r") as file_open: + meta_data = file_open.read() + except UnicodeDecodeError: + with meta.open("r", encoding="latin-1") as file_open: + meta_data = file_open.read() - return title, version + addon_name = live_title.search(meta_data).group(0) + addon_name = live_title_split.split(addon_name)[1] + version = live_version.search(meta_data).group(0) + version = live_version_split.split(version)[1] + + return addon_name, version, path + + return None, None, None diff --git a/banana/scripts.py b/banana/scripts.py index 5fbb9a6..20d1fb4 100644 --- a/banana/scripts.py +++ b/banana/scripts.py @@ -1,7 +1,8 @@ -import logging -from pathlib import Path from argparse import ArgumentParser +from pathlib import Path +import logging +from . import compare from . import config from . import parsing @@ -49,18 +50,18 @@ def periodical_script(): config_current = config.load(config_path) logging.info(f'addons list created at "{config_path}"') - addon_urls = config_current.get("addons") - - for url in addon_urls: - esoui = parsing.esoui(url) - logging.info(esoui) - live_path = Path(args.eso_live_path).joinpath("AddOns") if not live_path.is_dir(): logging.error(f"eso_live_path_invalid_dir {live_path}") return + addon_urls = config_current.get("addons") + esoui_uris = list() + + for url in addon_urls: + esoui = parsing.esoui(url) + esoui_uris.append(esoui) + for child in live_path.iterdir(): - live_addon = parsing.live_addon(child) - logging.info(live_addon) + compare.live_to_esoui(path=child, esoui_uris=esoui_uris) diff --git a/pyproject.toml b/pyproject.toml index d41070d..24e4952 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,5 +16,6 @@ include = "banana" eso-banana-script = "banana:scripts.periodical_script" [tool.poetry.dependencies] -requests = "" +packaging = "" PyYAML = "" +requests = "" diff --git a/setup.py b/setup.py index a29ae0a..e845526 100644 --- a/setup.py +++ b/setup.py @@ -8,5 +8,9 @@ setup( "console_scripts": ["eso-banana-script = banana:scripts.periodical_script"], }, python_requires=">3", - install_requires=["requests", "PyYAML"], + install_requires=[ + "packaging", + "PyYAML", + "requests", + ], )