public override void ProcessCommand()
		{
			
			try
			{
				CrossRef_AniDB_TraktRepository repCrossRef = new CrossRef_AniDB_TraktRepository();
				CrossRef_AniDB_Trakt xref = repCrossRef.GetByID(CrossRef_AniDB_TraktID);
				if (xref == null) return;

				Trakt_ShowRepository repShow = new Trakt_ShowRepository();
				Trakt_Show tvShow = repShow.GetByTraktID(xref.TraktID);
				if (tvShow == null) return;

				string showName = "";
				if (tvShow != null) showName = tvShow.Title;

				XMLService.Send_CrossRef_AniDB_Trakt(xref, showName);
			}
			catch (Exception ex)
			{
				logger.ErrorException("Error processing CommandRequest_WebCacheSendXRefAniDBTrakt: {0}" + ex.ToString(), ex);
				return;
			}
		}
		public bool Init(AnimeEpisode aniepisode)
		{
			try
			{
				if (string.IsNullOrEmpty(ServerSettings.Trakt_Username) || string.IsNullOrEmpty(ServerSettings.Trakt_Password))
					return false;
				
				username = ServerSettings.Trakt_Username;
				password = Utils.CalculateSHA1(ServerSettings.Trakt_Password, Encoding.Default);

				imdb_id = "";
				AnimeSeries ser = aniepisode.GetAnimeSeries();
				if (ser == null) return false;

				CrossRef_AniDB_TraktRepository repCrossRef = new CrossRef_AniDB_TraktRepository();
				CrossRef_AniDB_Trakt xref = repCrossRef.GetByAnimeID(ser.AniDB_ID);
				if (xref == null) return false;

				Trakt_ShowRepository repShows = new Trakt_ShowRepository();
				Trakt_Show show = repShows.GetByTraktID(xref.TraktID);
				if (show == null) return false;
				if (!show.TvDB_ID.HasValue) return false;

				tvdb_id = show.TvDB_ID.Value.ToString();
				title = show.Title;
				year = show.Year;

				int retEpNum = 0, retSeason = 0;
				GetTraktEpisodeNumber(aniepisode, show, xref.TraktSeasonNumber, ref retEpNum, ref retSeason);
				if (retEpNum < 0) return false;

				episode = retEpNum.ToString();
				season = retSeason.ToString();

				AniDB_Episode aniep = aniepisode.AniDB_Episode;
				if (aniep != null)
				{
					TimeSpan t = TimeSpan.FromSeconds(aniep.LengthSeconds + 14);
					int toMinutes = int.Parse(Math.Round(t.TotalMinutes).ToString());
					duration = toMinutes.ToString();
				}
				else
					duration = "25";

				progress = "100";

				plugin_version = "0.4";
				media_center_version = "1.2.0.1";
				media_center_date = "Dec 17 2010";

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

			return true;
		}
		public override void ProcessCommand()
		{
			logger.Info("Processing CommandRequest_TraktSearchAnime: {0}", AnimeID);

			try
			{
				using (var session = JMMService.SessionFactory.OpenSession())
				{
					// first check if the user wants to use the web cache
					if (ServerSettings.WebCache_TvDB_Get)
					{
						try
						{
							CrossRef_AniDB_TraktResult crossRef = XMLService.Get_CrossRef_AniDB_Trakt(AnimeID);
							if (crossRef != null)
							{
								TraktTVShow showInfo = TraktTVHelper.GetShowInfo(crossRef.TraktID);
								if (showInfo != null)
								{
									logger.Trace("Found trakt match on web cache for {0} - id = {1}", AnimeID, showInfo.title);
									TraktTVHelper.LinkAniDBTrakt(AnimeID, crossRef.TraktID, crossRef.TraktSeasonNumber, true);
									return;
								}
							}
						}
						catch (Exception ex)
						{
							logger.ErrorException(ex.ToString(), ex);
						}
					}


					// lets try to see locally if we have a tvDB link for this anime
					// Trakt allows the use of TvDB ID's or their own Trakt ID's
					CrossRef_AniDB_TvDBV2Repository repCrossRefTvDB = new CrossRef_AniDB_TvDBV2Repository();
					List<CrossRef_AniDB_TvDBV2> xrefTvDBs = repCrossRefTvDB.GetByAnimeID(session, AnimeID);
					if (xrefTvDBs != null && xrefTvDBs.Count == 1)  //TODO this is temporary code, until trakt also allows multiple links
					{
						TraktTVShow showInfo = TraktTVHelper.GetShowInfo(xrefTvDBs[0].TvDBID);
						if (showInfo != null)
						{
							// make sure the season specified by TvDB also exists on Trakt
							Trakt_ShowRepository repShow = new Trakt_ShowRepository();
							Trakt_Show traktShow = repShow.GetByTraktID(session, showInfo.TraktID);
							if (traktShow != null)
							{
								Trakt_SeasonRepository repSeasons = new Trakt_SeasonRepository();
								Trakt_Season traktSeason = repSeasons.GetByShowIDAndSeason(session, traktShow.Trakt_ShowID, xrefTvDBs[0].TvDBSeasonNumber);
								if (traktSeason != null)
								{
									logger.Trace("Found trakt match using TvDBID locally {0} - id = {1}", AnimeID, showInfo.title);
									TraktTVHelper.LinkAniDBTrakt(AnimeID, showInfo.TraktID, traktSeason.Season, true);
									return;
								}
							}
						}
					}

					// if not lets try the tvdb web cache based on the same reasoning
					if (ServerSettings.WebCache_TvDB_Get)
					{
						List<JMMServer.Providers.Azure.CrossRef_AniDB_TvDB> cacheResults = JMMServer.Providers.Azure.AzureWebAPI.Get_CrossRefAniDBTvDB(AnimeID);
						if (cacheResults != null && cacheResults.Count > 0)
						{
							TraktTVShow showInfo = TraktTVHelper.GetShowInfo(cacheResults[0].TvDBID);
							if (showInfo != null)
							{
								// make sure the season specified by TvDB also exists on Trakt
								Trakt_ShowRepository repShow = new Trakt_ShowRepository();
								Trakt_Show traktShow = repShow.GetByTraktID(session, showInfo.TraktID);
								if (traktShow != null)
								{
									Trakt_SeasonRepository repSeasons = new Trakt_SeasonRepository();
									Trakt_Season traktSeason = repSeasons.GetByShowIDAndSeason(session, traktShow.Trakt_ShowID, cacheResults[0].TvDBSeasonNumber);
									if (traktSeason != null)
									{
										logger.Trace("Found trakt match on web cache by using TvDBID {0} - id = {1}", AnimeID, showInfo.title);
										TraktTVHelper.LinkAniDBTrakt(AnimeID, showInfo.TraktID, traktSeason.Season, true);
										return;
									}
								}
							}
						}
					}

					// finally lets try searching Trakt directly
					string searchCriteria = "";
					AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository();
					AniDB_Anime anime = repAnime.GetByAnimeID(session, AnimeID);
					if (anime == null) return;

					searchCriteria = anime.MainTitle;

					// if not wanting to use web cache, or no match found on the web cache go to TvDB directly
					List<TraktTVShow> results = TraktTVHelper.SearchShow(searchCriteria);
					logger.Trace("Found {0} trakt results for {1} ", results.Count, searchCriteria);
					if (ProcessSearchResults(session, results, searchCriteria)) return;


					if (results.Count == 0)
					{
						foreach (AniDB_Anime_Title title in anime.GetTitles(session))
						{
							if (title.TitleType.ToUpper() != Constants.AnimeTitleType.Official.ToUpper()) continue;

							if (searchCriteria.ToUpper() == title.Title.ToUpper()) continue;

							results = TraktTVHelper.SearchShow(searchCriteria);
							logger.Trace("Found {0} trakt results for search on {1}", results.Count, title.Title);
							if (ProcessSearchResults(session, results, title.Title)) return;
						}
					}
				}

			}
			catch (Exception ex)
			{
				logger.Error("Error processing CommandRequest_TvDBSearchAnime: {0} - {1}", AnimeID, ex.ToString());
				return;
			}
		}
		public Contract_Trakt_Activity GetTraktFriendInfo(int maxResults, bool animeOnly, bool getShouts, bool getScrobbles)
		{
			CrossRef_AniDB_TraktRepository repXrefTrakt = new CrossRef_AniDB_TraktRepository();
			CrossRef_AniDB_TvDBV2Repository repXrefTvDB = new CrossRef_AniDB_TvDBV2Repository();
			AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository();
			AnimeSeriesRepository repSeries = new AnimeSeriesRepository();
			Trakt_FriendRepository repFriends = new Trakt_FriendRepository();
			Trakt_EpisodeRepository repEpisodes = new Trakt_EpisodeRepository();
			Trakt_ShowRepository repShows = new Trakt_ShowRepository();

			Contract_Trakt_Activity contract = new Contract_Trakt_Activity();
			contract.HasTraktAccount = true;
			if (string.IsNullOrEmpty(ServerSettings.Trakt_Username) || string.IsNullOrEmpty(ServerSettings.Trakt_Password))
				contract.HasTraktAccount = false;

			contract.TraktFriends = new List<Contract_Trakt_Friend>();
			contract.TraktFriendRequests = new List<Contract_Trakt_FriendFrequest>();
			contract.TraktFriendActivity = new List<Contract_Trakt_FriendActivity>();

			try
			{
				int i = 1;
				foreach (TraktTV_Activity act in StatsCache.Instance.TraktFriendActivityInfo)
				{
					

					if (i >= maxResults) break;

					if (act.ActivityAction == (int)TraktActivityAction.Scrobble && !getScrobbles) continue;
					if (act.ActivityAction == (int)TraktActivityAction.Shout && !getShouts) continue;

					Contract_Trakt_FriendActivity contractAct = new Contract_Trakt_FriendActivity();

					if (act.show == null) continue;

					// find the anime and series based on the trakt id
					int? animeID = null;
					CrossRef_AniDB_Trakt xref = null;
					if (act.episode != null)
						xref = repXrefTrakt.GetByTraktID(act.show.TraktID, int.Parse(act.episode.season));

					if (xref != null)
						animeID = xref.AnimeID;
					else
					{
						// try a rough match
						// since we won't always do an exact match by season
						List<CrossRef_AniDB_Trakt> traktXrefs = repXrefTrakt.GetByTraktID(act.show.TraktID);
						if (traktXrefs.Count > 0)
							animeID = traktXrefs[0].AnimeID;
						else
						{
							// try the tvdb id instead
							//TODO
							/*
							CrossRef_AniDB_TvDBV2 xrefTvDB = null;

							if (act.episode != null)
								xrefTvDB = repXrefTvDB.GetByTvDBID(int.Parse(act.show.tvdb_id), int.Parse(act.episode.season));

							if (xrefTvDB != null)
								animeID = xrefTvDB.AnimeID;*/
						}
					}

					// skip this activity if we can't find the anime and the user only wants to see anime related stuff
					if (!animeID.HasValue && animeOnly)
					{
						//TODO
						// however let's try and look it up on the web cache to see if it is an anime
						// this just might be an anime that user doesn't have in their local database
						continue;
					}

					

					// activity details
					contractAct.ActivityAction = act.ActivityAction;
					contractAct.ActivityType = act.ActivityType;
					contractAct.ActivityDate = Utils.GetAniDBDateAsDate(act.timestamp);
					if (act.elapsed != null)
					{
						contractAct.Elapsed = act.elapsed.full;
						contractAct.ElapsedShort = act.elapsed.shortElapsed;
					}

					Trakt_Friend traktFriend = repFriends.GetByUsername(act.user.username);
					if (traktFriend == null) return null;
					
					// user details
					contractAct.User = new Contract_Trakt_User();
					contractAct.User.Trakt_FriendID = traktFriend.Trakt_FriendID;
					contractAct.User.Username = act.user.username;
					contractAct.User.Full_name = act.user.full_name;
					contractAct.User.Gender = act.user.gender;
					contractAct.User.Age = act.user.age;
					contractAct.User.Location = act.user.location;
					contractAct.User.About = act.user.about;
					contractAct.User.Joined = act.user.joined;
					contractAct.User.Avatar = act.user.avatar;
					contractAct.User.Url = act.user.url;
					contractAct.User.JoinedDate = Utils.GetAniDBDateAsDate(act.user.joined);

					// episode details
					if (act.ActivityAction == (int)TraktActivityAction.Scrobble && act.episode != null) // scrobble episode
					{
						contractAct.Episode = new Contract_Trakt_WatchedEpisode();
						contractAct.Episode.AnimeSeriesID = null;

						contractAct.Episode.Episode_Number = act.episode.number;
						contractAct.Episode.Episode_Overview = act.episode.overview;
						contractAct.Episode.Episode_Season = act.episode.season;
						contractAct.Episode.Episode_Title = act.episode.title;
						contractAct.Episode.Episode_Url = act.episode.url;
						contractAct.Episode.Trakt_EpisodeID = -1;
						

						if (act.episode.images != null)
							contractAct.Episode.Episode_Screenshot = act.episode.images.screen;

						if (act.show != null)
						{
							contractAct.Episode.TraktShow = act.show.ToContract();

							Trakt_Show show = repShows.GetByTraktID(act.show.TraktID);
							if (show != null)
							{
								Trakt_Episode episode = repEpisodes.GetByShowIDSeasonAndEpisode(show.Trakt_ShowID, int.Parse(act.episode.season), int.Parse(act.episode.number));
								if (episode != null)
									contractAct.Episode.Trakt_EpisodeID = episode.Trakt_EpisodeID;
							}

							if (animeID.HasValue)
							{

								AnimeSeries ser = repSeries.GetByAnimeID(animeID.Value);
								if (ser != null)
									contractAct.Episode.AnimeSeriesID = ser.AnimeSeriesID;

								AniDB_Anime anime = repAnime.GetByAnimeID(animeID.Value);
								if (anime != null)
									contractAct.Episode.Anime = anime.ToContract(true, null);

							}
						}
					}

					// shout details
					if (act.ActivityAction == (int)TraktActivityAction.Shout && act.shout != null) // shout
					{
						contractAct.Shout = new Contract_Trakt_Shout();

						contractAct.Shout.ShoutType = act.ActivityType; // episode or show
						contractAct.Shout.Text = act.shout.text;
						contractAct.Shout.Spoiler = act.shout.spoiler;

						contractAct.Shout.AnimeSeriesID = null;

						if (act.ActivityType == 1 && act.episode != null) // episode
						{
							contractAct.Shout.Episode_Number = act.episode.number;
							contractAct.Shout.Episode_Overview = act.episode.overview;
							contractAct.Shout.Episode_Season = act.episode.season;
							contractAct.Shout.Episode_Title = act.episode.title;
							contractAct.Shout.Episode_Url = act.episode.url;

							if (act.episode.images != null)
								contractAct.Shout.Episode_Screenshot = act.episode.images.screen;
						}

						if (act.show != null) // episode or show
						{
							if (act.episode == null)
								contractAct.Shout.Episode_Screenshot = act.show.images.fanart;

							contractAct.Shout.TraktShow = act.show.ToContract();

							if (animeID.HasValue)
							{

								AnimeSeries ser = repSeries.GetByAnimeID(animeID.Value);
								if (ser != null)
									contractAct.Shout.AnimeSeriesID = ser.AnimeSeriesID;

								AniDB_Anime anime = repAnime.GetByAnimeID(animeID.Value);
								if (anime != null)
									contractAct.Shout.Anime = anime.ToContract(true, null);

							}
						}
					}

					contract.TraktFriendActivity.Add(contractAct);
					i++;
				}

				foreach (TraktTVFriendRequest req in StatsCache.Instance.TraktFriendRequests)
				{
					Contract_Trakt_FriendFrequest contractReq = req.ToContract();
					contract.TraktFriendRequests.Add(contractReq);
				}
			}
			catch (Exception ex)
			{
				logger.ErrorException(ex.ToString(), ex);
			}
			return contract;
		}
		public List<int> GetSeasonNumbersForTrakt(string traktID)
		{
			List<int> seasonNumbers = new List<int>();
			try
			{
				// refresh show info including season numbers from trakt
				TraktTVShow tvshow = TraktTVHelper.GetShowInfo(traktID);

				Trakt_ShowRepository repShows = new Trakt_ShowRepository();
				Trakt_Show show = repShows.GetByTraktID(traktID);
				if (show == null) return seasonNumbers;

				foreach (Trakt_Season season in show.Seasons)
					seasonNumbers.Add(season.Season);

				return seasonNumbers;

			}
			catch (Exception ex)
			{
				logger.ErrorException(ex.ToString(), ex);
				return seasonNumbers;
			}
		}
Example #6
0
		public static void MarkEpisodeUnwatched(AnimeEpisode ep)
		{
			try
			{
				if (string.IsNullOrEmpty(ServerSettings.Trakt_Username) || string.IsNullOrEmpty(ServerSettings.Trakt_Password))
					return;

				CrossRef_AniDB_Trakt xref = ep.GetAnimeSeries().CrossRefTrakt;
				if (xref == null) return;

				Trakt_ShowRepository repShows = new Trakt_ShowRepository();
				Trakt_Show show = repShows.GetByTraktID(xref.TraktID);
				if (show == null) return;
				if (!show.TvDB_ID.HasValue) return;

				Dictionary<int, int> dictTraktSeasons = null;
				Dictionary<int, Trakt_Episode> dictTraktEpisodes = null;
				Dictionary<int, Trakt_Episode> dictTraktSpecials = null;
				GetDictTraktEpisodesAndSeasons(show, ref dictTraktEpisodes, ref dictTraktSpecials, ref dictTraktSeasons);

				TraktTVPost_ShowEpisodeUnseen postUnseen = new TraktTVPost_ShowEpisodeUnseen();
				postUnseen.episodes = new List<TraktTVSeasonEpisode>();
				postUnseen.SetCredentials();
				postUnseen.imdb_id = "";
				postUnseen.title = show.Title;
				postUnseen.year = show.Year;
				postUnseen.tvdb_id = show.TvDB_ID.Value.ToString();

				int retEpNum = -1;
				int retSeason = -1;

				GetTraktEpisodeNumber(ep, ep.GetAnimeSeries(), show, xref.TraktSeasonNumber, ref retEpNum, ref retSeason, dictTraktEpisodes, dictTraktSpecials, dictTraktSeasons);
				if (retEpNum < 0) return;

				TraktTVSeasonEpisode traktEp = new TraktTVSeasonEpisode();
				traktEp.episode = retEpNum.ToString();
				traktEp.season = retSeason.ToString();
				postUnseen.episodes.Add(traktEp);

				logger.Trace("Marking episode as unwatched on Trakt: {0} - S{1} - EP{2}", show.Title, retSeason, retEpNum);

				string urlUnseen = string.Format(Constants.TraktTvURLs.URLPostShowEpisodeUnseen, Constants.TraktTvURLs.APIKey);
				string json = JSONHelper.Serialize<TraktTVPost_ShowEpisodeUnseen>(postUnseen);

				SendData(urlUnseen, json);

			}
			catch (Exception ex)
			{
				logger.ErrorException("Error in TraktTVHelper.MarkEpisodeWatched: " + ex.ToString(), ex);
			}

		}
Example #7
0
		public static bool PostShoutShow(int animeID, string shoutText, bool isSpoiler, ref string returnMessage)
		{
			returnMessage = "";
			try
			{
				if (string.IsNullOrEmpty(ServerSettings.Trakt_Username) || string.IsNullOrEmpty(ServerSettings.Trakt_Password))
				{
					returnMessage = "Trakt credentials have not been entered";
					return false;
				}

				if (string.IsNullOrEmpty(shoutText))
				{
					returnMessage = "Please enter text for your shout";
					return false;
				}

				CrossRef_AniDB_TraktRepository repXrefTrakt = new CrossRef_AniDB_TraktRepository();
				Trakt_ShowRepository repTraktShow = new Trakt_ShowRepository();

				CrossRef_AniDB_Trakt traktXRef = repXrefTrakt.GetByAnimeID(animeID);
				if (traktXRef == null)
				{
					returnMessage = string.Format("Could not find trakt show for Anime ID: {0}", animeID);
					return false;
				}

				Trakt_Show show = repTraktShow.GetByTraktID(traktXRef.TraktID);
				if (show == null || !show.TvDB_ID.HasValue)
				{
					returnMessage = string.Format("Could not find trakt show for Anime ID: {0}", animeID);
					return false;
				}

				TraktTVPost_ShoutShow cmd = new TraktTVPost_ShoutShow();
				cmd.Init(shoutText, isSpoiler, show.TvDB_ID.Value);

				string url = string.Format(Constants.TraktTvURLs.URLPostShoutShow, Constants.TraktTvURLs.APIKey);
				logger.Trace("PostShoutShow: {0}", url);

				string json = JSONHelper.Serialize<TraktTVPost_ShoutShow>(cmd);
				string jsonResponse = SendData(url, json);

				TraktTVGenericResponse genResponse = JSONHelper.Deserialize<TraktTVGenericResponse>(jsonResponse);
				if (genResponse.IsSuccess)
				{
					returnMessage = genResponse.message;
					return true;
				}
				else
				{
					returnMessage = genResponse.error;
					return false;
				}

			}
			catch (Exception ex)
			{
				logger.ErrorException("Error in TraktTVHelper.PostShoutShow: " + ex.ToString(), ex);
				returnMessage = ex.Message;
				return false;
			}
		}
Example #8
0
		/*public static void MarkEpisodeWatched(AnimeEpisode ep)
		{
			TraktTVPost_ShowScrobble tt = new TraktTVPost_ShowScrobble();
			if (!tt.Init(ep)) return;

			try
			{
				string url = string.Format(Constants.TraktTvURLs.URLPostShowScrobble, Constants.TraktTvURLs.APIKey);
				logger.Trace("GetShowInfo: {0}", url);

				logger.Trace("Marking episode as unwatched on Trakt: {0} - S{1} - EP{2}", show.Title, retSeason, retEpNum);

				string json = JSONHelper.Serialize<TraktTVPost_ShowScrobble>(tt);

				SendData(url, json);

			}
			catch (Exception ex)
			{
				logger.ErrorException("Error in TraktTVHelper.MarkEpisodeWatched: " + ex.ToString(), ex);
			}

		}*/

		public static void MarkEpisodeWatched(AnimeEpisode ep)
		{
			try
			{
				if (string.IsNullOrEmpty(ServerSettings.Trakt_Username) || string.IsNullOrEmpty(ServerSettings.Trakt_Password))
					return;

				CrossRef_AniDB_Trakt xref = ep.GetAnimeSeries().CrossRefTrakt;
				if (xref == null) return;

				Trakt_ShowRepository repShows = new Trakt_ShowRepository();
				Trakt_Show show = repShows.GetByTraktID(xref.TraktID);
				if (show == null) return;
				if (!show.TvDB_ID.HasValue) return;

				Dictionary<int, int> dictTraktSeasons = null;
				Dictionary<int, Trakt_Episode> dictTraktEpisodes = null;
				Dictionary<int, Trakt_Episode> dictTraktSpecials = null;
				GetDictTraktEpisodesAndSeasons(show, ref dictTraktEpisodes, ref dictTraktSpecials, ref dictTraktSeasons);

				int retEpNum = -1;
				int retSeason = -1;

				GetTraktEpisodeNumber(ep, ep.GetAnimeSeries(), show, xref.TraktSeasonNumber, ref retEpNum, ref retSeason, dictTraktEpisodes, dictTraktSpecials, dictTraktSeasons);
				if (retEpNum < 0) return;

				TraktTVPost_ShowScrobble postScrobble = new TraktTVPost_ShowScrobble();
				postScrobble.SetCredentials();
				postScrobble.imdb_id = "";
				postScrobble.title = show.Title;
				postScrobble.year = show.Year;
				postScrobble.tvdb_id = show.TvDB_ID.Value.ToString();
				postScrobble.episode = retEpNum.ToString();
				postScrobble.season = retSeason.ToString();

				AniDB_Episode aniep = ep.AniDB_Episode;
				if (aniep != null)
				{
					TimeSpan t = TimeSpan.FromSeconds(aniep.LengthSeconds + 14);
					int toMinutes = int.Parse(Math.Round(t.TotalMinutes).ToString());
					postScrobble.duration = toMinutes.ToString();
				}
				else
					postScrobble.duration = "25";

				postScrobble.progress = "100";

				postScrobble.plugin_version = "0.4";
				postScrobble.media_center_version = "1.2.0.1";
				postScrobble.media_center_date = "Dec 17 2010";
			
				logger.Trace("Marking episode as watched (scrobble) on Trakt: {0} - S{1} - EP{2}", show.Title, retSeason, retEpNum);

				string url = string.Format(Constants.TraktTvURLs.URLPostShowScrobble, Constants.TraktTvURLs.APIKey);
				string json = JSONHelper.Serialize<TraktTVPost_ShowScrobble>(postScrobble);

				SendData(url, json);

			}
			catch (Exception ex)
			{
				logger.ErrorException("Error in TraktTVHelper.MarkEpisodeWatched: " + ex.ToString(), ex);
			}

		}
Example #9
0
		public static void LinkAniDBTrakt(ISession session, int animeID, string traktID, int seasonNumber, bool fromWebCache)
		{
			CrossRef_AniDB_TraktRepository repCrossRef = new CrossRef_AniDB_TraktRepository();
			CrossRef_AniDB_Trakt xrefTemp = repCrossRef.GetByTraktID(traktID, seasonNumber);
			if (xrefTemp != null)
			{
				string msg = string.Format("Not using Trakt link as one already exists {0} ({1}) - {2}", traktID, seasonNumber, animeID);
				logger.Warn(msg);
				return;
			}

			// check if we have this information locally
			// if not download it now
			Trakt_ShowRepository repShow = new Trakt_ShowRepository();
			Trakt_Show traktShow = repShow.GetByTraktID(traktID);
			if (traktShow == null)
			{
				// we download the series info here
				TraktTVShow tvshow = GetShowInfo(traktID);
				if (tvshow == null) return;
			}

			// download fanart, posters
			DownloadAllImages(traktID);

			CrossRef_AniDB_Trakt xref = repCrossRef.GetByAnimeID(animeID);
			if (xref == null)
				xref = new CrossRef_AniDB_Trakt();

			xref.AnimeID = animeID;
			if (fromWebCache)
				xref.CrossRefSource = (int)CrossRefSource.WebCache;
			else
				xref.CrossRefSource = (int)CrossRefSource.User;

			xref.TraktID = traktID;
			xref.TraktSeasonNumber = seasonNumber;
			repCrossRef.Save(xref);

			StatsCache.Instance.UpdateUsingAnime(session, animeID);

			logger.Trace("Changed trakt association: {0}", animeID);

			CommandRequest_WebCacheSendXRefAniDBTrakt req = new CommandRequest_WebCacheSendXRefAniDBTrakt(xref.CrossRef_AniDB_TraktID);
			req.Save();
		}
Example #10
0
		public static void DownloadAllImages(string traktID)
		{
			try
			{
				//now download the images
				Trakt_ShowRepository repShow = new Trakt_ShowRepository();
				Trakt_Show show = repShow.GetByTraktID(traktID);
				if (show == null) return;


				if (ServerSettings.Trakt_DownloadFanart)
				{
					//download the fanart image for the show
					Trakt_ImageFanartRepository repFanart = new Trakt_ImageFanartRepository();
					Trakt_ImageFanart fanart = repFanart.GetByShowIDAndSeason(show.Trakt_ShowID, 1);
					if (fanart != null)
					{
						if (!string.IsNullOrEmpty(fanart.FullImagePath))
						{
							if (!File.Exists(fanart.FullImagePath))
							{
								CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(fanart.Trakt_ImageFanartID, JMMImageType.Trakt_Fanart, false);
								cmd.Save();
							}
						}
					}
				}

				
				// download the posters for seasons
				Trakt_ImagePosterRepository repPosters = new Trakt_ImagePosterRepository();
				foreach (Trakt_Season season in show.Seasons)
				{
					if (ServerSettings.Trakt_DownloadPosters)
					{
						Trakt_ImagePoster poster = repPosters.GetByShowIDAndSeason(season.Trakt_ShowID, season.Season);
						if (poster != null)
						{
							if (!string.IsNullOrEmpty(poster.FullImagePath))
							{
								if (!File.Exists(poster.FullImagePath))
								{
									CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(poster.Trakt_ImagePosterID, JMMImageType.Trakt_Poster, false);
									cmd.Save();
								}
							}
						}
					}

					if (ServerSettings.Trakt_DownloadEpisodes)
					{
						// download the screenshots for episodes
						foreach (Trakt_Episode ep in season.Episodes)
						{
							if (!string.IsNullOrEmpty(ep.FullImagePath))
							{
								if (!File.Exists(ep.FullImagePath))
								{
									CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(ep.Trakt_EpisodeID, JMMImageType.Trakt_Episode, false);
									cmd.Save();
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				logger.ErrorException("Error in TraktTVHelper.UpdateAllInfoAndImages: " + ex.ToString(), ex);
			}
		}
Example #11
0
		public static void SaveShowInfo(TraktTVShow tvshow)
		{
			try
			{
				// save this data to the DB for use later
				Trakt_ImageFanartRepository repFanart = new Trakt_ImageFanartRepository();
				Trakt_ShowRepository repShows = new Trakt_ShowRepository();
				Trakt_Show show = repShows.GetByTraktID(tvshow.TraktID);
				if (show == null)
					show = new Trakt_Show();

				show.Overview = tvshow.overview;
				show.Title = tvshow.title;
				show.TraktID = tvshow.TraktID;
				if (!string.IsNullOrEmpty(tvshow.tvdb_id)) show.TvDB_ID = int.Parse(tvshow.tvdb_id);
				show.URL = tvshow.url;
				show.Year = tvshow.year;
				repShows.Save(show);

				if (tvshow.images != null)
				{
					if (!string.IsNullOrEmpty(tvshow.images.fanart))
					{
						Trakt_ImageFanart fanart = repFanart.GetByShowIDAndSeason(show.Trakt_ShowID, 1);
						if (fanart == null)
						{
							fanart = new Trakt_ImageFanart();
							fanart.Enabled = 1;
						}

						fanart.ImageURL = tvshow.images.fanart;
						fanart.Season = 1;
						fanart.Trakt_ShowID = show.Trakt_ShowID;
						repFanart.Save(fanart);
					}
				}

			}
			catch (Exception ex)
			{
				logger.ErrorException("Error in TraktTVHelper.SaveExtendedShowInfo: " + ex.ToString(), ex);
			}
		}
Example #12
0
		public static void SaveExtendedShowInfo(TraktTVShow tvshow)
		{ 
			try
			{
				// save this data to the DB for use later
				Trakt_ImageFanartRepository repFanart = new Trakt_ImageFanartRepository();
				Trakt_ShowRepository repShows = new Trakt_ShowRepository();
				Trakt_Show show = repShows.GetByTraktID(tvshow.TraktID);
				if (show == null)
					show = new Trakt_Show();

				show.Populate(tvshow);
				repShows.Save(show);


				if (tvshow.images != null)
				{
					if (!string.IsNullOrEmpty(tvshow.images.fanart))
					{
						Trakt_ImageFanart fanart = repFanart.GetByShowIDAndSeason(show.Trakt_ShowID, 1);
						if (fanart == null)
						{
							fanart = new Trakt_ImageFanart();
							fanart.Enabled = 0;
						}

						fanart.ImageURL = tvshow.images.fanart;
						fanart.Season = 1;
						fanart.Trakt_ShowID = show.Trakt_ShowID;
						repFanart.Save(fanart);
					}
				}


				// save the seasons
				Trakt_SeasonRepository repSeasons = new Trakt_SeasonRepository();
				Trakt_EpisodeRepository repEpisodes = new Trakt_EpisodeRepository();
				Trakt_ImagePosterRepository repPosters = new Trakt_ImagePosterRepository();

				foreach (TraktTVSeason sea in tvshow.seasons)
				{
					Trakt_Season season = repSeasons.GetByShowIDAndSeason(show.Trakt_ShowID, int.Parse(sea.season));
					if (season == null)
						season = new Trakt_Season();

					season.Season = int.Parse(sea.season);
					season.URL = sea.url;
					season.Trakt_ShowID = show.Trakt_ShowID;
					repSeasons.Save(season);

					if (sea.images != null)
					{
						if (!string.IsNullOrEmpty(sea.images.poster))
						{
							Trakt_ImagePoster poster = repPosters.GetByShowIDAndSeason(show.Trakt_ShowID, season.Season);
							if (poster == null)
							{
								poster = new Trakt_ImagePoster();
								poster.Enabled = 0;
							}

							poster.ImageURL = sea.images.poster;
							poster.Season = season.Season;
							poster.Trakt_ShowID = show.Trakt_ShowID;
							repPosters.Save(poster);
						}
					}

					foreach (TraktTVEpisode ep in sea.episodes)
					{
						Trakt_Episode episode = repEpisodes.GetByShowIDSeasonAndEpisode(show.Trakt_ShowID, int.Parse(ep.season), int.Parse(ep.episode));
						if (episode == null)
							episode = new Trakt_Episode();

						episode.EpisodeImage = ep.screen;
						episode.EpisodeNumber = int.Parse(ep.episode);
						episode.Overview = ep.overview;
						episode.Season = int.Parse(ep.season);
						episode.Title = ep.title;
						episode.URL = ep.url;
						episode.Trakt_ShowID = show.Trakt_ShowID;
						repEpisodes.Save(episode);
					}
				}


			}
			catch (Exception ex)
			{
				logger.ErrorException("Error in TraktTVHelper.SaveExtendedShowInfo: " + ex.ToString(), ex);
			}
		}
Example #13
0
		public static List<TraktTVUser> GetFriends()
		{
			List<TraktTVUser> friends = new List<TraktTVUser>();

			try
			{
				string url = string.Format(Constants.TraktTvURLs.URLGetFriends, Constants.TraktTvURLs.APIKey, ServerSettings.Trakt_Username);
				//string url = string.Format(Constants.TraktTvURLs.URLGetFriends, Constants.TraktTvURLs.APIKey, "lwerndly");
				logger.Trace("GetFriends: {0}", url);

				TraktTVPost_GetFriends cmdFriends = new TraktTVPost_GetFriends();
				cmdFriends.Init();

				string json = JSONHelper.Serialize<TraktTVPost_GetFriends>(cmdFriends);
				string jsonResponse = SendData(url, json);
				if (jsonResponse.Trim().Length == 0) return friends;
				friends = JSONHelper.Deserialize<List<TraktTVUser>>(jsonResponse);

				/*string json = Utils.DownloadWebPage(url);

				if (json.Trim().Length == 0) return null;

				friends = JSONHelper.Deserialize<List<TraktTVUser>>(json);*/

				Trakt_ShowRepository repShows = new Trakt_ShowRepository();
				Trakt_EpisodeRepository repEpisodes = new Trakt_EpisodeRepository();
				Trakt_FriendRepository repFriends = new Trakt_FriendRepository();

				foreach (TraktTVUser friend in friends)
				{
					Trakt_Friend traktFriend = repFriends.GetByUsername(friend.username);
					if (traktFriend == null)
					{
						traktFriend = new Trakt_Friend();
						traktFriend.LastAvatarUpdate = DateTime.Now;
					}

					traktFriend.Populate(friend);
					repFriends.Save(traktFriend);

					if (!string.IsNullOrEmpty(traktFriend.FullImagePath))
					{
						bool fileExists = File.Exists(traktFriend.FullImagePath);
						TimeSpan ts = DateTime.Now - traktFriend.LastAvatarUpdate;

						if (!fileExists || ts.TotalHours > 8)
						{
							traktFriend.LastAvatarUpdate = DateTime.Now;
							CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(traktFriend.Trakt_FriendID, JMMImageType.Trakt_Friend, true);
							cmd.Save();
						}
					}

					foreach (TraktTVWatched wtch in friend.watched)
					{
						if (wtch.episode != null && wtch.show != null)
						{

							Trakt_Show show = repShows.GetByTraktID(wtch.show.TraktID);
							if (show == null)
							{
								show = new Trakt_Show();
								show.Populate(wtch.show);
								repShows.Save(show);
							}

							Trakt_Episode episode = repEpisodes.GetByShowIDSeasonAndEpisode(show.Trakt_ShowID, int.Parse(wtch.episode.season), int.Parse(wtch.episode.number));
							if (episode == null)
								episode = new Trakt_Episode();

							episode.Populate(wtch.episode, show.Trakt_ShowID);
							repEpisodes.Save(episode);

							if (!string.IsNullOrEmpty(episode.FullImagePath))
							{
								bool fileExists = File.Exists(episode.FullImagePath);
								if (!fileExists)
								{
									CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(episode.Trakt_EpisodeID, JMMImageType.Trakt_Episode, false);
									cmd.Save();
								}
							}
						}
					}
				}

				

				//Contract_Trakt_Friend fr = friends[0].ToContract();

			}
			catch (Exception ex)
			{
				logger.ErrorException("Error in TraktTVHelper.GetFriends: " + ex.ToString(), ex);
				return friends;
			}

			return friends;
		}
Example #14
0
		public static TraktTV_ActivitySummary GetActivityFriends(bool shoutsOnly)
		{
			TraktTV_ActivitySummary summ = null;
			try
			{
				if (string.IsNullOrEmpty(ServerSettings.Trakt_Username) || string.IsNullOrEmpty(ServerSettings.Trakt_Password))
					return null;

				string url = string.Format(Constants.TraktTvURLs.URLGetActivityFriends, Constants.TraktTvURLs.APIKey);
				if (shoutsOnly)
					url = string.Format(Constants.TraktTvURLs.URLGetActivityFriendsShoutsOnly, Constants.TraktTvURLs.APIKey);
				logger.Trace("GetActivityFriends: {0}", url);

				TraktTVPost_GetFriends cmdFriends = new TraktTVPost_GetFriends();
				cmdFriends.Init();

				string json = JSONHelper.Serialize<TraktTVPost_GetFriends>(cmdFriends); // TraktTVPost_GetFriends is really just an auth method
				string jsonResponse = SendData(url, json);
				if (jsonResponse.Trim().Length == 0) return null;

				summ = JSONHelper.Deserialize<TraktTV_ActivitySummary>(jsonResponse);
				if (summ == null) return null;


				// save any trakt data that we don't have already
				Trakt_ShowRepository repShows = new Trakt_ShowRepository();
				Trakt_EpisodeRepository repEpisodes = new Trakt_EpisodeRepository();
				Trakt_FriendRepository repFriends = new Trakt_FriendRepository();

				foreach (TraktTV_Activity act in summ.activity)
				{
					if (act.user == null) continue;
					TraktTV_UserActivity friend = act.user;

					Trakt_Friend traktFriend = repFriends.GetByUsername(friend.username);
					if (traktFriend == null)
					{
						traktFriend = new Trakt_Friend();
						traktFriend.LastAvatarUpdate = DateTime.Now;
					}

					traktFriend.Populate(friend);
					repFriends.Save(traktFriend);

					if (!string.IsNullOrEmpty(traktFriend.FullImagePath))
					{
						bool fileExists = File.Exists(traktFriend.FullImagePath);
						TimeSpan ts = DateTime.Now - traktFriend.LastAvatarUpdate;

						if (!fileExists || ts.TotalHours > 8)
						{
							traktFriend.LastAvatarUpdate = DateTime.Now;
							CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(traktFriend.Trakt_FriendID, JMMImageType.Trakt_Friend, true);
							cmd.Save();
						}
					}

					if (act.episode != null && act.show != null)
					{
						Trakt_Show show = repShows.GetByTraktID(act.show.TraktID);
						if (show == null)
						{
							show = new Trakt_Show();
							show.Populate(act.show);
							repShows.Save(show);
						}

						Trakt_Episode episode = repEpisodes.GetByShowIDSeasonAndEpisode(show.Trakt_ShowID, int.Parse(act.episode.season), int.Parse(act.episode.number));
						if (episode == null)
							episode = new Trakt_Episode();

						episode.Populate(act.episode, show.Trakt_ShowID);
						repEpisodes.Save(episode);

						if (!string.IsNullOrEmpty(episode.FullImagePath))
						{
							bool fileExists = File.Exists(episode.FullImagePath);
							if (!fileExists)
							{
								CommandRequest_DownloadImage cmd = new CommandRequest_DownloadImage(episode.Trakt_EpisodeID, JMMImageType.Trakt_Episode, false);
								cmd.Save();
							}
						}
					}

					// a shout on just the show
					if (act.episode == null && act.show != null)
					{
						Trakt_Show show = repShows.GetByTraktID(act.show.TraktID);
						if (show == null)
						{
							show = new Trakt_Show();
							show.Populate(act.show);
							repShows.Save(show);
						}
					}
				}

			}
			catch (Exception ex)
			{
				logger.ErrorException("Error in TraktTVHelper.GetActivityFriends: " + ex.ToString(), ex);
			}

			return summ;
		}
Example #15
0
		public static void SyncCollectionToTrakt_Series(AnimeSeries series)
		{
			try
			{
				// check that we have at least one user nominated for Trakt
				JMMUserRepository repUsers = new JMMUserRepository();
				List<JMMUser> traktUsers = repUsers.GetTraktUsers();
				if (traktUsers.Count == 0) return;

				string url = string.Format(Constants.TraktTvURLs.URLPostShowEpisodeLibrary, Constants.TraktTvURLs.APIKey);
				string urlSeen = string.Format(Constants.TraktTvURLs.URLPostShowEpisodeSeen, Constants.TraktTvURLs.APIKey);

				int retEpNum = 0, retSeason = 0;

				CrossRef_AniDB_Trakt xref = series.CrossRefTrakt;
				if (xref == null) return;

				Trakt_ShowRepository repShows = new Trakt_ShowRepository();
				Trakt_Show show = repShows.GetByTraktID(xref.TraktID);
				if (show == null) return;
				if (!show.TvDB_ID.HasValue) return;

				Dictionary<int, int> dictTraktSeasons = null;
				Dictionary<int, Trakt_Episode> dictTraktEpisodes = null;
				Dictionary<int, Trakt_Episode> dictTraktSpecials = null;
				GetDictTraktEpisodesAndSeasons(show, ref dictTraktEpisodes, ref dictTraktSpecials, ref dictTraktSeasons);


				TraktTVPost_ShowEpisodeLibrary postLibrary = new TraktTVPost_ShowEpisodeLibrary();
				postLibrary.episodes = new List<TraktTVSeasonEpisode>();
				postLibrary.SetCredentials();
				postLibrary.imdb_id = "";
				postLibrary.title = show.Title;
				postLibrary.year = show.Year;
				postLibrary.tvdb_id = show.TvDB_ID.Value.ToString();

				TraktTVPost_ShowEpisodeSeen postSeen = new TraktTVPost_ShowEpisodeSeen();
				postSeen.episodes = new List<TraktTVSeasonEpisode>();
				postSeen.SetCredentials();
				postSeen.imdb_id = "";
				postSeen.title = show.Title;
				postSeen.year = show.Year;
				postSeen.tvdb_id = show.TvDB_ID.Value.ToString();

				foreach (AnimeEpisode ep in series.GetAnimeEpisodes())
				{
					if (ep.GetVideoLocals().Count > 0)
					{
						retEpNum = -1;
						retSeason = -1;

						GetTraktEpisodeNumber(ep, series, show, xref.TraktSeasonNumber, ref retEpNum, ref retSeason, dictTraktEpisodes, dictTraktSpecials, dictTraktSeasons);
						if (retEpNum < 0) continue;

						TraktTVSeasonEpisode traktEp = new TraktTVSeasonEpisode();
						traktEp.episode = retEpNum.ToString();
						traktEp.season = retSeason.ToString();
						postLibrary.episodes.Add(traktEp);

						AnimeEpisode_User userRecord = null;
						foreach (JMMUser juser in traktUsers)
						{
							userRecord = ep.GetUserRecord(juser.JMMUserID);
							if (userRecord != null) break;
						}

						if (userRecord != null) 
							postSeen.episodes.Add(traktEp);
					}
				}

				if (postLibrary.episodes.Count > 0)
				{
					logger.Info("PostShowEpisodeLibrary: {0}/{1}/{2} eps", show.Title, show.TraktID, postLibrary.episodes.Count);

					string json = JSONHelper.Serialize<TraktTVPost_ShowEpisodeLibrary>(postLibrary);
					string jsonResponse = SendData(url, json);
					logger.Info("PostShowEpisodeLibrary RESPONSE: {0}", jsonResponse);
				}

				if (postSeen.episodes.Count > 0)
				{
					logger.Info("PostShowEpisodeSeen: {0}/{1}/{2} eps", show.Title, show.TraktID, postSeen.episodes.Count);

					string json = JSONHelper.Serialize<TraktTVPost_ShowEpisodeSeen>(postSeen);
					string jsonResponse = SendData(urlSeen, json);
					logger.Info("PostShowEpisodeSeen RESPONSE: {0}", jsonResponse);
				}
			}
			catch (Exception ex)
			{
				logger.ErrorException("Error in TraktTVHelper.SyncCollectionToTrakt_Series: " + ex.ToString(), ex);
			}
		}