Skip to content

Commit

Permalink
Absolute Episode Ordering (#132)
Browse files Browse the repository at this point in the history
Patch to allow for absolute episode ordering between seasons, see #131
  • Loading branch information
Jaynator495 authored May 1, 2022
1 parent a8f392c commit 603fab0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
10 changes: 7 additions & 3 deletions anilist.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,23 @@ def match_to_plex(anilist_series: List[AnilistSeries], plex_series_watched: List
# Check if we have custom mappings for all seasons (One Piece for example)
if len(plex_seasons) > 1:
custom_mapping_season_count = 0
for plex_season in plex_seasons:
for index, plex_season in enumerate(plex_seasons):
season_mappings = retrieve_season_mappings(
plex_title, plex_season.season_number
)
matched_id = 0
if season_mappings:
matched_id = season_mappings[0].anime_id
if custom_mapping_seasons_anilist_id in (0, matched_id):
plex_watched_episode_count_custom_mapping += plex_season.watched_episodes
custom_mapping_season_count += 1

custom_mapping_seasons_anilist_id = matched_id

# Check if season should be added together
if (index != 0 and plex_season.first_episode > plex_seasons[index - 1].last_episode):
plex_watched_episode_count_custom_mapping = plex_season.watched_episodes
else:
plex_watched_episode_count_custom_mapping += plex_season.watched_episodes

# If we had custom mappings for multiple seasons with the same ID use
# cumulative episode count and skip per season processing
if custom_mapping_season_count > 1:
Expand Down
16 changes: 14 additions & 2 deletions plexmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
class PlexSeason:
season_number: int
watched_episodes: int
first_episode: int
last_episode: int


@dataclass
Expand Down Expand Up @@ -173,7 +175,9 @@ def get_watched_shows(shows: List[Show]) -> Optional[List[PlexWatchedSeries]]:
seasons = []
for season in show_seasons:
season_watchcount = get_watched_episodes_for_show_season(season)
seasons.append(PlexSeason(season.seasonNumber, season_watchcount))
season_firstepisode = get_first_episode_for_show_season(season)
season_lastepisode = get_last_episode_for_show_season(season)
seasons.append(PlexSeason(season.seasonNumber, season_watchcount, season_firstepisode, season_lastepisode))

if seasons:
# Add year if we have one otherwise fallback
Expand Down Expand Up @@ -235,7 +239,7 @@ def get_watched_shows(shows: List[Show]) -> Optional[List[PlexWatchedSeries]]:
show.titleSort.strip(),
show.originalTitle.strip(),
year,
[PlexSeason(1, 1)],
[PlexSeason(1, 1, 1, 1)],
anilist_id
)
watched_series.append(watched_show)
Expand Down Expand Up @@ -264,3 +268,11 @@ def get_watched_episodes_for_show_season(season: Season) -> int:

logger.info(f'[PLEX] {episodes_watched} episodes watched for {season.parentTitle} season {season.seasonNumber}')
return episodes_watched


def get_first_episode_for_show_season(season: Season) -> int:
return season.episodes()[0].index


def get_last_episode_for_show_season(season: Season) -> int:
return season.episodes()[-1].index

0 comments on commit 603fab0

Please sign in to comment.