Beispiel #1
0
        public static List<TraktV2Follower> GetFriendsV2()
        {
            List<TraktV2Follower> friends = new List<TraktV2Follower>();

            try
            {
                if (!ServerSettings.Trakt_IsEnabled || string.IsNullOrEmpty(ServerSettings.Trakt_AuthToken))
                    return friends;

                string url = TraktURIs.GetUserFriends;
                logger.Trace("GetFollowers: {0}", url);

                string json = GetFromTrakt(url);
                if (string.IsNullOrEmpty(json)) return null;

                var resultFollowers = json.FromJSONArray<TraktV2Follower>();

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

                foreach (TraktV2Follower friend in resultFollowers)
                {
                    Trakt_Friend traktFriend = repFriends.GetByUsername(friend.user.username);
                    if (traktFriend == null)
                        traktFriend = new Trakt_Friend();

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

                    // get a watched history for each friend
                    url = string.Format(TraktURIs.GetUserHistory, friend.user.username);
                    logger.Trace("GetUserHistory: {0}", url);

                    json = GetFromTrakt(url);
                    if (string.IsNullOrEmpty(json)) continue;

                    var resultHistory = json.FromJSONArray<TraktV2UserEpisodeHistory>();

                    /*
                    foreach (TraktV2UserEpisodeHistory wtch in resultHistory)
                    {
                        if (wtch.episode != null && wtch.show != null)
                        {

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

                            Trakt_Episode episode = repEpisodes.GetByShowIDSeasonAndEpisode(show.Trakt_ShowID, wtch.episode.season, 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;
        }
Beispiel #2
0
        public List<Contract_Trakt_CommentUser> GetTraktCommentsForAnime(int animeID)
        {
            List<Contract_Trakt_CommentUser> comments = new List<Contract_Trakt_CommentUser>();

            try
            {
                Trakt_FriendRepository repFriends = new Trakt_FriendRepository();

                List<TraktV2Comment> commentsTemp = TraktTVHelper.GetShowCommentsV2(animeID);
                if (commentsTemp == null || commentsTemp.Count == 0) return comments;

                foreach (TraktV2Comment sht in commentsTemp)
                {
                    Contract_Trakt_CommentUser comment = new Contract_Trakt_CommentUser();

                    Trakt_Friend traktFriend = repFriends.GetByUsername(sht.user.username);

                    // user details
                    comment.User = new Contract_Trakt_User();
                    if (traktFriend == null)
                        comment.User.Trakt_FriendID = 0;
                    else
                        comment.User.Trakt_FriendID = traktFriend.Trakt_FriendID;

                    comment.User.Username = sht.user.username;
                    comment.User.Full_name = sht.user.name;

                    // comment details
                    comment.Comment = new Contract_Trakt_Comment();
                    comment.Comment.CommentType = (int)TraktActivityType.Show; // episode or show
                    comment.Comment.Text = sht.comment;
                    comment.Comment.Spoiler = sht.spoiler;
                    comment.Comment.Inserted = sht.CreatedAtDate;

                    // urls
                    comment.Comment.Comment_Url = string.Format(TraktURIs.WebsiteComment, sht.id);

                    comments.Add(comment);
                }
            }
            catch (Exception ex)
            {
                logger.ErrorException(ex.ToString(), ex);
            }
            return comments;
        }
		public System.IO.Stream GetImage(string ImageType, string ImageID)
		{
			AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository();
			TvDB_ImagePosterRepository repPosters = new TvDB_ImagePosterRepository();
			TvDB_EpisodeRepository repEpisodes = new TvDB_EpisodeRepository();
			TvDB_ImageFanartRepository repFanart = new TvDB_ImageFanartRepository();
			TvDB_ImageWideBannerRepository repWideBanners = new TvDB_ImageWideBannerRepository();

			MovieDB_PosterRepository repMoviePosters = new MovieDB_PosterRepository();
			MovieDB_FanartRepository repMovieFanart = new MovieDB_FanartRepository();

			Trakt_ImageFanartRepository repTraktFanart = new Trakt_ImageFanartRepository();
			Trakt_ImagePosterRepository repTraktPosters = new Trakt_ImagePosterRepository();
			Trakt_EpisodeRepository repTraktEpisodes = new Trakt_EpisodeRepository();
			Trakt_FriendRepository repTraktFriends = new Trakt_FriendRepository();

			JMMImageType imageType = (JMMImageType)int.Parse(ImageType);

			switch (imageType)
			{
				case JMMImageType.AniDB_Cover:

					AniDB_Anime anime = repAnime.GetByAnimeID(int.Parse(ImageID));
					if (anime == null) return null;

					if (File.Exists(anime.PosterPath))
					{
						FileStream fs = File.OpenRead(anime.PosterPath);
						WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
						return fs;
					}
					else
					{
						logger.Trace("Could not find AniDB_Cover image: {0}", anime.PosterPath);
						return null;
					}

				case JMMImageType.AniDB_Character:

					AniDB_CharacterRepository repChar = new AniDB_CharacterRepository();
					AniDB_Character chr = repChar.GetByID(int.Parse(ImageID));
					if (chr == null) return null;

					if (File.Exists(chr.PosterPath))
					{
						FileStream fs = File.OpenRead(chr.PosterPath);
						WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
						return fs;
					}
					else
					{
						logger.Trace("Could not find AniDB_Character image: {0}", chr.PosterPath);
						return null;
					}

				case JMMImageType.AniDB_Creator:

					AniDB_SeiyuuRepository repCreator = new AniDB_SeiyuuRepository();
					AniDB_Seiyuu creator = repCreator.GetByID(int.Parse(ImageID));
					if (creator == null) return null;

					if (File.Exists(creator.PosterPath))
					{
						FileStream fs = File.OpenRead(creator.PosterPath);
						WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
						return fs;
					}
					else
					{
						logger.Trace("Could not find AniDB_Creator image: {0}", creator.PosterPath);
						return null;
					}

				case JMMImageType.TvDB_Cover:

					TvDB_ImagePoster poster = repPosters.GetByID(int.Parse(ImageID));
					if (poster == null) return null;

					if (File.Exists(poster.FullImagePath))
					{
						FileStream fs = File.OpenRead(poster.FullImagePath);
						WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
						return fs;
					}
					else
					{
						logger.Trace("Could not find TvDB_Cover image: {0}", poster.FullImagePath);
						return null;
					}

				case JMMImageType.TvDB_Banner:

					TvDB_ImageWideBanner wideBanner = repWideBanners.GetByID(int.Parse(ImageID));
					if (wideBanner == null) return null;

					if (File.Exists(wideBanner.FullImagePath))
					{
						FileStream fs = File.OpenRead(wideBanner.FullImagePath);
						WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
						return fs;
					}
					else
					{
						logger.Trace("Could not find TvDB_Banner image: {0}", wideBanner.FullImagePath);
						return null;
					}

				case JMMImageType.TvDB_Episode:

					TvDB_Episode ep = repEpisodes.GetByID(int.Parse(ImageID));
					if (ep == null) return null;

					if (File.Exists(ep.FullImagePath))
					{
						FileStream fs = File.OpenRead(ep.FullImagePath);
						WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
						return fs;
					}
					else
					{
						logger.Trace("Could not find TvDB_Episode image: {0}", ep.FullImagePath);
						return null;
					}

				case JMMImageType.TvDB_FanArt:

					TvDB_ImageFanart fanart = repFanart.GetByID(int.Parse(ImageID));
					if (fanart == null) return null;

					if (File.Exists(fanart.FullImagePath))
					{
						FileStream fs = File.OpenRead(fanart.FullImagePath);
						WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
						return fs;
					}
					else
					{
						logger.Trace("Could not find TvDB_FanArt image: {0}", fanart.FullImagePath);
						return null;
					}

				case JMMImageType.MovieDB_Poster:

					MovieDB_Poster mPoster = repMoviePosters.GetByID(int.Parse(ImageID));
					if (mPoster == null) return null;

					// now find only the original size
					mPoster = repMoviePosters.GetByOnlineID(mPoster.ImageID, Constants.MovieDBImageSize.Original);
					if (mPoster == null) return null;

					if (File.Exists(mPoster.FullImagePath))
					{
						FileStream fs = File.OpenRead(mPoster.FullImagePath);
						WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
						return fs;
					}
					else
					{
						logger.Trace("Could not find MovieDB_Poster image: {0}", mPoster.FullImagePath);
						return null;
					}

				case JMMImageType.MovieDB_FanArt:

					MovieDB_Fanart mFanart = repMovieFanart.GetByID(int.Parse(ImageID));
					if (mFanart == null) return null;

					mFanart = repMovieFanart.GetByOnlineID(mFanart.ImageID, Constants.MovieDBImageSize.Original);
					if (mFanart == null) return null;

					if (File.Exists(mFanart.FullImagePath))
					{
						FileStream fs = File.OpenRead(mFanart.FullImagePath);
						WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
						return fs;
					}
					else
					{
						logger.Trace("Could not find MovieDB_FanArt image: {0}", mFanart.FullImagePath);
						return null;
					}

				case JMMImageType.Trakt_Fanart:

					Trakt_ImageFanart tFanart = repTraktFanart.GetByID(int.Parse(ImageID));
					if (tFanart == null) return null;

					if (File.Exists(tFanart.FullImagePath))
					{
						FileStream fs = File.OpenRead(tFanart.FullImagePath);
						WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
						return fs;
					}
					else
					{
						logger.Trace("Could not find Trakt_Fanart image: {0}", tFanart.FullImagePath);
						return null;
					}

				case JMMImageType.Trakt_Friend:


					Trakt_Friend tFriend = repTraktFriends.GetByID(int.Parse(ImageID));
					if (tFriend == null) return null;

					if (File.Exists(tFriend.FullImagePath))
					{
						FileStream fs = File.OpenRead(tFriend.FullImagePath);
						WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
						return fs;
					}
					else
					{
						logger.Trace("Could not find Trakt_Friend image: {0}", tFriend.FullImagePath);
						return null;
					}

				case JMMImageType.Trakt_ActivityScrobble:
				case JMMImageType.Trakt_ShoutUser:


					Trakt_Friend tFriendScrobble = repTraktFriends.GetByID(int.Parse(ImageID));
					if (tFriendScrobble == null) return null;

					if (File.Exists(tFriendScrobble.FullImagePath))
					{
						FileStream fs = File.OpenRead(tFriendScrobble.FullImagePath);
						WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
						return fs;
					}
					else
					{
						logger.Trace("Could not find Trakt_ActivityScrobble image: {0}", tFriendScrobble.FullImagePath);
						return null;
					}

				case JMMImageType.Trakt_Poster:

					Trakt_ImagePoster tPoster = repTraktPosters.GetByID(int.Parse(ImageID));
					if (tPoster == null) return null;

					if (File.Exists(tPoster.FullImagePath))
					{
						FileStream fs = File.OpenRead(tPoster.FullImagePath);
						WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
						return fs;
					}
					else
					{
						logger.Trace("Could not find Trakt_Poster image: {0}", tPoster.FullImagePath);
						return null;
					}

				case JMMImageType.Trakt_Episode:
				case JMMImageType.Trakt_WatchedEpisode:

					Trakt_Episode tEpisode = repTraktEpisodes.GetByID(int.Parse(ImageID));
					if (tEpisode == null) return null;

					if (File.Exists(tEpisode.FullImagePath))
					{
						FileStream fs = File.OpenRead(tEpisode.FullImagePath);
						WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
						return fs;
					}
					else
					{
						logger.Trace("Could not find Trakt_Episode image: {0}", tEpisode.FullImagePath);
						return null;
					}

				default:

					return null;
			}



			
		}
		public List<MetroContract_Comment> GetTraktCommentsForAnime(int animeID, int maxRecords)
		{
			List<MetroContract_Comment> comments = new List<MetroContract_Comment>();

			try
			{
				using (var session = JMMService.SessionFactory.OpenSession())
				{
					Trakt_FriendRepository repFriends = new Trakt_FriendRepository();

                    List<TraktV2Comment> commentsTemp = TraktTVHelper.GetShowCommentsV2(session, animeID);

					if (commentsTemp == null || commentsTemp.Count == 0) return comments;

					int cnt = 0;
                    foreach (TraktV2Comment sht in commentsTemp)
					{
						MetroContract_Comment comment = new MetroContract_Comment();

						Trakt_Friend traktFriend = repFriends.GetByUsername(session, sht.user.username);

						// user details
						Contract_Trakt_User user = new Contract_Trakt_User();
						if (traktFriend == null)
							comment.UserID = 0;
						else
							comment.UserID = traktFriend.Trakt_FriendID;

						comment.UserName = sht.user.username;

						// shout details
						comment.CommentText = sht.comment;
						comment.IsSpoiler = sht.spoiler;
                        comment.CommentDate = sht.CreatedAtDate;

						//shout.ImageURL = sht.user.avatar;
						comment.CommentType = (int)WhatPeopleAreSayingType.TraktComment;
						comment.Source = "Trakt";

						cnt++;
						comments.Add(comment);

						if (cnt == maxRecords) break;
					}

					if (comments.Count > 0)
					{
						List<SortPropOrFieldAndDirection> sortCriteria = new List<SortPropOrFieldAndDirection>();
						sortCriteria.Add(new SortPropOrFieldAndDirection("ShoutDate", false, SortType.eDateTime));
						comments = Sorting.MultiSort<MetroContract_Comment>(comments, sortCriteria);
					}
				}
			}
			catch (Exception ex)
			{
				logger.ErrorException(ex.ToString(), ex);
			}
			return comments;
		}
		public byte[] GetImage(string entityID, int entityType, bool thumnbnailOnly)
		{
			AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository();
			TvDB_ImagePosterRepository repPosters = new TvDB_ImagePosterRepository();
			TvDB_EpisodeRepository repEpisodes = new TvDB_EpisodeRepository();
			TvDB_ImageFanartRepository repFanart = new TvDB_ImageFanartRepository();
			TvDB_ImageWideBannerRepository repWideBanners = new TvDB_ImageWideBannerRepository();

			MovieDB_PosterRepository repMoviePosters = new MovieDB_PosterRepository();
			MovieDB_FanartRepository repMovieFanart = new MovieDB_FanartRepository();

			Trakt_ImageFanartRepository repTraktFanart = new Trakt_ImageFanartRepository();
			Trakt_ImagePosterRepository repTraktPosters = new Trakt_ImagePosterRepository();
			Trakt_EpisodeRepository repTraktEpisodes = new Trakt_EpisodeRepository();
			Trakt_FriendRepository repTraktFriends = new Trakt_FriendRepository();

			JMMImageType imageType = (JMMImageType)entityType;

			switch (imageType)
			{
				case JMMImageType.AniDB_Cover:

					AniDB_Anime anime = repAnime.GetByAnimeID(int.Parse(entityID));
					if (anime == null) return null;

					if (File.Exists(anime.PosterPath))
						return File.ReadAllBytes(anime.PosterPath);
					else
					{
						logger.Trace("Could not find AniDB_Cover image: {0}", anime.PosterPath);
						return null;
					}

				case JMMImageType.AniDB_Character:

					AniDB_CharacterRepository repChar = new AniDB_CharacterRepository();
					AniDB_Character chr = repChar.GetByID(int.Parse(entityID));
					if (chr == null) return null;

					if (File.Exists(chr.PosterPath))
						return File.ReadAllBytes(chr.PosterPath);
					else
					{
						logger.Trace("Could not find AniDB_Character image: {0}", chr.PosterPath);
						return null;
					}

				case JMMImageType.AniDB_Creator:

					AniDB_SeiyuuRepository repCreator = new AniDB_SeiyuuRepository();
					AniDB_Seiyuu creator = repCreator.GetByID(int.Parse(entityID));
					if (creator == null) return null;

					if (File.Exists(creator.PosterPath))
						return File.ReadAllBytes(creator.PosterPath);
					else
					{
						logger.Trace("Could not find AniDB_Creator image: {0}", creator.PosterPath);
						return null;
					}

				case JMMImageType.TvDB_Cover:

					TvDB_ImagePoster poster = repPosters.GetByID(int.Parse(entityID));
					if (poster == null) return null;

					if (File.Exists(poster.FullImagePath))
						return File.ReadAllBytes(poster.FullImagePath);
					else
					{
						logger.Trace("Could not find TvDB_Cover image: {0}", poster.FullImagePath);
						return null;
					}

				case JMMImageType.TvDB_Banner:

					TvDB_ImageWideBanner wideBanner = repWideBanners.GetByID(int.Parse(entityID));
					if (wideBanner == null) return null;

					if (File.Exists(wideBanner.FullImagePath))
						return File.ReadAllBytes(wideBanner.FullImagePath);
					else
					{
						logger.Trace("Could not find TvDB_Banner image: {0}", wideBanner.FullImagePath);
						return null;
					}

				case JMMImageType.TvDB_Episode:

					TvDB_Episode ep = repEpisodes.GetByID(int.Parse(entityID));
					if (ep == null) return null;

					if (File.Exists(ep.FullImagePath))
						return File.ReadAllBytes(ep.FullImagePath);
					else
					{
						logger.Trace("Could not find TvDB_Episode image: {0}", ep.FullImagePath);
						return null;
					}

				case JMMImageType.TvDB_FanArt:

					TvDB_ImageFanart fanart = repFanart.GetByID(int.Parse(entityID));
					if (fanart == null) return null;

					if (thumnbnailOnly)
					{
						if (File.Exists(fanart.FullThumbnailPath))
							return File.ReadAllBytes(fanart.FullThumbnailPath);
						else
						{
							logger.Trace("Could not find TvDB_FanArt image: {0}", fanart.FullThumbnailPath);
							return null;
						}
					}
					else
					{
						if (File.Exists(fanart.FullImagePath))
							return File.ReadAllBytes(fanart.FullImagePath);
						else
						{
							logger.Trace("Could not find TvDB_FanArt image: {0}", fanart.FullImagePath);
							return null;
						}
					}

				case JMMImageType.MovieDB_Poster:

					MovieDB_Poster mPoster = repMoviePosters.GetByID(int.Parse(entityID));
					if (mPoster == null) return null;

					// now find only the original size
                    mPoster = repMoviePosters.GetByOnlineID(mPoster.URL);
					if (mPoster == null) return null;

					if (File.Exists(mPoster.FullImagePath))
						return File.ReadAllBytes(mPoster.FullImagePath);
					else
					{
						logger.Trace("Could not find MovieDB_Poster image: {0}", mPoster.FullImagePath);
						return null;
					}

				case JMMImageType.MovieDB_FanArt:

					MovieDB_Fanart mFanart = repMovieFanart.GetByID(int.Parse(entityID));
					if (mFanart == null) return null;

					mFanart = repMovieFanart.GetByOnlineID(mFanart.URL);
					if (mFanart == null) return null;

					if (File.Exists(mFanart.FullImagePath))
						return File.ReadAllBytes(mFanart.FullImagePath);
					else
					{
						logger.Trace("Could not find MovieDB_FanArt image: {0}", mFanart.FullImagePath);
						return null;
					}

				case JMMImageType.Trakt_Fanart:

					Trakt_ImageFanart tFanart = repTraktFanart.GetByID(int.Parse(entityID));
					if (tFanart == null) return null;

					if (File.Exists(tFanart.FullImagePath))
						return File.ReadAllBytes(tFanart.FullImagePath);
					else
					{
						logger.Trace("Could not find Trakt_Fanart image: {0}", tFanart.FullImagePath);
						return null;
					}

				case JMMImageType.Trakt_Poster:

					Trakt_ImagePoster tPoster = repTraktPosters.GetByID(int.Parse(entityID));
					if (tPoster == null) return null;

					if (File.Exists(tPoster.FullImagePath))
						return File.ReadAllBytes(tPoster.FullImagePath);
					else
					{
						logger.Trace("Could not find Trakt_Poster image: {0}", tPoster.FullImagePath);
						return null;
					}

				case JMMImageType.Trakt_Episode:
				case JMMImageType.Trakt_WatchedEpisode:

					Trakt_Episode tEpisode = repTraktEpisodes.GetByID(int.Parse(entityID));
					if (tEpisode == null) return null;

					if (File.Exists(tEpisode.FullImagePath))
						return File.ReadAllBytes(tEpisode.FullImagePath);
					else
					{
						logger.Trace("Could not find Trakt_Episode image: {0}", tEpisode.FullImagePath);
						return null;
					}

				default:

					return null;
			}

			

		}
		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;
		}
Beispiel #7
0
        public override void ProcessCommand()
        {
            logger.Info("Processing CommandRequest_DownloadImage: {0}", EntityID);
            string downloadURL = "";
            try
            {
                ImageDownloadRequest req = null;
                switch (EntityTypeEnum)
                {
                    case JMMImageType.AniDB_Cover:
                        AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository();
                        AniDB_Anime anime = repAnime.GetByID(EntityID);
                        if (anime == null) return;

                        req = new ImageDownloadRequest(EntityTypeEnum, anime, ForceDownload);
                        break;

                    case JMMImageType.TvDB_Episode:

                        TvDB_EpisodeRepository repTvEp = new TvDB_EpisodeRepository();
                        TvDB_Episode ep = repTvEp.GetByID(EntityID);
                        if (ep == null) return;
                        if (string.IsNullOrEmpty(ep.Filename)) return;

                        req = new ImageDownloadRequest(EntityTypeEnum, ep, ForceDownload);
                        break;

                    case JMMImageType.TvDB_FanArt:

                        TvDB_ImageFanartRepository repFanart = new TvDB_ImageFanartRepository();
                        TvDB_ImageFanart fanart = repFanart.GetByID(EntityID);
                        if (fanart == null) return;
                        if (string.IsNullOrEmpty(fanart.BannerPath)) return;

                        req = new ImageDownloadRequest(EntityTypeEnum, fanart, ForceDownload);
                        break;

                    case JMMImageType.TvDB_Cover:

                        TvDB_ImagePosterRepository repPoster = new TvDB_ImagePosterRepository();
                        TvDB_ImagePoster poster = repPoster.GetByID(EntityID);
                        if (poster == null) return;
                        if (string.IsNullOrEmpty(poster.BannerPath)) return;

                        req = new ImageDownloadRequest(EntityTypeEnum, poster, ForceDownload);
                        break;

                    case JMMImageType.TvDB_Banner:

                        TvDB_ImageWideBannerRepository repBanners = new TvDB_ImageWideBannerRepository();
                        TvDB_ImageWideBanner wideBanner = repBanners.GetByID(EntityID);
                        if (wideBanner == null) return;
                        if (string.IsNullOrEmpty(wideBanner.BannerPath)) return;

                        req = new ImageDownloadRequest(EntityTypeEnum, wideBanner, ForceDownload);
                        break;

                    case JMMImageType.MovieDB_Poster:

                        MovieDB_PosterRepository repMoviePosters = new MovieDB_PosterRepository();
                        MovieDB_Poster moviePoster = repMoviePosters.GetByID(EntityID);
                        if (moviePoster == null) return;
                        if (string.IsNullOrEmpty(moviePoster.URL)) return;

                        req = new ImageDownloadRequest(EntityTypeEnum, moviePoster, ForceDownload);
                        break;

                    case JMMImageType.MovieDB_FanArt:

                        MovieDB_FanartRepository repMovieFanart = new MovieDB_FanartRepository();
                        MovieDB_Fanart movieFanart = repMovieFanart.GetByID(EntityID);
                        if (movieFanart == null) return;
                        if (string.IsNullOrEmpty(movieFanart.URL)) return;

                        req = new ImageDownloadRequest(EntityTypeEnum, movieFanart, ForceDownload);
                        break;

                    case JMMImageType.Trakt_Poster:

                        Trakt_ImagePosterRepository repTraktPosters = new Trakt_ImagePosterRepository();
                        Trakt_ImagePoster traktPoster = repTraktPosters.GetByID(EntityID);
                        if (traktPoster == null) return;
                        if (string.IsNullOrEmpty(traktPoster.ImageURL)) return;

                        req = new ImageDownloadRequest(EntityTypeEnum, traktPoster, ForceDownload);
                        break;

                    case JMMImageType.Trakt_Fanart:

                        Trakt_ImageFanartRepository repTraktFanarts = new Trakt_ImageFanartRepository();
                        Trakt_ImageFanart traktFanart = repTraktFanarts.GetByID(EntityID);
                        if (traktFanart == null) return;
                        if (string.IsNullOrEmpty(traktFanart.ImageURL)) return;

                        req = new ImageDownloadRequest(EntityTypeEnum, traktFanart, ForceDownload);
                        break;

                    case JMMImageType.Trakt_Friend:

                        Trakt_FriendRepository repFriends = new Trakt_FriendRepository();
                        Trakt_Friend friend = repFriends.GetByID(EntityID);
                        if (friend == null) return;
                        if (string.IsNullOrEmpty(friend.Avatar)) return;

                        req = new ImageDownloadRequest(EntityTypeEnum, friend, ForceDownload);
                        break;

                    case JMMImageType.Trakt_Episode:

                        Trakt_EpisodeRepository repTraktEpisodes = new Trakt_EpisodeRepository();
                        Trakt_Episode traktEp = repTraktEpisodes.GetByID(EntityID);
                        if (traktEp == null) return;
                        if (string.IsNullOrEmpty(traktEp.EpisodeImage)) return;

                        req = new ImageDownloadRequest(EntityTypeEnum, traktEp, ForceDownload);
                        break;

                    case JMMImageType.AniDB_Character:
                        AniDB_CharacterRepository repChars = new AniDB_CharacterRepository();
                        AniDB_Character chr = repChars.GetByID(EntityID);
                        if (chr == null) return;

                        req = new ImageDownloadRequest(EntityTypeEnum, chr, ForceDownload);
                        break;

                    case JMMImageType.AniDB_Creator:
                        AniDB_SeiyuuRepository repCreator = new AniDB_SeiyuuRepository();
                        AniDB_Seiyuu creator = repCreator.GetByID(EntityID);
                        if (creator == null) return;

                        req = new ImageDownloadRequest(EntityTypeEnum, creator, ForceDownload);
                        break;
                }

                if (req == null) return;

                List<string> fileNames = new List<string>();
                List<string> downloadURLs = new List<string>();

                string fileNameTemp = GetFileName(req, false);
                string downloadURLTemp = GetFileURL(req, false);

                fileNames.Add(fileNameTemp);
                downloadURLs.Add(downloadURLTemp);

                if (req.ImageType == JMMImageType.TvDB_FanArt)
                {
                    fileNameTemp = GetFileName(req, true);
                    downloadURLTemp = GetFileURL(req, true);

                    fileNames.Add(fileNameTemp);
                    downloadURLs.Add(downloadURLTemp);
                }

                for (int i = 0; i < fileNames.Count; i++)
                {
                    string fileName = fileNames[i];
                    downloadURL = downloadURLs[i];

                    bool downloadImage = true;
                    bool fileExists = File.Exists(fileName);

                    if (fileExists)
                    {
                        if (!req.ForceDownload)
                            downloadImage = false;
                        else
                            downloadImage = true;
                    }
                    else
                        downloadImage = true;

                    if (downloadImage)
                    {
                        string tempName = Path.Combine(ImageUtils.GetImagesTempFolder(), Path.GetFileName(fileName));
                        if (File.Exists(tempName)) File.Delete(tempName);

                        try
                        {
                            if (fileExists) File.Delete(fileName);
                        }
                        catch (Exception ex)
                        {
                            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(ServerSettings.Culture);

                            string msg = string.Format(JMMServer.Properties.Resources.Command_DeleteError, fileName, ex.Message);
                            logger.Warn(msg);
                            return;
                        }

                        // download image
                        using (WebClient client = new WebClient())
                        {
                            client.Headers.Add("user-agent", "JMM");
                            //OnImageDownloadEvent(new ImageDownloadEventArgs("", req, ImageDownloadEventType.Started));
                            //BaseConfig.MyAnimeLog.Write("ProcessImages: Download: {0}  *** to ***  {1}", req.URL, fullName);
                            if (downloadURL.Length > 0)
                            {
                                client.DownloadFile(downloadURL, tempName);

                                string extension = "";
                                string contentType = client.ResponseHeaders["Content-type"].ToLower();
                                if (contentType.IndexOf("gif") >= 0) extension = ".gif";
                                if (contentType.IndexOf("jpg") >= 0) extension = ".jpg";
                                if (contentType.IndexOf("jpeg") >= 0) extension = ".jpg";
                                if (contentType.IndexOf("bmp") >= 0) extension = ".bmp";
                                if (contentType.IndexOf("png") >= 0) extension = ".png";
                                if (extension.Length > 0)
                                {
                                    string newFile = Path.ChangeExtension(tempName, extension);
                                    if (!newFile.ToLower().Equals(tempName.ToLower()))
                                    {
                                        try
                                        {
                                            System.IO.File.Delete(newFile);
                                        }
                                        catch
                                        {
                                            //BaseConfig.MyAnimeLog.Write("DownloadedImage:Download() Delete failed:{0}", newFile);
                                        }
                                        System.IO.File.Move(tempName, newFile);
                                        tempName = newFile;
                                    }
                                }
                            }
                        }

                        // move the file to it's final location
                        // check that the final folder exists
                        string fullPath = Path.GetDirectoryName(fileName);
                        if (!Directory.Exists(fullPath))
                            Directory.CreateDirectory(fullPath);

                        System.IO.File.Move(tempName, fileName);
                        logger.Info("Image downloaded: {0}", fileName);
                    }
                }

            }
            catch (Exception ex)
            {
                logger.Warn("Error processing CommandRequest_DownloadImage: {0} ({1}) - {2}", downloadURL, EntityID, ex.Message);
                return;
            }
        }
		public List<Contract_Trakt_ShoutUser> GetTraktShoutsForAnime(int animeID)
		{
			List<Contract_Trakt_ShoutUser> shouts = new List<Contract_Trakt_ShoutUser>();

			try
			{
				Trakt_FriendRepository repFriends = new Trakt_FriendRepository();

				List<TraktTV_ShoutGet> shoutsTemp = TraktTVHelper.GetShowShouts(animeID);
				if (shoutsTemp == null || shoutsTemp.Count == 0) return shouts;

				foreach (TraktTV_ShoutGet sht in shoutsTemp)
				{
					Contract_Trakt_ShoutUser shout = new Contract_Trakt_ShoutUser();

					Trakt_Friend traktFriend = repFriends.GetByUsername(sht.user.username);

					// user details
					shout.User = new Contract_Trakt_User();
					if (traktFriend == null)
						shout.User.Trakt_FriendID = 0;
					else
						shout.User.Trakt_FriendID = traktFriend.Trakt_FriendID;
					shout.User.Username = sht.user.username;
					shout.User.Full_name = sht.user.full_name;
					shout.User.Gender = sht.user.gender;
					shout.User.Age = sht.user.age;
					shout.User.Location = sht.user.location;
					shout.User.About = sht.user.about;
					shout.User.Joined = sht.user.joined;
					shout.User.Avatar = sht.user.avatar;
					shout.User.Url = sht.user.url;
					shout.User.JoinedDate = Utils.GetAniDBDateAsDate(sht.user.joined);

					// shout details
					shout.Shout = new Contract_Trakt_Shout();
					shout.Shout.ShoutType = (int)TraktActivityType.Show; // episode or show
					shout.Shout.Text = sht.shout;
					shout.Shout.Spoiler = sht.spoiler;
					shout.Shout.Inserted = Utils.GetAniDBDateAsDate(sht.inserted);

					shouts.Add(shout);
				}
			}
			catch (Exception ex)
			{
				logger.ErrorException(ex.ToString(), ex);
			}
			return shouts;
		}
Beispiel #9
0
		public Contract_Trakt_Friend ToContract()
		{
			CrossRef_AniDB_TraktRepository repXrefTrakt = new CrossRef_AniDB_TraktRepository();
			CrossRef_AniDB_TvDBRepository repXrefTvDB = new CrossRef_AniDB_TvDBRepository();
			AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository();
			AnimeSeriesRepository repSeries = new AnimeSeriesRepository();
			Trakt_FriendRepository repFriends = new Trakt_FriendRepository();
			Trakt_EpisodeRepository repEpisodes = new Trakt_EpisodeRepository();

			Trakt_Friend traktFriend = repFriends.GetByUsername(username);
			if (traktFriend == null) return null;

			Contract_Trakt_Friend contract = new Contract_Trakt_Friend();

			contract.Trakt_FriendID = traktFriend.Trakt_FriendID;
			contract.Username = username;
			contract.Full_name = full_name;
			contract.Gender = gender;
			contract.Age = age;
			contract.Location = location;
			contract.About = about;
			contract.Joined = joined;
			contract.Avatar = avatar;
			contract.Url = url;
			contract.JoinedDate = Utils.GetAniDBDateAsDate(joined);



			contract.WatchedEpisodes = new List<Contract_Trakt_WatchedEpisode>();

			// we only care about the watched episodes
			foreach (TraktTVWatched wtch in watched)
			{
				if (wtch.episode != null)
				{
					Contract_Trakt_WatchedEpisode watchedEp = new Contract_Trakt_WatchedEpisode();

					watchedEp.Watched = wtch.watched;
					watchedEp.WatchedDate = Utils.GetAniDBDateAsDate(wtch.watched);

					if (!contract.LastEpisodeWatched.HasValue)
						contract.LastEpisodeWatched = watchedEp.WatchedDate;

					if (watchedEp.WatchedDate.HasValue && watchedEp.WatchedDate > contract.LastEpisodeWatched)
						contract.LastEpisodeWatched = watchedEp.WatchedDate;

					watchedEp.AnimeSeriesID = null;

					watchedEp.Episode_Number = wtch.episode.number;
					watchedEp.Episode_Overview = wtch.episode.overview;
					watchedEp.Episode_Season = wtch.episode.season;
					watchedEp.Episode_Title = wtch.episode.title;
					watchedEp.Episode_Url = wtch.episode.url;

					

					if (wtch.episode.images != null)
						watchedEp.Episode_Screenshot = wtch.episode.images.screen;

					if (wtch.show != null)
					{
						watchedEp.TraktShow = wtch.show.ToContract();

						// find the anime and series based on the trakt id
						int? animeID = null;
						CrossRef_AniDB_Trakt xref = repXrefTrakt.GetByTraktID(wtch.show.TraktID, int.Parse(wtch.episode.season));
						if (xref != null)
							animeID = xref.AnimeID;
						else
						{
							// try the tvdb id instead
							//TODO
							/*
							CrossRef_AniDB_TvDB xrefTvDB = repXrefTvDB.GetByTvDBID(int.Parse(wtch.show.tvdb_id), int.Parse(wtch.episode.season));
							if (xrefTvDB != null)
								animeID = xrefTvDB.AnimeID;*/
						}

						if (animeID.HasValue)
						{

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

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

						}
					}

					

					contract.WatchedEpisodes.Add(watchedEp);
					break; // only show the latest show
				}

			}

			List<SortPropOrFieldAndDirection> sortCriteria = new List<SortPropOrFieldAndDirection>();
			sortCriteria.Add(new SortPropOrFieldAndDirection("WatchedDate", true, SortType.eDateTime));
			contract.WatchedEpisodes = Sorting.MultiSort<Contract_Trakt_WatchedEpisode>(contract.WatchedEpisodes, sortCriteria);

			return contract;
		}
