Moved from .env to ini config format

This commit is contained in:
strNophix 2022-07-22 14:54:57 +02:00
parent 4bf249a9fd
commit ffa8090399
10 changed files with 90 additions and 63 deletions

View File

@ -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

3
.gitignore vendored
View File

@ -158,3 +158,6 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear # 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. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
# Development
config.ini

View File

@ -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: Assuming you have [poetry](https://python-poetry.org/docs/#installation) installed:
```sh ```sh
cp .env.example .env cp .env.example .env
$EDITOR .env # Update the config with your editor of choice.
poetry install poetry install
poetry run python3 -m al_arr_sync poetry run python3 -m al_arr_sync
``` ```

View File

@ -1,21 +1,20 @@
import os import os
from dotenv import load_dotenv
from al_arr_sync.anilist import AniListClient from al_arr_sync.anilist import AniListClient
from al_arr_sync.radarr import RadarrClient from al_arr_sync.radarr import RadarrClient
from al_arr_sync.sonarr import SonarrClient from al_arr_sync.sonarr import SonarrClient
from al_arr_sync.types import DlAutomator from al_arr_sync.types import DlAutomator
from al_arr_sync.config import load_config
load_dotenv()
def main() -> int: def main() -> int:
al = AniListClient() cfg = load_config("config.ini")
sonarr: DlAutomator = SonarrClient.from_env()
radarr: DlAutomator = RadarrClient.from_env()
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) media = al.currently_watching(username)
for entry in media: for entry in media:

7
al_arr_sync/config.py Normal file
View File

@ -0,0 +1,7 @@
import configparser
def load_config(path: str) -> configparser.ConfigParser:
config = configparser.ConfigParser()
config.read(path)
return config

View File

@ -1,4 +1,5 @@
import os import os
import configparser
import typing import typing
from urllib.parse import urljoin from urllib.parse import urljoin
@ -9,16 +10,32 @@ from al_arr_sync.types import AnyDict
class RadarrClient: class RadarrClient:
def __init__(self, radarr_url: str, api_key: str) -> None: def __init__(
self.radarr_url = radarr_url 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.api_key = api_key
self.folder_path = folder_path
self.quality_profile = quality_profile
self.http_session = Session() self.http_session = Session()
@staticmethod @staticmethod
def from_env() -> "RadarrClient": def from_env() -> "RadarrClient":
return RadarrClient( return RadarrClient(
radarr_url=os.environ["RADARR_API_URL"], api_url=os.environ["SONARR_API_URL"],
api_key=os.environ["RADARR_API_KEY"], 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( def _prepare_request(
@ -28,7 +45,7 @@ class RadarrClient:
params: AnyDict = {}, params: AnyDict = {},
json: typing.Optional[AnyDict] = None, json: typing.Optional[AnyDict] = None,
) -> PreparedRequest: ) -> PreparedRequest:
url = urljoin(self.radarr_url, endpoint) url = urljoin(self.api_url, endpoint)
headers = {"X-Api-Key": self.api_key} headers = {"X-Api-Key": self.api_key}
req = PreparedRequest() req = PreparedRequest()
@ -50,8 +67,8 @@ class RadarrClient:
"ignoreEpisodesWithFiles": False, "ignoreEpisodesWithFiles": False,
"ignoreEpisodesWithoutFiles": False, "ignoreEpisodesWithoutFiles": False,
}, },
"rootFolderPath": os.environ["RADARR_FOLDER_PATH"], "rootFolderPath": self.folder_path,
"qualityProfileId": int(os.environ["RADARR_QUALITY_PROFILE"]), "qualityProfileId": self.quality_profile,
} }
) )
req = self._prepare_request("/api/v3/movie", method="POST", json=payload) req = self._prepare_request("/api/v3/movie", method="POST", json=payload)

View File

