Exemplo n.º 1
0
        private static void PopulateVideoEpisodeFromAnimeEpisode(Video v, AnimeEpisode ep, int userid)
        {
            AniDB_Episode aep = ep.AniDB_Episode;

            if (aep != null)
            {
                v.EpNumber      = aep.EpisodeNumber;
                v.Index         = aep.EpisodeNumber.ToString();
                v.Title         = aep.EnglishName;
                v.OriginalTitle = aep.RomajiName;
                v.Rating        = (float.Parse(aep.Rating, CultureInfo.InvariantCulture)).ToString(CultureInfo.InvariantCulture);
                if (aep.AirDateAsDate.HasValue)
                {
                    v.Year = aep.AirDateAsDate.Value.Year.ToString();
                    v.OriginallyAvailableAt = aep.AirDateAsDate.Value.ToPlexDate();
                }
                AnimeEpisode_User epuser = ep.GetUserRecord(userid);
                if (epuser != null)
                {
                    v.ViewCount = epuser.WatchedCount.ToString();
                    if (epuser.WatchedDate.HasValue)
                    {
                        v.LastViewedAt = epuser.WatchedDate.Value.ToUnixTime();
                    }
                }
                MetroContract_Anime_Episode contract = new MetroContract_Anime_Episode();
                JMMServiceImplementationMetro.SetTvDBInfo(aep.AnimeID, aep, ref contract);
                v.Thumb   = contract.GenPoster();
                v.Summary = contract.EpisodeOverview;
            }
        }
        public override void ProcessCommand()
        {
            logger.Info("Processing CommandRequest_MALDownloadStatusFromMAL");

            try
            {
                if (string.IsNullOrEmpty(ServerSettings.MAL_Username) || string.IsNullOrEmpty(ServerSettings.MAL_Password))
                {
                    return;
                }

                // find the latest eps to update
                AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository();

                myanimelist mal = MALHelper.GetMALAnimeList();
                if (mal == null)
                {
                    return;
                }
                if (mal.anime == null)
                {
                    return;
                }

                CrossRef_AniDB_MALRepository repCrossRef = new CrossRef_AniDB_MALRepository();
                AniDB_EpisodeRepository      repAniEps   = new AniDB_EpisodeRepository();
                AnimeEpisodeRepository       repEp       = new AnimeEpisodeRepository();

                // find the anidb user
                JMMUserRepository repUsers   = new JMMUserRepository();
                List <JMMUser>    aniDBUsers = repUsers.GetAniDBUsers();
                if (aniDBUsers.Count == 0)
                {
                    return;
                }

                JMMUser user = aniDBUsers[0];


                foreach (myanimelistAnime malAnime in mal.anime)
                {
                    // look up the anime
                    CrossRef_AniDB_MAL xref = repCrossRef.GetByMALID(malAnime.series_animedb_id);
                    if (xref == null)
                    {
                        continue;
                    }

                    if (malAnime.series_animedb_id == 8107 || malAnime.series_animedb_id == 10737)
                    {
                        Console.Write("");
                    }

                    // check if this anime has any other links
                    List <CrossRef_AniDB_MAL> allXrefs = repCrossRef.GetByAnimeID(xref.AnimeID);
                    if (allXrefs.Count == 0)
                    {
                        continue;
                    }

                    // find the range of watched episodes that this applies to
                    int startEpNumber = xref.StartEpisodeNumber;
                    int endEpNumber   = GetUpperEpisodeLimit(allXrefs, xref);

                    List <AniDB_Episode> aniEps = repAniEps.GetByAnimeID(xref.AnimeID);
                    foreach (AniDB_Episode aniep in aniEps)
                    {
                        if (aniep.EpisodeType != xref.StartEpisodeType)
                        {
                            continue;
                        }

                        AnimeEpisode ep = repEp.GetByAniDBEpisodeID(aniep.EpisodeID);
                        if (ep == null)
                        {
                            continue;
                        }

                        int adjustedWatchedEps = malAnime.my_watched_episodes + xref.StartEpisodeNumber - 1;
                        int epNum = aniep.EpisodeNumber;

                        if (epNum < startEpNumber || epNum > endEpNumber)
                        {
                            continue;
                        }

                        AnimeEpisode_User usrRec = ep.GetUserRecord(user.JMMUserID);

                        if (epNum <= adjustedWatchedEps)
                        {
                            // update if the user doesn't have a record (means not watched)
                            // or it is currently un-watched
                            bool update = false;
                            if (usrRec == null)
                            {
                                update = true;
                            }
                            else
                            {
                                if (!usrRec.WatchedDate.HasValue)
                                {
                                    update = true;
                                }
                            }

                            if (update)
                            {
                                ep.ToggleWatchedStatus(true, true, DateTime.Now, user.JMMUserID, false);
                            }
                        }
                        else
                        {
                            bool update = false;
                            if (usrRec != null)
                            {
                                if (usrRec.WatchedDate.HasValue)
                                {
                                    update = true;
                                }
                            }

                            if (update)
                            {
                                ep.ToggleWatchedStatus(false, true, DateTime.Now, user.JMMUserID, false);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error processing CommandRequest_MALDownloadStatusFromMAL: {0}", ex.ToString());
                return;
            }
        }
        public override void ProcessCommand()
        {
            logger.Info("Processing CommandRequest_AddFileToMyList: {0}", Hash);


            try
            {
                vid = RepoFactory.VideoLocal.GetByHash(this.Hash);
                List <AnimeEpisode> animeEpisodes = new List <AnimeEpisode>();
                if (vid != null)
                {
                    animeEpisodes = vid.GetAnimeEpisodes();
                }

                if (vid != null)
                {
                    // when adding a file via the API, newWatchedStatus will return with current watched status on AniDB
                    // if the file is already on the user's list

                    bool isManualLink = false;
                    List <CrossRef_File_Episode> xrefs = vid.EpisodeCrossRefs;
                    if (xrefs.Count > 0)
                    {
                        isManualLink = xrefs[0].CrossRefSource != (int)CrossRefSource.AniDB;
                    }

                    // mark the video file as watched
                    DateTime?watchedDate      = null;
                    bool     newWatchedStatus = false;

                    if (isManualLink)
                    {
                        newWatchedStatus = JMMService.AnidbProcessor.AddFileToMyList(xrefs[0].AnimeID,
                                                                                     xrefs[0].Episode.EpisodeNumber,
                                                                                     ref watchedDate);
                    }
                    else
                    {
                        newWatchedStatus = JMMService.AnidbProcessor.AddFileToMyList(vid, ref watchedDate);
                    }

                    // do for all AniDB users
                    List <JMMUser> aniDBUsers = RepoFactory.JMMUser.GetAniDBUsers();


                    if (aniDBUsers.Count > 0)
                    {
                        JMMUser juser = aniDBUsers[0];
                        vid.ToggleWatchedStatus(newWatchedStatus, false, watchedDate, false, false, juser.JMMUserID,
                                                false, true);
                        logger.Info("Adding file to list: {0} - {1}", vid.ToString(), watchedDate);

                        // if the the episode is watched we may want to set the file to watched as well
                        if (ServerSettings.Import_UseExistingFileWatchedStatus && !newWatchedStatus)
                        {
                            if (animeEpisodes.Count > 0)
                            {
                                AnimeEpisode      ep     = animeEpisodes[0];
                                AnimeEpisode_User epUser = null;

                                foreach (JMMUser tempuser in aniDBUsers)
                                {
                                    // only find the first user who watched this
                                    if (epUser == null)
                                    {
                                        epUser = ep.GetUserRecord(tempuser.JMMUserID);
                                    }
                                }

                                if (epUser != null)
                                {
                                    logger.Info(
                                        "Setting file as watched, because episode was already watched: {0} - user: {1}",
                                        vid.ToString(),
                                        juser.Username);
                                    vid.ToggleWatchedStatus(true, true, epUser.WatchedDate, false, false,
                                                            epUser.JMMUserID, false, true);
                                }
                            }
                        }
                    }

                    AnimeSeries ser = animeEpisodes[0].GetAnimeSeries();
                    // all the eps should belong to the same anime
                    ser.QueueUpdateStats();
                    //StatsCache.Instance.UpdateUsingSeries(ser.AnimeSeriesID);

                    // lets also try adding to the users trakt collecion
                    if (ser != null && ServerSettings.Trakt_IsEnabled &&
                        !string.IsNullOrEmpty(ServerSettings.Trakt_AuthToken))
                    {
                        foreach (AnimeEpisode aep in animeEpisodes)
                        {
                            CommandRequest_TraktCollectionEpisode cmdSyncTrakt = new CommandRequest_TraktCollectionEpisode
                                                                                 (
                                aep.AnimeEpisodeID, TraktSyncAction.Add);
                            cmdSyncTrakt.Save();
                        }
                    }

                    // sync the series on MAL
                    if (ser != null && !string.IsNullOrEmpty(ServerSettings.MAL_Username) &&
                        !string.IsNullOrEmpty(ServerSettings.MAL_Password))
                    {
                        CommandRequest_MALUpdatedWatchedStatus cmdMAL =
                            new CommandRequest_MALUpdatedWatchedStatus(ser.AniDB_ID);
                        cmdMAL.Save();
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error processing CommandRequest_AddFileToMyList: {0} - {1}", Hash, ex.ToString());
                return;
            }
        }
Exemplo n.º 4
0
        private static void PopulateVideoEpisodeFromAnimeEpisode(Video v, AnimeEpisode ep, int userid)
        {
            AniDB_Episode aep = ep.AniDB_Episode;

            if (aep != null)
            {
                v.JMMEpisodeId  = ep.AnimeEpisodeID;
                v.EpNumber      = aep.EpisodeNumber;
                v.Index         = aep.EpisodeNumber.ToString();
                v.Title         = aep.EnglishName;
                v.OriginalTitle = aep.RomajiName;
                v.Rating        = (Convert.ToDouble(aep.Rating)).ToString(CultureInfo.InvariantCulture);
                v.Votes         = aep.Votes;
                if (aep.AirDateAsDate.HasValue)
                {
                    v.Year = aep.AirDateAsDate.Value.Year.ToString();
                    v.OriginallyAvailableAt = aep.AirDateAsDate.Value.Year.ToString("0000") + "-" +
                                              aep.AirDateAsDate.Value.Month.ToString("00") +
                                              "-" + aep.AirDateAsDate.Value.Day.ToString("00");
                }
                AnimeEpisode_User epuser = ep.GetUserRecord(userid);
                if (epuser != null)
                {
                    v.ViewCount = epuser.WatchedCount.ToString();
                }
                MetroContract_Anime_Episode contract = new MetroContract_Anime_Episode();
                JMMServiceImplementationMetro.SetTvDBInfo(aep.AnimeID, aep, ref contract);
                if (contract.ImageID != 0)
                {
                    v.Thumb = ServerUrl(int.Parse(ServerSettings.JMMServerPort), MainWindow.PathAddressREST + "/GetImage/" + contract.ImageType + "/" + contract.ImageID);
                }
                else
                {
                    v.Thumb = ServerUrl(int.Parse(ServerSettings.JMMServerPort), MainWindow.PathAddressKodi + "/GetSupportImage/plex_404.png");
                }
                v.Summary = contract.EpisodeOverview;

                //community support

                //CrossRef_AniDB_TraktV2Repository repCrossRef = new CrossRef_AniDB_TraktV2Repository();
                //List<CrossRef_AniDB_TraktV2> Trakt = repCrossRef.GetByAnimeID(aep.AnimeID);
                //if (Trakt != null)
                //{
                //    if (Trakt.Count > 0)
                //    {
                //        v.Trakt = Trakt[0].TraktID;
                //    }
                //}

                //CrossRef_AniDB_TvDBV2Repository repCrossRefV2 = new CrossRef_AniDB_TvDBV2Repository();
                //List<CrossRef_AniDB_TvDBV2> TvDB = repCrossRefV2.GetByAnimeID(aep.AnimeID);
                //if (TvDB != null)
                //{
                //    if (TvDB.Count > 0)
                //    {
                //        v.TvDB = TvDB[0].TvDBID.ToString();
                //    }
                //}

                //community support END
            }
        }
        public override void ProcessCommand()
        {
            logger.Info("Processing CommandRequest_AddFileToMyList: {0}", Hash);


            try
            {
                VideoLocalRepository   repVids     = new VideoLocalRepository();
                AnimeEpisodeRepository repEpisodes = new AnimeEpisodeRepository();

                vid = repVids.GetByHash(this.Hash);
                if (vid != null)
                {
                    // when adding a file via the API, newWatchedStatus will return with current watched status on AniDB
                    // if the file is already on the user's list

                    bool isManualLink = false;
                    List <CrossRef_File_Episode> xrefs = vid.EpisodeCrossRefs;
                    if (xrefs.Count > 0)
                    {
                        isManualLink = xrefs[0].CrossRefSource != (int)CrossRefSource.AniDB;
                    }

                    // mark the video file as watched
                    DateTime?watchedDate      = null;
                    bool     newWatchedStatus = false;

                    if (isManualLink)
                    {
                        newWatchedStatus = JMMService.AnidbProcessor.AddFileToMyList(xrefs[0].AnimeID, xrefs[0].Episode.EpisodeNumber, ref watchedDate);
                    }
                    else
                    {
                        newWatchedStatus = JMMService.AnidbProcessor.AddFileToMyList(vid, ref watchedDate);
                    }

                    // do for all AniDB users
                    JMMUserRepository repUsers   = new JMMUserRepository();
                    List <JMMUser>    aniDBUsers = repUsers.GetAniDBUsers();

                    List <AnimeEpisode> animeEpisodes = vid.GetAnimeEpisodes();
                    if (aniDBUsers.Count > 0)
                    {
                        JMMUser juser = aniDBUsers[0];
                        vid.ToggleWatchedStatus(newWatchedStatus, false, watchedDate, false, false, juser.JMMUserID, false, true);
                        logger.Info("Adding file to list: {0} - {1}", vid.ToString(), watchedDate);

                        // if the the episode is watched we may want to set the file to watched as well
                        if (ServerSettings.Import_UseExistingFileWatchedStatus && !newWatchedStatus)
                        {
                            if (animeEpisodes.Count > 0)
                            {
                                AnimeEpisode      ep     = animeEpisodes[0];
                                AnimeEpisode_User epUser = null;

                                foreach (JMMUser tempuser in aniDBUsers)
                                {
                                    // only find the first user who watched this
                                    if (epUser == null)
                                    {
                                        epUser = ep.GetUserRecord(tempuser.JMMUserID);
                                    }
                                }

                                if (epUser != null)
                                {
                                    logger.Info("Setting file as watched, because episode was already watched: {0} - user: {1}", vid.ToString(), juser.Username);
                                    vid.ToggleWatchedStatus(true, true, epUser.WatchedDate, false, false, epUser.JMMUserID, false, true);
                                }
                            }
                        }
                    }

                    AnimeSeries ser = animeEpisodes[0].GetAnimeSeries();
                    // all the eps should belong to the same anime
                    ser.UpdateStats(true, true, true);
                    StatsCache.Instance.UpdateUsingSeries(ser.AnimeSeriesID);
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error processing CommandRequest_AddFileToMyList: {0} - {1}", Hash, ex.ToString());
                return;
            }
        }