Skip to content

Commit

Permalink
Merge pull request #1342 from glensc/cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc authored Jan 9, 2023
2 parents f08d234 + 1f74fe0 commit 8b70dd9
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 35 deletions.
13 changes: 9 additions & 4 deletions plextraktsync/commands/watch.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import annotations

from collections import UserDict
from typing import TYPE_CHECKING

from plextraktsync.decorators.cached_property import cached_property
from plextraktsync.events import (ActivityNotification, Error,
PlaySessionStateNotification, TimelineEntry)
from plextraktsync.factory import factory, logging
from plextraktsync.trakt.ScrobblerProxy import ScrobblerProxy

if TYPE_CHECKING:
from typing import Union
Expand All @@ -20,15 +22,18 @@
from plextraktsync.trakt.TraktApi import TraktApi


class ScrobblerCollection(dict):
class ScrobblerCollection(UserDict):
def __init__(self, trakt: TraktApi, threshold=80):
super().__init__()
self.trakt = trakt
self.threshold = threshold

def __missing__(self, key: Union[Movie, TVEpisode]):
self[key] = value = self.trakt.scrobbler(key, self.threshold)
return value
def __missing__(self, media: Union[Movie, TVEpisode]):
scrobbler = media.scrobble(0, None, None)
proxy = ScrobblerProxy(scrobbler, self.threshold)
self[media] = proxy

return proxy


class SessionCollection(dict):
Expand Down
15 changes: 14 additions & 1 deletion plextraktsync/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
from plextraktsync.plex.PlexLibraryItem import PlexLibraryItem
from plextraktsync.trakt.TraktApi import TraktApi
from plextraktsync.trakt.TraktItem import TraktItem
from plextraktsync.trakt.types import TraktMedia


class Media:
"""
Class containing Plex and Trakt media items (Movie, Episode)
"""
trakt: TraktMedia
plex: PlexLibraryItem

def __init__(
Expand All @@ -45,6 +47,14 @@ def __init__(
def media_type(self):
return self.trakt.media_type

@cached_property
def type(self):
"""
Return "movie", "show", "season", "episode"
"""
# NB: TVSeason does not have "media_type" property
return self.trakt.media_type[:-1]

@property
def season_number(self):
return self.trakt.season
Expand Down Expand Up @@ -202,7 +212,10 @@ def plex_history(self, **kwargs):
return self.plex_api.history(self.plex.item, **kwargs)

def __str__(self):
return str(self.plex)
if self.plex:
return str(self.plex)

return str(self.trakt)


class MediaFactory:
Expand Down
6 changes: 0 additions & 6 deletions plextraktsync/plex_api.py

This file was deleted.

15 changes: 4 additions & 11 deletions plextraktsync/trakt/TraktApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from plextraktsync.decorators.time_limit import time_limit
from plextraktsync.factory import factory, logger
from plextraktsync.path import pytrakt_file
from plextraktsync.trakt.ScrobblerProxy import ScrobblerProxy
from plextraktsync.trakt.TraktLookup import TraktLookup
from plextraktsync.trakt.TraktRatingCollection import TraktRatingCollection
from plextraktsync.trakt.types import TraktMedia
Expand All @@ -25,7 +24,7 @@
from typing import Optional, Union

from trakt.movies import Movie
from trakt.tv import TVEpisode, TVShow
from trakt.tv import TVShow

from plextraktsync.plex.PlexGuid import PlexGuid
from plextraktsync.plex.PlexLibraryItem import PlexLibraryItem
Expand Down Expand Up @@ -147,12 +146,11 @@ def rating(self, m) -> Optional[int]:
However, the Movie property is always None.
So fetch for all types.
"""
if m.media_type in ["movies", "shows", "episodes"]:
r = self.ratings[m.media_type]
return r.get(m.trakt, None)
else:
if m.media_type not in ["movies", "shows", "episodes"]:
raise ValueError(f"Unsupported type: {m.media_type}")

return self.ratings[m.media_type].get(m.trakt, None)

@rate_limit()
@retry()
def get_ratings(self, media_type: str):
Expand All @@ -164,11 +162,6 @@ def get_ratings(self, media_type: str):
def rate(self, m, rating):
m.rate(rating)

@staticmethod
def scrobbler(media: Union[Movie, TVEpisode], threshold=80) -> ScrobblerProxy:
scrobbler = media.scrobble(0, None, None)
return ScrobblerProxy(scrobbler, threshold)

@rate_limit()
@time_limit()
@retry()
Expand Down
6 changes: 0 additions & 6 deletions plextraktsync/trakt_api.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from unittest.mock import Mock

from plextraktsync.timer import Timer
from plextraktsync.trakt_api import TraktBatch
from plextraktsync.trakt.TraktBatch import TraktBatch
from tests.conftest import factory, load_mock

trakt = factory.trakt_api
Expand Down
2 changes: 1 addition & 1 deletion tests/test_collection_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from plextraktsync.plex_api import PlexLibraryItem
from plextraktsync.plex.PlexLibraryItem import PlexLibraryItem
from tests.conftest import make

testdata = [
Expand Down
2 changes: 1 addition & 1 deletion tests/test_new_agent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3 -m pytest
from plextraktsync.plex_api import PlexLibraryItem
from plextraktsync.plex.PlexLibraryItem import PlexLibraryItem
from tests.conftest import factory, make

trakt = factory.trakt_api
Expand Down
2 changes: 1 addition & 1 deletion tests/test_trakt_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from trakt.tv import TVShow

from plextraktsync.pytrakt_extensions import ShowProgress
from plextraktsync.trakt_api import TraktApi
from plextraktsync.trakt.TraktApi import TraktApi
from tests.conftest import factory

trakt: TraktApi = factory.trakt_api
Expand Down
5 changes: 3 additions & 2 deletions tests/test_tv_lookup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env python3 -m pytest
from trakt.tv import TVShow

from plextraktsync.plex_api import PlexGuid, PlexLibraryItem
from plextraktsync.trakt_api import TraktLookup
from plextraktsync.plex.PlexGuid import PlexGuid
from plextraktsync.plex.PlexLibraryItem import PlexLibraryItem
from plextraktsync.trakt.TraktLookup import TraktLookup
from tests.conftest import factory, make

trakt = factory.trakt_api
Expand Down
3 changes: 2 additions & 1 deletion tests/test_walker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3 -m pytest
from plextraktsync.plex_api import PlexApi, PlexLibrarySection
from plextraktsync.plex.PlexApi import PlexApi
from plextraktsync.plex.PlexLibrarySection import PlexLibrarySection
from plextraktsync.walker import WalkConfig, WalkPlanner


Expand Down

0 comments on commit 8b70dd9

Please sign in to comment.