@ -1,3 +1,4 @@
import configparser
import os import os
import typing import typing
from urllib.parse import urljoin from urllib.parse import urljoin
@ -9,16 +10,40 @@ from al_arr_sync.types import AnyDict
class SonarrClient: class SonarrClient:
def __init__(self, sonarr_url: str, api_key: str) -> None: def __init__(
self.sonarr_url = sonarr_url 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.api_key = api_key
self.folder_path = folder_path
self.quality_profile = quality_profile
self.language_profile = language_profile
self.http_session = Session() self.http_session = Session()
@staticmethod @staticmethod
def from_env() -> "SonarrClient": def from_env() -> "SonarrClient":
return SonarrClient( return SonarrClient(
sonarr_url=os.environ["SONARR_API_URL"], api_url=os.environ["SONARR_API_URL"],
api_key=os.environ["SONARR_API_KEY"], 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( def _prepare_request(
@ -28,7 +53,7 @@ class SonarrClient:
params: AnyDict = {}, params: AnyDict = {},
json: typing.Optional[AnyDict] = None, json: typing.Optional[AnyDict] = None,
) -> PreparedRequest: ) -> PreparedRequest:
url = urljoin(self.sonarr_url, endpoint) url = urljoin(self.api_url, endpoint)
headers = {"X-Api-Key": self.api_key} headers = {"X-Api-Key": self.api_key}
req = PreparedRequest() req = PreparedRequest()
@ -50,9 +75,9 @@ class SonarrClient:
"searchForCutoffUnmetEpisodes": False, "searchForCutoffUnmetEpisodes": False,
"searchForMissingEpisodes": False, "searchForMissingEpisodes": False,
}, },
"rootFolderPath": os.environ["SONARR_FOLDER_PATH"], "rootFolderPath": self.folder_path,
"qualityProfileId": int(os.environ["SONARR_QUALITY_PROFILE"]), "qualityProfileId": self.quality_profile,
"languageProfileId": int(os.environ["SONARR_LANGUAGE_PROFILE"]), "languageProfileId": self.language_profile,
} }
) )
req = self._prepare_request("/api/v3/series", method="POST", json=payload) req = self._prepare_request("/api/v3/series", method="POST", json=payload)

15
config.ini.example Normal file
View File

@ -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

17
poetry.lock generated
View File

@ -249,17 +249,6 @@ wcwidth = "*"
checkqa-mypy = ["mypy (==v0.761)"] checkqa-mypy = ["mypy (==v0.761)"]
testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "requests", "xmlschema"] 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]] [[package]]
name = "pyyaml" name = "pyyaml"
version = "6.0" version = "6.0"
@ -352,7 +341,7 @@ python-versions = "*"
[metadata] [metadata]
lock-version = "1.1" lock-version = "1.1"
python-versions = "^3.10" python-versions = "^3.10"
content-hash = "d220af8512855de47e706e9e80c8120f420726c45563d746fb8e8349bb111c38" content-hash = "f9fcae0bb7bb64dcd825eaba7f83945316eaa5c7a0747271ba135d028c48e275"
[metadata.files] [metadata.files]
atomicwrites = [] atomicwrites = []
@ -414,10 +403,6 @@ pytest = [
{file = "pytest-5.4.3-py3-none-any.whl", hash = "sha256:5c0db86b698e8f170ba4582a492248919255fcd4c79b1ee64ace34301fb589a1"}, {file = "pytest-5.4.3-py3-none-any.whl", hash = "sha256:5c0db86b698e8f170ba4582a492248919255fcd4c79b1ee64ace34301fb589a1"},
{file = "pytest-5.4.3.tar.gz", hash = "sha256:7979331bfcba207414f5e1263b5a0f8f521d0f457318836a7355531ed1a4c7d8"}, {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 = [ pyyaml = [
{file = "PyYAML-6.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:d4db7c7aef085872ef65a8fd7d6d09a14ae91f691dec3e87ee5ee0539d516f53"}, {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"}, {file = "PyYAML-6.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:9df7ed3b3d2e0ecfe09e14741b857df43adb5a3ddadc919a2d94fbdf78fea53c"},

View File

@ -6,7 +6,6 @@ authors = ["strNophix <nvdpoel01@gmail.com>"]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "^3.10" python = "^3.10"
python-dotenv = "^0.20.0"
requests = "^2.28.1" requests = "^2.28.1"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]