Moved AnyDict to seperate file
This commit is contained in:
parent
ec2471a932
commit
7b86003d9f
@ -3,6 +3,7 @@ import typing
|
|||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from al_arr_sync.anilist import AniListClient
|
from al_arr_sync.anilist import AniListClient
|
||||||
from al_arr_sync.sonarr import SonarrClient
|
from al_arr_sync.sonarr import SonarrClient
|
||||||
|
from al_arr_sync.types import AnyDict
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
@ -13,8 +14,8 @@ def main() -> int:
|
|||||||
|
|
||||||
username = os.environ["ANILIST_USERNAME"]
|
username = os.environ["ANILIST_USERNAME"]
|
||||||
media = al.currently_watching(username)
|
media = al.currently_watching(username)
|
||||||
|
|
||||||
series: typing.List[typing.Any] = []
|
series: typing.List[AnyDict] = []
|
||||||
for entry in media:
|
for entry in media:
|
||||||
media_format = entry["media"]["format"]
|
media_format = entry["media"]["format"]
|
||||||
if media_format == "TV":
|
if media_format == "TV":
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
import typing
|
import typing
|
||||||
from requests import Session
|
from requests import Session
|
||||||
|
from al_arr_sync.types import AnyDict
|
||||||
if typing.TYPE_CHECKING:
|
|
||||||
AnyDict = typing.Dict[typing.Any, typing.Any]
|
|
||||||
|
|
||||||
|
|
||||||
USER_WATCHING_QUERY = """
|
USER_WATCHING_QUERY = """
|
||||||
@ -28,12 +26,12 @@ class AniListClient:
|
|||||||
self.graphql_api_url = "https://graphql.anilist.co/"
|
self.graphql_api_url = "https://graphql.anilist.co/"
|
||||||
self.http_session = Session()
|
self.http_session = Session()
|
||||||
|
|
||||||
def currently_watching(self, username: str) -> typing.List['AnyDict']:
|
def currently_watching(self, username: str) -> typing.List[AnyDict]:
|
||||||
resp = self.http_session.post(
|
resp = self.http_session.post(
|
||||||
self.graphql_api_url,
|
self.graphql_api_url,
|
||||||
json={"query": USER_WATCHING_QUERY, "variables": {"userName": username}},
|
json={"query": USER_WATCHING_QUERY, "variables": {"userName": username}},
|
||||||
)
|
)
|
||||||
resp_data: 'AnyDict' = resp.json()
|
resp_data: AnyDict = resp.json()
|
||||||
if errors := resp_data.get("errors"):
|
if errors := resp_data.get("errors"):
|
||||||
raise Exception(errors)
|
raise Exception(errors)
|
||||||
|
|
||||||
|
@ -3,9 +3,8 @@ from requests import Session
|
|||||||
from requests import PreparedRequest
|
from requests import PreparedRequest
|
||||||
import os
|
import os
|
||||||
from urllib.parse import urljoin
|
from urllib.parse import urljoin
|
||||||
|
from al_arr_sync.types import AnyDict
|
||||||
|
|
||||||
if typing.TYPE_CHECKING:
|
|
||||||
AnyDict = typing.Dict[typing.Any, typing.Any]
|
|
||||||
|
|
||||||
class SonarrClient:
|
class SonarrClient:
|
||||||
def __init__(self, sonarr_url: str, api_key: str) -> None:
|
def __init__(self, sonarr_url: str, api_key: str) -> None:
|
||||||
@ -24,8 +23,8 @@ class SonarrClient:
|
|||||||
self,
|
self,
|
||||||
endpoint: str,
|
endpoint: str,
|
||||||
method: str = "GET",
|
method: str = "GET",
|
||||||
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.sonarr_url, endpoint)
|
||||||
headers = {"X-Api-Key": self.api_key}
|
headers = {"X-Api-Key": self.api_key}
|
||||||
@ -34,14 +33,14 @@ class SonarrClient:
|
|||||||
req.prepare(method=method, url=url, headers=headers, params=params, json=json)
|
req.prepare(method=method, url=url, headers=headers, params=params, json=json)
|
||||||
return req
|
return req
|
||||||
|
|
||||||
def lookup_series(self, title: str) -> typing.List['AnyDict']:
|
def lookup_series(self, title: str) -> typing.List[AnyDict]:
|
||||||
req = self._prepare_request("/api/v3/series/lookup", params={"term": title})
|
req = self._prepare_request("/api/v3/series/lookup", params={"term": title})
|
||||||
resp = self.http_session.send(req)
|
resp = self.http_session.send(req)
|
||||||
return resp.json()
|
return resp.json()
|
||||||
|
|
||||||
def add_series(self, *series: 'AnyDict'):
|
def add_series(self, *series: AnyDict):
|
||||||
for show in series:
|
for show in series:
|
||||||
payload: 'AnyDict' = show.copy()
|
payload: AnyDict = show.copy()
|
||||||
payload.update(
|
payload.update(
|
||||||
{
|
{
|
||||||
"addOptions": {
|
"addOptions": {
|
||||||
|
3
al_arr_sync/types.py
Normal file
3
al_arr_sync/types.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import typing
|
||||||
|
|
||||||
|
AnyDict = typing.Dict[typing.Any, typing.Any]
|
Loading…
x
Reference in New Issue
Block a user