From c9f7ab8f9250fbbad2cb91da7ada0f2c2b13f677 Mon Sep 17 00:00:00 2001 From: Isaac Shoebottom Date: Wed, 31 Jul 2024 20:01:55 -0300 Subject: [PATCH] Add some docker shit --- .dockerignore | 12 ++++++++++++ .env.example | 3 +++ Dockerfile | 7 +++++++ docker-compose.yml | 7 +++++++ download.py | 36 ++++++++++++++++++++++++++++-------- env.example.py | 1 - 6 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 .dockerignore create mode 100644 .env.example create mode 100644 Dockerfile create mode 100644 docker-compose.yml delete mode 100644 env.example.py diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..dc24df2 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,12 @@ +__pycache__ +*.pyc +*.pyo +*.pyd +downloads/ +*.7z +VERSION +*.json +.git +.gitignore +.env.example +.env \ No newline at end of file diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..b8e633e --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +API_KEY = "asdf1234" +CHECK_INTERVAL = 86400 +DOWNLOAD_PATH = "./downloads" \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..33a7984 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3.11.9-alpine + +# Set the working directory +WORKDIR /app +COPY . /app + +CMD ["python", "download.py"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..6d850ba --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,7 @@ +version: "3" +services: + xedit-mirror: + image: xedit-mirror + build: + context: . + dockerfile: Dockerfile \ No newline at end of file diff --git a/download.py b/download.py index 1ea7dc1..2389716 100644 --- a/download.py +++ b/download.py @@ -5,7 +5,26 @@ from urllib.request import Request, urlopen, build_opener BASE_URL = "https://api.nexusmods.com" API_VERSION = "v1" API_URL = f"{BASE_URL}/{API_VERSION}/games" -from env import API_KEY + +# Read in environment variables (takes precedence over .env file) +API_KEY = os.environ.get("API_KEY") +if not API_KEY: + raise ValueError("API_KEY environment variable must be set") +CHECK_INTERVAL = os.environ.get("CHECK_INTERVAL") +if not CHECK_INTERVAL: + CHECK_INTERVAL = 60 * 60 * 24 # 24 hours +DOWNLOAD_PATH = os.environ.get("DOWNLOAD_PATH") +if not DOWNLOAD_PATH: + DOWNLOAD_PATH = "./downloads" + +# Read in dot env file +file = open(".env", "r") +lines = file.readlines() +for line in lines: + key, value = line.split("=") + key = key.strip() + if os.environ.get(key) is None: + os.environ[key] = value.strip() # Docs: https://app.swaggerhub.com/apis-docs/NexusMods/nexus-mods_public_api_params_in_form_data/1.0 @@ -89,7 +108,7 @@ def download_file(edit): # Create directories if they don't exist filename = f"{edit['name']} {edit['version']}.7z" - path = f"./downloads/{edit['name']}/{filename}" + path = f"{DOWNLOAD_PATH}/{edit['name']}/{filename}" if not os.path.exists(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) @@ -104,9 +123,10 @@ def download_file(edit): with open(path, "wb") as file: file.write(response.read()) - -for edit in xEdits: - print(f"Downloading {edit}...") - download_file(xEdits[edit]) - # Wait 1 second between downloads, to prevent rate limiting - time.sleep(1) +while True: + for edit in xEdits: + print(f"Downloading {edit}...") + download_file(xEdits[edit]) + # Wait 1 second between downloads, to prevent rate limiting + time.sleep(1) + time.sleep(CHECK_INTERVAL) diff --git a/env.example.py b/env.example.py deleted file mode 100644 index 8ba7b0a..0000000 --- a/env.example.py +++ /dev/null @@ -1 +0,0 @@ -API_KEY = "" \ No newline at end of file