Exemplo n.º 1
0
        public static void SetTvDBInfo(int anidbid, AniDB_Episode ep, ref MetroContract_Anime_Episode contract)
        {
            TvDBSummary tvSummary = new TvDBSummary();
            tvSummary.Populate(anidbid);

            SetTvDBInfo(tvSummary, ep, ref contract);
        }
        public static void SetTvDBInfo(TvDBSummary tvSummary,	AniDB_Episode ep, ref MetroContract_Anime_Episode contract)
		{
            #region episode override
            // check if this episode has a direct tvdb over-ride
            if (tvSummary.DictTvDBCrossRefEpisodes.ContainsKey(ep.EpisodeID))
            {
                foreach (TvDB_Episode tvep in tvSummary.DictTvDBEpisodes.Values)
                {
                    if (tvSummary.DictTvDBCrossRefEpisodes[ep.EpisodeID] == tvep.Id)
                    {
                        if (string.IsNullOrEmpty(tvep.Overview))
                            contract.EpisodeOverview = "Episode Overview Not Available";
                        else
                            contract.EpisodeOverview = tvep.Overview;

                        if (string.IsNullOrEmpty(tvep.FullImagePath) || !File.Exists(tvep.FullImagePath))
                        {
                            contract.ImageType = 0;
                            contract.ImageID = 0;
                        }
                        else
                        {
                            contract.ImageType = (int)JMMImageType.TvDB_Episode;
                            contract.ImageID = tvep.TvDB_EpisodeID;
                        }

                        if (ServerSettings.EpisodeTitleSource == DataSourceType.TheTvDB && !string.IsNullOrEmpty(tvep.EpisodeName))
                            contract.EpisodeName = tvep.EpisodeName;

                        return;
                    }
                }
            }
            #endregion

            #region normal episodes
            // now do stuff to improve performance
            if (ep.EpisodeTypeEnum == enEpisodeType.Episode)
            {
                if (tvSummary != null && tvSummary.CrossRefTvDBV2 != null && tvSummary.CrossRefTvDBV2.Count > 0)
                {
                    // find the xref that is right
                    // relies on the xref's being sorted by season number and then episode number (desc)
                    List<SortPropOrFieldAndDirection> sortCriteria = new List<SortPropOrFieldAndDirection>();
                    sortCriteria.Add(new SortPropOrFieldAndDirection("AniDBStartEpisodeNumber", true, SortType.eInteger));
                    List<CrossRef_AniDB_TvDBV2> tvDBCrossRef = Sorting.MultiSort<CrossRef_AniDB_TvDBV2>(tvSummary.CrossRefTvDBV2, sortCriteria);

                    bool foundStartingPoint = false;
                    CrossRef_AniDB_TvDBV2 xrefBase = null;
                    foreach (CrossRef_AniDB_TvDBV2 xrefTV in tvDBCrossRef)
                    {
                        if (xrefTV.AniDBStartEpisodeType != (int)enEpisodeType.Episode) continue;
                        if (ep.EpisodeNumber >= xrefTV.AniDBStartEpisodeNumber)
                        {
                            foundStartingPoint = true;
                            xrefBase = xrefTV;
                            break;
                        }
                    }

                    // we have found the starting epiosde numbder from AniDB
                    // now let's check that the TvDB Season and Episode Number exist
                    if (foundStartingPoint)
                    {

                        Dictionary<int, int> dictTvDBSeasons = null;
                        Dictionary<int, TvDB_Episode> dictTvDBEpisodes = null;
                        foreach (TvDBDetails det in tvSummary.TvDetails.Values)
                        {
                            if (det.TvDBID == xrefBase.TvDBID)
                            {
                                dictTvDBSeasons = det.DictTvDBSeasons;
                                dictTvDBEpisodes = det.DictTvDBEpisodes;
                                break;
                            }
                        }

                        if (dictTvDBSeasons.ContainsKey(xrefBase.TvDBSeasonNumber))
                        {
                            int episodeNumber = dictTvDBSeasons[xrefBase.TvDBSeasonNumber] + (ep.EpisodeNumber + xrefBase.TvDBStartEpisodeNumber - 2) - (xrefBase.AniDBStartEpisodeNumber - 1);
                            if (dictTvDBEpisodes.ContainsKey(episodeNumber))
                            {

                                TvDB_Episode tvep = dictTvDBEpisodes[episodeNumber];
                                if (string.IsNullOrEmpty(tvep.Overview))
                                    contract.EpisodeOverview = "Episode Overview Not Available";
                                else
                                    contract.EpisodeOverview = tvep.Overview;

                                if (string.IsNullOrEmpty(tvep.FullImagePath) || !File.Exists(tvep.FullImagePath))
                                {
                                    contract.ImageType = 0;
                                    contract.ImageID = 0;
                                }
                                else
                                {
                                    contract.ImageType = (int)JMMImageType.TvDB_Episode;
                                    contract.ImageID = tvep.TvDB_EpisodeID;
                                }

                                if (ServerSettings.EpisodeTitleSource == DataSourceType.TheTvDB && !string.IsNullOrEmpty(tvep.EpisodeName))
                                    contract.EpisodeName = tvep.EpisodeName;
                            }
                        }
                    }
                }
            }
            #endregion

            #region special episodes
            if (ep.EpisodeTypeEnum == enEpisodeType.Special)
            {
                // find the xref that is right
                // relies on the xref's being sorted by season number and then episode number (desc)
                List<SortPropOrFieldAndDirection> sortCriteria = new List<SortPropOrFieldAndDirection>();
                sortCriteria.Add(new SortPropOrFieldAndDirection("AniDBStartEpisodeNumber", true, SortType.eInteger));
                List<CrossRef_AniDB_TvDBV2> tvDBCrossRef = Sorting.MultiSort<CrossRef_AniDB_TvDBV2>(tvSummary.CrossRefTvDBV2, sortCriteria);

                bool foundStartingPoint = false;
                CrossRef_AniDB_TvDBV2 xrefBase = null;
                foreach (CrossRef_AniDB_TvDBV2 xrefTV in tvDBCrossRef)
                {
                    if (xrefTV.AniDBStartEpisodeType != (int)enEpisodeType.Special) continue;
                    if (ep.EpisodeNumber >= xrefTV.AniDBStartEpisodeNumber)
                    {
                        foundStartingPoint = true;
                        xrefBase = xrefTV;
                        break;
                    }
                }

                if (tvSummary != null && tvSummary.CrossRefTvDBV2 != null && tvSummary.CrossRefTvDBV2.Count > 0)
                {
                    // we have found the starting epiosde numbder from AniDB
                    // now let's check that the TvDB Season and Episode Number exist
                    if (foundStartingPoint)
                    {

                        Dictionary<int, int> dictTvDBSeasons = null;
                        Dictionary<int, TvDB_Episode> dictTvDBEpisodes = null;
                        foreach (TvDBDetails det in tvSummary.TvDetails.Values)
                        {
                            if (det.TvDBID == xrefBase.TvDBID)
                            {
                                dictTvDBSeasons = det.DictTvDBSeasons;
                                dictTvDBEpisodes = det.DictTvDBEpisodes;
                                break;
                            }
                        }

                        if (dictTvDBSeasons.ContainsKey(xrefBase.TvDBSeasonNumber))
                        {
                            int episodeNumber = dictTvDBSeasons[xrefBase.TvDBSeasonNumber] + (ep.EpisodeNumber + xrefBase.TvDBStartEpisodeNumber - 2) - (xrefBase.AniDBStartEpisodeNumber - 1);
                            if (dictTvDBEpisodes.ContainsKey(episodeNumber))
                            {
                                TvDB_Episode tvep = dictTvDBEpisodes[episodeNumber];
                                contract.EpisodeOverview = tvep.Overview;

                                if (string.IsNullOrEmpty(tvep.FullImagePath) || !File.Exists(tvep.FullImagePath))
                                {
                                    contract.ImageType = 0;
                                    contract.ImageID = 0;
                                }
                                else
                                {
                                    contract.ImageType = (int)JMMImageType.TvDB_Episode;
                                    contract.ImageID = tvep.TvDB_EpisodeID;
                                }

                                if (ServerSettings.EpisodeTitleSource == DataSourceType.TheTvDB && !string.IsNullOrEmpty(tvep.EpisodeName))
                                    contract.EpisodeName = tvep.EpisodeName;
                            }
                        }
                    }
                }
            }
            #endregion




		}
		public MetroContract_Anime_Detail GetAnimeDetail(int animeID, int jmmuserID, int maxEpisodeRecords)
		{
			try
			{
				using (var session = JMMService.SessionFactory.OpenSession())
				{
					AnimeSeriesRepository repSeries = new AnimeSeriesRepository();
					AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository();

					AniDB_Anime anime = repAnime.GetByAnimeID(session, animeID);
					if (anime == null) return null;

					AnimeSeries ser = repSeries.GetByAnimeID(session, animeID);

					MetroContract_Anime_Detail ret = new MetroContract_Anime_Detail();
					ret.AnimeID = anime.AnimeID;

					if (ser != null)
						ret.AnimeName = ser.GetSeriesName(session);
					else
						ret.AnimeName = anime.MainTitle;

					if (ser != null)
						ret.AnimeSeriesID = ser.AnimeSeriesID;
					else
						ret.AnimeSeriesID = 0;

					ret.BeginYear = anime.BeginYear;
					ret.EndYear = anime.EndYear;

					ImageDetails imgDet = anime.GetDefaultPosterDetailsNoBlanks(session);
					ret.PosterImageType = (int)imgDet.ImageType;
					ret.PosterImageID = imgDet.ImageID;

					ImageDetails imgDetFan = anime.GetDefaultFanartDetailsNoBlanks(session);
					if (imgDetFan != null)
					{
						ret.FanartImageType = (int)imgDetFan.ImageType;
						ret.FanartImageID = imgDetFan.ImageID;
					}
					else
					{
						ret.FanartImageType = 0;
						ret.FanartImageID = 0;
					}

					ret.AnimeType = anime.AnimeTypeDescription;
					ret.Description = anime.Description;
					ret.EpisodeCountNormal = anime.EpisodeCountNormal;
					ret.EpisodeCountSpecial = anime.EpisodeCountSpecial;


					ret.AirDate = anime.AirDate;
					ret.EndDate = anime.EndDate;

					ret.OverallRating = anime.AniDBRating;
					ret.TotalVotes = anime.AniDBTotalVotes;
					ret.AllTags = anime.TagsString;

					ret.NextEpisodesToWatch = new List<MetroContract_Anime_Episode>();
					if (ser != null)
					{
						AnimeSeries_User serUserRec = ser.GetUserRecord(session, jmmuserID);
						if (ser != null)
							ret.UnwatchedEpisodeCount = serUserRec.UnwatchedEpisodeCount;
						else
							ret.UnwatchedEpisodeCount = 0;

						AnimeEpisodeRepository repEps = new AnimeEpisodeRepository();
						AnimeEpisode_UserRepository repEpUser = new AnimeEpisode_UserRepository();

						List<AnimeEpisode> epList = new List<AnimeEpisode>();
						Dictionary<int, AnimeEpisode_User> dictEpUsers = new Dictionary<int, AnimeEpisode_User>();
						foreach (AnimeEpisode_User userRecord in repEpUser.GetByUserIDAndSeriesID(session, jmmuserID, ser.AnimeSeriesID))
							dictEpUsers[userRecord.AnimeEpisodeID] = userRecord;

						foreach (AnimeEpisode animeep in repEps.GetBySeriesID(session, ser.AnimeSeriesID))
						{
							if (!dictEpUsers.ContainsKey(animeep.AnimeEpisodeID))
							{
								epList.Add(animeep);
								continue;
							}

							AnimeEpisode_User usrRec = dictEpUsers[animeep.AnimeEpisodeID];
							if (usrRec.WatchedCount == 0 || !usrRec.WatchedDate.HasValue)
								epList.Add(animeep);
						}

						AniDB_EpisodeRepository repAniEps = new AniDB_EpisodeRepository();
						List<AniDB_Episode> aniEpList = repAniEps.GetByAnimeID(session, ser.AniDB_ID);
						Dictionary<int, AniDB_Episode> dictAniEps = new Dictionary<int, AniDB_Episode>();
						foreach (AniDB_Episode aniep in aniEpList)
							dictAniEps[aniep.EpisodeID] = aniep;

						List<Contract_AnimeEpisode> candidateEps = new List<Contract_AnimeEpisode>();

						foreach (AnimeEpisode ep in epList)
						{
							if (dictAniEps.ContainsKey(ep.AniDB_EpisodeID))
							{
								AniDB_Episode anidbep = dictAniEps[ep.AniDB_EpisodeID];
								if (anidbep.EpisodeType == (int)enEpisodeType.Episode || anidbep.EpisodeType == (int)enEpisodeType.Special)
								{
									AnimeEpisode_User userRecord = null;
									if (dictEpUsers.ContainsKey(ep.AnimeEpisodeID))
										userRecord = dictEpUsers[ep.AnimeEpisodeID];

									Contract_AnimeEpisode epContract = ep.ToContract(anidbep, new List<VideoLocal>(), userRecord, serUserRec);
									candidateEps.Add(epContract);
								}
							}
						}

						if (candidateEps.Count > 0)
						{
                            TvDBSummary tvSummary = new TvDBSummary();
                            tvSummary.Populate(ser.AniDB_ID);

							// sort by episode type and number to find the next episode
							List<SortPropOrFieldAndDirection> sortCriteria = new List<SortPropOrFieldAndDirection>();
							sortCriteria.Add(new SortPropOrFieldAndDirection("EpisodeType", false, SortType.eInteger));
							sortCriteria.Add(new SortPropOrFieldAndDirection("EpisodeNumber", false, SortType.eInteger));
							candidateEps = Sorting.MultiSort<Contract_AnimeEpisode>(candidateEps, sortCriteria);

							// this will generate a lot of queries when the user doesn have files
							// for these episodes
							int cnt = 0;
							foreach (Contract_AnimeEpisode canEp in candidateEps)
							{

								if (dictAniEps.ContainsKey(canEp.AniDB_EpisodeID))
								{
									AniDB_Episode anidbep = dictAniEps[canEp.AniDB_EpisodeID];

									AnimeEpisode_User userEpRecord = null;
									if (dictEpUsers.ContainsKey(canEp.AnimeEpisodeID))
										userEpRecord = dictEpUsers[canEp.AnimeEpisodeID];

									// now refresh from the database to get file count
									AnimeEpisode epFresh = repEps.GetByID(session, canEp.AnimeEpisodeID);

									int fileCount = epFresh.GetVideoLocals(session).Count;
									if (fileCount > 0)
									{
										MetroContract_Anime_Episode contract = new MetroContract_Anime_Episode();
										contract.AnimeEpisodeID = epFresh.AnimeEpisodeID;
										contract.LocalFileCount = fileCount;

										if (userEpRecord == null)
											contract.IsWatched = false;
										else
											contract.IsWatched = userEpRecord.WatchedCount > 0;

										// anidb
										contract.EpisodeNumber = anidbep.EpisodeNumber;
										contract.EpisodeName = anidbep.RomajiName;
										if (!string.IsNullOrEmpty(anidbep.EnglishName))
											contract.EpisodeName = anidbep.EnglishName;

										contract.EpisodeType = anidbep.EpisodeType;
										contract.LengthSeconds = anidbep.LengthSeconds;
										contract.AirDate = anidbep.AirDateFormatted;

										// tvdb
                                        SetTvDBInfo(tvSummary, anidbep, ref contract);
										

										ret.NextEpisodesToWatch.Add(contract);
										cnt++;
									}
								}
								if (cnt == maxEpisodeRecords) break;
							}
						}
					}

					return ret;
				}

			}
			catch (Exception ex)
			{
				logger.ErrorException(ex.ToString(), ex);
				return null;
			}
		}