Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion plextraktsync/trakt_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,11 +546,28 @@ def from_guid(self, guid: PlexGuid):
Find Trakt Episode from Guid of Plex Episode
"""
te = self.from_number(guid.pm.season_number, guid.pm.episode_number)
if not te or (str(te.ids.get(guid.provider)) != guid.id and not guid.pm.is_legacy_agent):
if self.invalid_match(guid, te):
te = self.from_id(guid.provider, guid.id)

return te

@staticmethod
def invalid_match(guid: PlexGuid, episode: Optional[TVEpisode]) -> bool:
"""
Checks if guid and episode don't match by comparing trakt provided id
"""

if not episode:
# nothing to compare with
return True
if guid.pm.is_legacy_agent:
# check can not be performed
return False
id_from_trakt = getattr(episode, guid.provider, None)
if str(id_from_trakt) != guid.id:
return True
return False

def from_number(self, season, number):
try:
ep = self.table[season][number]
Expand Down
70 changes: 61 additions & 9 deletions tests/test_tv_lookup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3 -m pytest
from trakt.tv import TVShow

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

Expand Down Expand Up @@ -39,9 +39,64 @@ def test_show_episodes():
episodes = seasons[1].episodes
assert episodes[0].title == "Winter Is Coming"


def test_show_episodes_attack_on_titan():
show = TVShow("Attack on Titan")

assert len(show.seasons) > 0
seasons = show.seasons
episodes = seasons[1].episodes
assert episodes[0].title == "To You, in 2000 Years: The Fall of Shiganshina (1)"

pe = PlexLibraryItem(
make(
cls="Episode",
guid="imdb://tt2825724",
type="episode",
seasonNumber=1,
index=1,
)
)

lookup = TraktLookup(show)
guid = pe.guids[0]
te = trakt.find_episode_guid(guid, lookup)
assert te.season == 1
assert te.episode == 1
assert te.imdb == "tt2825724"


def test_show_episodes_attack_on_titan_new_agent():
show = TVShow("Attack on Titan")

assert len(show.seasons) > 0
seasons = show.seasons
assert len(seasons) == 9
assert seasons[1].episodes[0].title == "Winter Is Coming"
episodes = seasons[1].episodes
assert episodes[0].title == "To You, in 2000 Years: The Fall of Shiganshina (1)"

guid = PlexGuid(
"imdb://tt2825724",
"episode",
)
pe = PlexLibraryItem(
make(
cls="Episode",
guid="plex://foo",
guids=[
guid,
],
type="episode",
seasonNumber=1,
index=1,
)
)

lookup = TraktLookup(show)
guid = pe.guids[0]
te = trakt.find_episode_guid(guid, lookup)
assert te.season == 1
assert te.episode == 1
assert te.imdb == "tt2825724"


def test_tv_lookup_by_episode_id():
Expand All @@ -62,11 +117,7 @@ def test_tv_lookup_by_episode_id():


def test_find_episode():
tm = make(
cls="TVShow",
# trakt=4965066,
trakt=176447,
)
show = TVShow("Frank of Ireland")

pe = PlexLibraryItem(
make(
Expand All @@ -79,7 +130,7 @@ def test_find_episode():
)

guid = pe.guids[0]
lookup = trakt.lookup(tm)
lookup = TraktLookup(show)
te = trakt.find_episode_guid(guid, lookup)
assert te.season == 1
assert te.episode == 1
Expand All @@ -88,3 +139,4 @@ def test_find_episode():

if __name__ == '__main__':
test_show_episodes()
test_show_episodes_attack_on_titan()