Use factory pattern to get instance objects
MODIFY
plex_trakt_sync/commands/clear_collections.py
plex_trakt_sync/commands/clear_collections.py
import click
+
+from plex_trakt_sync.factory import factory
from plex_trakt_sync.logging import logger
-from plex_trakt_sync.trakt_api import TraktApi
@click.command()
@@ -15,7 +16,7 @@ def clear_collections(confirm, dry_run):
click.echo('You need to pass --confirm or --dry-run option to proceed')
return
- trakt = TraktApi()
+ trakt = factory.trakt_api()
for movie in trakt.movie_collection:
logger.info(f"Deleting: {movie}")

MODIFY
plex_trakt_sync/commands/inspect.py
plex_trakt_sync/commands/inspect.py
import click
-from plexapi.server import PlexServer
-from plex_trakt_sync.config import CONFIG
-from plex_trakt_sync.plex_api import PlexApi
-from plex_trakt_sync.trakt_api import TraktApi
+from plex_trakt_sync.factory import factory
from plex_trakt_sync.version import git_version_info
@@ -16,11 +13,8 @@ def inspect(input):
git_version = git_version_info() or 'Unknown version'
print(f"PlexTraktSync inspect [{git_version}]")
- url = CONFIG["PLEX_BASEURL"]
- token = CONFIG["PLEX_TOKEN"]
- server = PlexServer(url, token)
- plex = PlexApi(server)
- trakt = TraktApi()
+ plex = factory.plex_api()
+ trakt = factory.trakt_api()
if input.isnumeric():
input = int(input)

MODIFY
plex_trakt_sync/commands/sync.py
plex_trakt_sync/commands/sync.py
from plexapi.server import PlexServer
from plex_trakt_sync.commands.login import ensure_login
-from plex_trakt_sync.media import MediaFactory, Media
+from plex_trakt_sync.factory import factory
+from plex_trakt_sync.media import Media
from plex_trakt_sync.requests_cache import requests_cache
-from plex_trakt_sync.plex_server import get_plex_server
from plex_trakt_sync.config import CONFIG
from plex_trakt_sync.decorators import measure_time
-from plex_trakt_sync.plex_api import PlexApi
from plex_trakt_sync.trakt_api import TraktApi
from plex_trakt_sync.trakt_list_util import TraktListUtil
from plex_trakt_sync.logging import logger
@@ -139,10 +138,10 @@ def sync(sync_option: str, library: str, show: str, movie: str, batch_size: int)
movies = sync_option in ["all", "movies"]
tv = sync_option in ["all", "tv"]
- server = get_plex_server()
- plex = PlexApi(server)
- trakt = TraktApi(batch_size=batch_size)
- mf = MediaFactory(plex, trakt)
+ server = factory.plex_server()
+ plex = factory.plex_api()
+ trakt = factory.trakt_api(batch_size=batch_size)
+ mf = factory.media_factory(batch_size=batch_size)
w = Walker(plex, mf, movies=movies, shows=tv, progressbar=click.progressbar)
if library:

MODIFY
plex_trakt_sync/commands/trakt_login.py
plex_trakt_sync/commands/trakt_login.py
from trakt.errors import ForbiddenException
from plex_trakt_sync.config import CONFIG
+from plex_trakt_sync.factory import factory
from plex_trakt_sync.path import pytrakt_file
from plex_trakt_sync.style import title, success, error, prompt
from plex_trakt_sync.trakt_api import TraktApi
@@ -47,7 +48,7 @@ def trakt_login():
Log in to Trakt Account to obtain Access Token.
"""
- api = TraktApi()
+ api = factory.trakt_api()
trakt_authenticate(api)
user = api.me.username

MODIFY
plex_trakt_sync/commands/watch.py
plex_trakt_sync/commands/watch.py
import click
from plexapi.server import PlexServer
+from plex_trakt_sync.factory import factory
from plex_trakt_sync.listener import WebSocketListener, PLAYING
from plex_trakt_sync.config import CONFIG
from plex_trakt_sync.plex_api import PlexApi
@@ -86,11 +87,9 @@ def watch():
Listen to events from Plex
"""
- url = CONFIG["PLEX_BASEURL"]
- token = CONFIG["PLEX_TOKEN"]
- server = PlexServer(url, token)
- trakt = TraktApi()
- plex = PlexApi(server)
+ server = factory.plex_server()
+ trakt = factory.trakt_api()
+ plex = factory.plex_api()
ws = WebSocketListener(server)
ws.on(PLAYING, WatchStateUpdater(plex, trakt))

MODIFY
plex_trakt_sync/commands/webhook.py
plex_trakt_sync/commands/webhook.py
import click
+from plex_trakt_sync.factory import factory
from plex_trakt_sync.logging import logger
from plex_trakt_sync.media import MediaFactory
from plex_trakt_sync.plex_api import PlexApi
-from plex_trakt_sync.plex_server import get_plex_server
-from plex_trakt_sync.trakt_api import TraktApi
TAUTULLI_WEBHOOK_URL = "https://github.com/Taxel/PlexTraktSync#tautulli-webhook"
@@ -100,10 +99,8 @@ def webhook(bind: str, port: int):
"""
with socketserver.TCPServer((bind, port), HttpRequestHandler, bind_and_activate=False) as httpd:
- server = get_plex_server()
- plex = PlexApi(server)
- trakt = TraktApi()
- mf = MediaFactory(server, trakt)
+ plex = factory.plex_api()
+ mf = factory.media_factory()
httpd.allow_reuse_address = True
httpd.webhook = WebhookHandler(plex, mf)

Use factory pattern to get instance objects