Skip to content

Commit

Permalink
pytrakt v4 migration (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
purarue authored Feb 1, 2025
1 parent a884d4e commit 17055b1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = traktexport
version = 0.1.8
version = 0.1.9
description = Export your Movies, TV shows and ratings from Trakt.tv
long_description = file: README.md
long_description_content_type = text/markdown
Expand All @@ -27,7 +27,7 @@ install_requires =
backoff
click>=8.1
logzero
pytrakt==3.4.35
pytrakt>=4.1.0
python_requires = >=3.8
include_package_data = True

Expand Down
30 changes: 20 additions & 10 deletions traktexport/export.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import sys
import os
import logging
import shlex
from time import sleep
from typing import Dict, Any, Iterator, List, Optional, Literal
from functools import lru_cache
from urllib.parse import urljoin

import backoff # type: ignore[import]
from trakt.core import CORE, BASE_URL # type: ignore[import]
from trakt.core import api, BASE_URL # type: ignore[import]
from trakt.api import TokenAuth # type: ignore[import]
from trakt.errors import RateLimitException # type: ignore[import]
from logzero import logger # type: ignore[import]

Expand All @@ -17,11 +19,17 @@ def _check_config() -> None:

if not os.path.exists(traktexport_cfg):
raise FileNotFoundError(
f"Config file '{traktexport_cfg}' not found. Run 'traktexport auth' to create it."
f"Config file '{traktexport_cfg}' not found. Run '{shlex.quote(sys.executable)} -m traktexport auth' to create it."
)

# loads config and refreshes token if needed
CORE._bootstrap()
client = api()
auth: Optional[TokenAuth] = client.auth
if auth is None:
raise ValueError(f"No auth config found on client={client}")

# this refreshes the token, if needed
auth.get_token()


SLEEP_TIME = int(os.environ.get("TRAKTEXPORT_SLEEP_TIME", 2))
Expand All @@ -37,23 +45,25 @@ def _trakt_request(
method: Literal["get", "post", "patch"] = "get",
) -> Any:
"""
Uses CORE._handle_request (configured trakt session handled by trakt)
Uses api().request (configured trakt session handled by trakt)
to request information from Trakt
This uses the bare CORE._handle_request instead of the wrapper
This uses the bare base request instead of the wrapper
types so that I have access to more info
the trakt module here is used for authentication, I just
create the URLs/save the entire response
endpoint: The URL to make a request to, doesn't include the domain
method is lowercase because _handle_request expects it to be
method is lowercase because api().request expects it to be a relative URI
"""
_check_config()
url = urljoin(BASE_URL, endpoint)
if endpoint.startswith("/"):
endpoint = endpoint[1:]
if logger:
logger.debug(f"Requesting '{url}'...")
json_data = CORE._handle_request(method=method, url=url, data=data)
logger.debug(f"Requesting '{BASE_URL}{endpoint}'...")
# api is lru_cache(maxsize=None), returns the globally configured client
json_data = api().request(method=method, url=endpoint, data=data)
if sleep_time:
sleep(sleep_time)
return json_data
Expand Down

0 comments on commit 17055b1

Please sign in to comment.