From ffa80903990b7b4d4d43203ac4d21fe1920a6c10 Mon Sep 17 00:00:00 2001 From: strNophix Date: Fri, 22 Jul 2022 14:54:57 +0200 Subject: [PATCH] Moved from .env to ini config format --- .env.example | 23 ----------------------- .gitignore | 3 +++ README.md | 2 +- al_arr_sync/__main__.py | 15 +++++++-------- al_arr_sync/config.py | 7 +++++++ al_arr_sync/radarr.py | 31 ++++++++++++++++++++++++------- al_arr_sync/sonarr.py | 39 ++++++++++++++++++++++++++++++++------- config.ini.example | 15 +++++++++++++++ poetry.lock | 17 +---------------- pyproject.toml | 1 - 10 files changed, 90 insertions(+), 63 deletions(-) delete mode 100644 .env.example create mode 100644 al_arr_sync/config.py create mode 100644 config.ini.example diff --git a/.env.example b/.env.example deleted file mode 100644 index cb11d87..0000000 --- a/.env.example +++ /dev/null @@ -1,23 +0,0 @@ -# Name of user to sync from -ANILIST_USERNAME= - -# The Sonarr instance to sync to -SONARR_API_URL= -SONARR_API_KEY= - -# Root folder to save the shows to -SONARR_FOLDER_PATH= - -# IDs of the quality and language profiles to use -SONARR_QUALITY_PROFILE=4 -SONARR_LANGUAGE_PROFILE=1 - -# The Radarr instance to sync to -RADARR_API_URL= -RADARR_API_KEY= - -# Root folder to save the movies to -RADARR_FOLDER_PATH= - -# ID of the quality profile to use -RADARR_QUALITY_PROFILE=4 diff --git a/.gitignore b/.gitignore index 68bc17f..eff883a 100644 --- a/.gitignore +++ b/.gitignore @@ -158,3 +158,6 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + +# Development +config.ini diff --git a/README.md b/README.md index 5a412ea..fdd5093 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A small script for syncing your currently watching anime from AniList to Sonarr/ Assuming you have [poetry](https://python-poetry.org/docs/#installation) installed: ```sh cp .env.example .env -$EDITOR .env +# Update the config with your editor of choice. poetry install poetry run python3 -m al_arr_sync ``` diff --git a/al_arr_sync/__main__.py b/al_arr_sync/__main__.py index b515e96..ef39a45 100644 --- a/al_arr_sync/__main__.py +++ b/al_arr_sync/__main__.py @@ -1,21 +1,20 @@ import os -from dotenv import load_dotenv - from al_arr_sync.anilist import AniListClient from al_arr_sync.radarr import RadarrClient from al_arr_sync.sonarr import SonarrClient from al_arr_sync.types import DlAutomator - -load_dotenv() +from al_arr_sync.config import load_config def main() -> int: - al = AniListClient() - sonarr: DlAutomator = SonarrClient.from_env() - radarr: DlAutomator = RadarrClient.from_env() + cfg = load_config("config.ini") - username = os.environ["ANILIST_USERNAME"] + al = AniListClient() + sonarr: DlAutomator = SonarrClient.from_config(cfg) + radarr: DlAutomator = RadarrClient.from_config(cfg) + + username = cfg["anilist"]["username"] media = al.currently_watching(username) for entry in media: diff --git a/al_arr_sync/config.py b/al_arr_sync/config.py new file mode 100644 index 0000000..d0c2c4e --- /dev/null +++ b/al_arr_sync/config.py @@ -0,0 +1,7 @@ +import configparser + + +def load_config(path: str) -> configparser.ConfigParser: + config = configparser.ConfigParser() + config.read(path) + return config diff --git a/al_arr_sync/radarr.py b/al_arr_sync/radarr.py index fb9347e..85b2858 100644 --- a/al_arr_sync/radarr.py +++ b/al_arr_sync/radarr.py @@ -1,4 +1,5 @@ import os +import configparser import typing from urllib.parse import urljoin @@ -9,16 +10,32 @@ from al_arr_sync.types import AnyDict class RadarrClient: - def __init__(self, radarr_url: str, api_key: str) -> None: - self.radarr_url = radarr_url + def __init__( + self, api_url: str, api_key: str, folder_path: str, quality_profile: int = 4 + ) -> None: + self.api_url = api_url self.api_key = api_key + self.folder_path = folder_path + self.quality_profile = quality_profile + self.http_session = Session() @staticmethod def from_env() -> "RadarrClient": return RadarrClient( - radarr_url=os.environ["RADARR_API_URL"], - api_key=os.environ["RADARR_API_KEY"], + api_url=os.environ["SONARR_API_URL"], + api_key=os.environ["SONARR_API_KEY"], + folder_path=os.environ["SONARR_FOLDER_PATH"], + quality_profile=int(os.environ["SONARR_QUALITY_PROFILE"]), + ) + + @staticmethod + def from_config(cfg: configparser.ConfigParser) -> "RadarrClient": + return RadarrClient( + api_url=cfg["radarr"]["api_url"], + api_key=cfg["radarr"]["api_key"], + folder_path=cfg["radarr"]["folder_path"], + quality_profile=int(cfg["radarr"]["quality_profile"]), ) def _prepare_request( @@ -28,7 +45,7 @@ class RadarrClient: params: AnyDict = {}, json: typing.Optional[AnyDict] = None, ) -> PreparedRequest: - url = urljoin(self.radarr_url, endpoint) + url = urljoin(self.api_url, endpoint) headers = {"X-Api-Key": self.api_key} req = PreparedRequest() @@ -50,8 +67,8 @@ class RadarrClient: "ignoreEpisodesWithFiles": False, "ignoreEpisodesWithoutFiles": False, }, - "rootFolderPath": os.environ["RADARR_FOLDER_PATH"], - "qualityProfileId": int(os.environ["RADARR_QUALITY_PROFILE"]), + "rootFolderPath": self.folder_path, + "qualityProfileId": self.quality_profile, } ) req = self._prepare_request("/api/v3/movie", method="POST", json=payload) diff --git a/al_arr_sync/sonarr.py b/al_arr_sync/sonarr.py index 63ca713..1d0f407 100644 --- a/al_arr_sync/sonarr.py +++ b/al_arr_sync/sonarr.py @@ -1,3 +1,4 @@ +import configparser import os import typing from urllib.parse import urljoin @@ -9,16 +10,40 @@ from al_arr_sync.types import AnyDict class SonarrClient: - def __init__(self, sonarr_url: str, api_key: str) -> None: - self.sonarr_url = sonarr_url + def __init__( + self, + api_url: str, + api_key: str, + folder_path: str, + quality_profile: int = 4, + language_profile: int = 1, + ) -> None: + self.api_url = api_url self.api_key = api_key + self.folder_path = folder_path + self.quality_profile = quality_profile + self.language_profile = language_profile + self.http_session = Session() @staticmethod def from_env() -> "SonarrClient": return SonarrClient( - sonarr_url=os.environ["SONARR_API_URL"], + api_url=os.environ["SONARR_API_URL"], api_key=os.environ["SONARR_API_KEY"], + folder_path=os.environ["SONARR_FOLDER_PATH"], + quality_profile=int(os.environ["SONARR_QUALITY_PROFILE"]), + language_profile=int(os.environ["SONARR_LANGUAGE_PROFILE"]), + ) + + @staticmethod + def from_config(cfg: configparser.ConfigParser) -> "SonarrClient": + return SonarrClient( + api_url=cfg["sonarr"]["api_url"], + api_key=cfg["sonarr"]["api_key"], + folder_path=cfg["sonarr"]["folder_path"], + quality_profile=int(cfg["sonarr"]["quality_profile"]), + language_profile=int(cfg["sonarr"]["language_profile"]), ) def _prepare_request( @@ -28,7 +53,7 @@ class SonarrClient: params: AnyDict = {}, json: typing.Optional[AnyDict] = None, ) -> PreparedRequest: - url = urljoin(self.sonarr_url, endpoint) + url = urljoin(self.api_url, endpoint) headers = {"X-Api-Key": self.api_key} req = PreparedRequest() @@ -50,9 +75,9 @@ class SonarrClient: "searchForCutoffUnmetEpisodes": False, "searchForMissingEpisodes": False, }, - "rootFolderPath": os.environ["SONARR_FOLDER_PATH"], - "qualityProfileId": int(os.environ["SONARR_QUALITY_PROFILE"]), - "languageProfileId": int(os.environ["SONARR_LANGUAGE_PROFILE"]), + "rootFolderPath": self.folder_path, + "qualityProfileId": self.quality_profile, + "languageProfileId": self.language_profile, } ) req = self._prepare_request("/api/v3/series", method="POST", json=payload) diff --git a/config.ini.example b/config.ini.example new file mode 100644 index 0000000..a1a5495 --- /dev/null +++ b/config.ini.example @@ -0,0 +1,15 @@ +[anilist] +username= + +[sonarr] +api_url= +api_key= +folder_path= +quality_profile=4 +language_profile=1 + +[radarr] +api_url= +api_key= +folder_path= +quality_profile=4 diff --git a/poetry.lock b/poetry.lock index 56ada62..6188f71 100644 --- a/poetry.lock +++ b/poetry.lock @@ -249,17 +249,6 @@ wcwidth = "*" checkqa-mypy = ["mypy (==v0.761)"] testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "requests", "xmlschema"] -[[package]] -name = "python-dotenv" -version = "0.20.0" -description = "Read key-value pairs from a .env file and set them as environment variables" -category = "main" -optional = false -python-versions = ">=3.5" - -[package.extras] -cli = ["click (>=5.0)"] - [[package]] name = "pyyaml" version = "6.0" @@ -352,7 +341,7 @@ python-versions = "*" [metadata] lock-version = "1.1" python-versions = "^3.10" -content-hash = "d220af8512855de47e706e9e80c8120f420726c45563d746fb8e8349bb111c38" +content-hash = "f9fcae0bb7bb64dcd825eaba7f83945316eaa5c7a0747271ba135d028c48e275" [metadata.files] atomicwrites = [] @@ -414,10 +403,6 @@ pytest = [ {file = "pytest-5.4.3-py3-none-any.whl", hash = "sha256:5c0db86b698e8f170ba4582a492248919255fcd4c79b1ee64ace34301fb589a1"}, {file = "pytest-5.4.3.tar.gz", hash = "sha256:7979331bfcba207414f5e1263b5a0f8f521d0f457318836a7355531ed1a4c7d8"}, ] -python-dotenv = [ - {file = "python-dotenv-0.20.0.tar.gz", hash = "sha256:b7e3b04a59693c42c36f9ab1cc2acc46fa5df8c78e178fc33a8d4cd05c8d498f"}, - {file = "python_dotenv-0.20.0-py3-none-any.whl", hash = "sha256:d92a187be61fe482e4fd675b6d52200e7be63a12b724abbf931a40ce4fa92938"}, -] pyyaml = [ {file = "PyYAML-6.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:d4db7c7aef085872ef65a8fd7d6d09a14ae91f691dec3e87ee5ee0539d516f53"}, {file = "PyYAML-6.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:9df7ed3b3d2e0ecfe09e14741b857df43adb5a3ddadc919a2d94fbdf78fea53c"}, diff --git a/pyproject.toml b/pyproject.toml index be8b36c..1e2c260 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,6 @@ authors = ["strNophix "] [tool.poetry.dependencies] python = "^3.10" -python-dotenv = "^0.20.0" requests = "^2.28.1" [tool.poetry.dev-dependencies]