Exemplo n.º 1
0
def update_episode_incremental(series: AnilistSeries,
                               watched_episode_count: int,
                               anilist_episodes_watched: int, new_status: str):
    # calculate episode difference and iterate up so activity stream lists
    # episodes watched if episode difference exceeds 32 only update most
    # recent as otherwise will flood the notification feed
    episode_difference = watched_episode_count - anilist_episodes_watched
    if episode_difference > 32:
        update_series(series.anilist_id, watched_episode_count, new_status)
    else:
        for current_episodes_watched in range(anilist_episodes_watched + 1,
                                              watched_episode_count + 1):
            update_series(series.anilist_id, current_episodes_watched,
                          new_status)
Exemplo n.º 2
0
def update_entry(title: str, year: int, watched_episode_count: int,
                 matched_anilist_series: List[AnilistSeries],
                 ignore_year: bool):
    for series in matched_anilist_series:
        status = ""
        logger.info(f"[ANILIST] Found AniList entry for Plex title: {title}")
        if hasattr(series, "status"):
            status = series.status
        if status == "COMPLETED":
            logger.info(
                "[ANILIST] Series is already marked as completed on AniList so skipping update"
            )
            return

        if hasattr(series, "started_year") and year != series.started_year:
            if ignore_year is False:
                logger.error(
                    f"[ANILIST] Series year did not match (skipping update) => Plex has {year} and AniList has {series.started_year}"
                )
                continue
            elif ignore_year is True:
                logger.info(
                    f"[ANILIST] Series year did not match however skip year check was given so adding anyway => "
                    f"Plex has {year} and AniList has {series.started_year}")

        anilist_total_episodes = 0
        anilist_episodes_watched = 0
        anilist_media_status = ""

        if hasattr(series, "media_status"):
            anilist_media_status = series.media_status
        if hasattr(series, "episodes"):
            if series.episodes is not None:
                try:
                    anilist_total_episodes = int(series.episodes)
                except BaseException:
                    logger.error(
                        "Series has unknown total total episodes on AniList "
                        "(not an Integer), will most likely not match up properly"
                    )
                    anilist_total_episodes = 0
            else:
                logger.error(
                    "Series has no total episodes which is normal for shows "
                    "with undetermined end-date otherwise can be invalid info "
                    "on AniList (NoneType), using Plex watched count as fallback"
                )
                anilist_total_episodes = watched_episode_count
        if hasattr(series, "progress"):
            try:
                anilist_episodes_watched = int(series.progress)
            except BaseException:
                pass

        if (watched_episode_count >= anilist_total_episodes > 0
                and anilist_media_status == "FINISHED"):
            # series completed watched
            logger.warning(
                f"[ANILIST] Plex episode watch count [{watched_episode_count}] was higher than the "
                f"one on AniList total episodes for that series [{anilist_total_episodes}] | updating "
                "AniList entry to completed")

            # calculate episode difference and iterate up so activity stream
            # lists episodes watched if episode difference exceeds 32 only
            # update most recent as otherwise will flood the notification feed
            episode_difference = watched_episode_count - anilist_episodes_watched
            if episode_difference == 1 or episode_difference > 32:
                update_series(series.anilist_id, watched_episode_count,
                              "COMPLETED")
            else:
                for current_episodes_watched in range(
                        anilist_episodes_watched + 1,
                        watched_episode_count + 1):
                    update_series(series.anilist_id, current_episodes_watched,
                                  "COMPLETED")
            return
        elif (watched_episode_count > anilist_episodes_watched
              and anilist_total_episodes > 0):
            # episode watch count higher than plex
            logger.warning(
                f"[ANILIST] Plex episode watch count [{watched_episode_count}] was higher than the one"
                f" on AniList [{anilist_episodes_watched}] which has total of {anilist_total_episodes} "
                "episodes | updating AniList entry to currently watching")

            # calculate episode difference and iterate up so activity stream lists
            # episodes watched if episode difference exceeds 32 only update most
            # recent as otherwise will flood the notification feed
            episode_difference = watched_episode_count - anilist_episodes_watched
            if episode_difference == 1 or episode_difference > 32:
                update_series(series.anilist_id, watched_episode_count,
                              "CURRENT")
            else:
                for current_episodes_watched in range(
                        anilist_episodes_watched + 1,
                        watched_episode_count + 1):
                    update_series(series.anilist_id, current_episodes_watched,
                                  "CURRENT")
            return

        elif watched_episode_count == anilist_episodes_watched:
            logger.info(
                "[ANILIST] Episodes watched was the same on AniList and Plex so skipping update"
            )
            return
        elif (anilist_episodes_watched > watched_episode_count
              and ANILIST_PLEX_EPISODE_COUNT_PRIORITY):
            if watched_episode_count > 0:
                logger.info(
                    f"[ANILIST] Episodes watched was higher on AniList [{anilist_episodes_watched}] than on Plex [{watched_episode_count}] "
                    "however Plex episode count override is active so updating"
                )

                # Since AniList episode count is higher we don't loop thru
                # updating the notification feed and just set the AniList
                # episode count once
                update_series(series.anilist_id, watched_episode_count,
                              "CURRENT")
                return
            else:
                logger.info(
                    f"[ANILIST] Episodes watched was higher on AniList [{anilist_episodes_watched}] than "
                    f"on Plex [{watched_episode_count}] with Plex episode count override active however "
                    "Plex watched count is 0 so skipping update")
        elif anilist_episodes_watched > watched_episode_count:
            logger.info(
                f"[ANILIST] Episodes watched was higher on AniList [{anilist_episodes_watched}] than on Plex [{watched_episode_count}] so skipping update"
            )
        elif anilist_total_episodes <= 0:
            logger.info(
                "[ANILIST] AniList total episodes was 0 so most likely invalid data"
            )