Beispiel #10
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;
		}
Beispiel #11
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;
		}
Beispiel #12
0
		public static List<TraktTV_ShoutGet> GetShowShouts(ISession session, int animeID)
		{
			List<TraktTV_ShoutGet> shouts = null;
			try
			{
				if (string.IsNullOrEmpty(ServerSettings.Trakt_Username) || string.IsNullOrEmpty(ServerSettings.Trakt_Password))
					return null;

				CrossRef_AniDB_TraktRepository repXrefTrakt = new CrossRef_AniDB_TraktRepository();
				CrossRef_AniDB_Trakt traktXRef = repXrefTrakt.GetByAnimeID(session, animeID);
				if (traktXRef == null) return null;

				string url = string.Format(Constants.TraktTvURLs.URLGetShowShouts, Constants.TraktTvURLs.APIKey, traktXRef.TraktID);
				logger.Trace("GetShowShouts: {0}", url);

				// Search for a series
				string json = Utils.DownloadWebPage(url);

				if (json.Trim().Length == 0) return new List<TraktTV_ShoutGet>();

				shouts = JSONHelper.Deserialize<List<TraktTV_ShoutGet>>(json);

				Trakt_FriendRepository repFriends = new Trakt_FriendRepository();
				foreach (TraktTV_ShoutGet shout in shouts)
				{
					Trakt_Friend traktFriend = repFriends.GetByUsername(session, shout.user.username);
					if (traktFriend == null)
					{
						traktFriend = new Trakt_Friend();
						traktFriend.LastAvatarUpdate = DateTime.Now;
					}

					traktFriend.Populate(shout.user);
					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(session);
						}
					}
				}

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

			return shouts;
		}