public override void ProcessCommand()
        {
            logger.Info("Processing CommandRequest_GetCalendar");

            try
            {
                // we will always assume that an anime was downloaded via http first
                ScheduledUpdateRepository repSched = new ScheduledUpdateRepository();
                AniDB_AnimeRepository     repAnime = new AniDB_AnimeRepository();

                ScheduledUpdate sched = repSched.GetByUpdateType((int)ScheduledUpdateType.AniDBCalendar);
                if (sched == null)
                {
                    sched               = new ScheduledUpdate();
                    sched.UpdateType    = (int)ScheduledUpdateType.AniDBCalendar;
                    sched.UpdateDetails = "";
                }
                else
                {
                    int freqHours = Utils.GetScheduledHours(ServerSettings.AniDB_Calendar_UpdateFrequency);

                    // if we have run this in the last 12 hours and are not forcing it, then exit
                    TimeSpan tsLastRun = DateTime.Now - sched.LastUpdate;
                    if (tsLastRun.TotalHours < freqHours)
                    {
                        if (!ForceRefresh)
                        {
                            return;
                        }
                    }
                }

                sched.LastUpdate = DateTime.Now;
                repSched.Save(sched);

                CalendarCollection colCalendars = JMMService.AnidbProcessor.GetCalendarUDP();
                if (colCalendars == null || colCalendars.Calendars == null)
                {
                    logger.Error("Could not get calendar from AniDB");
                    return;
                }
                foreach (AniDBAPI.Calendar cal in colCalendars.Calendars)
                {
                    AniDB_Anime anime = repAnime.GetByAnimeID(cal.AnimeID);
                    if (anime != null)
                    {
                        // don't update if the local data is less 2 days old
                        TimeSpan ts = DateTime.Now - anime.DateTimeUpdated;
                        if (ts.TotalDays >= 2)
                        {
                            CommandRequest_GetAnimeHTTP cmdAnime = new CommandRequest_GetAnimeHTTP(cal.AnimeID, true,
                                                                                                   false);
                            cmdAnime.Save();
                        }
                        else
                        {
                            // update the release date even if we don't update the anime record
                            if (anime.AirDate != cal.ReleaseDate)
                            {
                                anime.AirDate = cal.ReleaseDate;
                                repAnime.Save(anime);
                                AnimeSeriesRepository srepo = new AnimeSeriesRepository();
                                AnimeSeries           ser   = srepo.GetByAnimeID(anime.AnimeID);
                                if (ser != null)
                                {
                                    srepo.Save(ser, true, false);
                                }
                            }
                        }
                    }
                    else
                    {
                        CommandRequest_GetAnimeHTTP cmdAnime = new CommandRequest_GetAnimeHTTP(cal.AnimeID, true, false);
                        cmdAnime.Save();
                    }
                }
            }
            catch (Exception ex)
            {
                logger.ErrorException("Error processing CommandRequest_GetCalendar: " + ex.ToString(), ex);
                return;
            }
        }
Example #2
0
        public GroupStatusCollection GetReleaseGroupStatusUDP(int animeID)
        {
            if (!Login())
            {
                return(null);
            }

            enHelperActivityType        ev     = enHelperActivityType.NoSuchCreator;
            AniDBCommand_GetGroupStatus getCmd = null;

            lock (lockAniDBConnections)
            {
                Pause();

                getCmd = new AniDBCommand_GetGroupStatus();
                getCmd.Init(animeID);
                SetWaitingOnResponse(true);
                ev = getCmd.Process(ref soUdp, ref remoteIpEndPoint, curSessionID, new UnicodeEncoding(true, false));
                SetWaitingOnResponse(false);
            }

            if (ev == enHelperActivityType.GotGroupStatus && getCmd.GrpStatusCollection != null)
            {
                // delete existing records
                AniDB_GroupStatusRepository repGrpStat = new AniDB_GroupStatusRepository();
                AniDB_AnimeRepository       repAnime   = new AniDB_AnimeRepository();
                AniDB_EpisodeRepository     repAniEp   = new AniDB_EpisodeRepository();
                AnimeSeriesRepository       repSeries  = new AnimeSeriesRepository();

                repGrpStat.DeleteForAnime(animeID);

                // save the records
                foreach (Raw_AniDB_GroupStatus raw in getCmd.GrpStatusCollection.Groups)
                {
                    AniDB_GroupStatus grpstat = new AniDB_GroupStatus(raw);
                    repGrpStat.Save(grpstat);
                }

                // updated cached stats
                // we don't do it in the save method as it would be too many unecessary updates
                logger.Trace("Updating group stats by anime from GetReleaseGroupStatusUDP: {0}", animeID);
                StatsCache.Instance.UpdateUsingAnime(animeID);

                if (getCmd.GrpStatusCollection.LatestEpisodeNumber > 0)
                {
                    // update the anime with a record of the latest subbed episode
                    AniDB_Anime anime = repAnime.GetByAnimeID(animeID);
                    if (anime != null)
                    {
                        anime.LatestEpisodeNumber = getCmd.GrpStatusCollection.LatestEpisodeNumber;
                        repAnime.Save(anime);

                        // check if we have this episode in the database
                        // if not get it now by updating the anime record
                        List <AniDB_Episode> eps = repAniEp.GetByAnimeIDAndEpisodeNumber(animeID, getCmd.GrpStatusCollection.LatestEpisodeNumber);
                        if (eps.Count == 0)
                        {
                            CommandRequest_GetAnimeHTTP cr_anime = new CommandRequest_GetAnimeHTTP(animeID, true, false);
                            cr_anime.Save();
                        }

                        // update the missing episode stats on groups and children
                        AnimeSeries series = repSeries.GetByAnimeID(animeID);
                        if (series != null)
                        {
                            series.UpdateStats(true, true, true);
                            //series.TopLevelAnimeGroup.UpdateStatsFromTopLevel(true, true, true);
                        }
                    }
                }
            }

            return(getCmd.GrpStatusCollection);
        }