public static void SaveExtendedShowInfoV2(TraktV2ShowExtended tvshow, List<TraktV2Season> seasons) { 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.GetByTraktSlug(tvshow.ids.slug); if (show == null) show = new Trakt_Show(); show.Populate(tvshow); repShows.Save(show); if (tvshow.images != null && tvshow.images.fanart != null) { if (!string.IsNullOrEmpty(tvshow.images.fanart.full)) { Trakt_ImageFanart fanart = repFanart.GetByShowIDAndSeason(show.Trakt_ShowID, 1); if (fanart == null) { fanart = new Trakt_ImageFanart(); fanart.Enabled = 0; } fanart.ImageURL = tvshow.images.fanart.full; 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(); // delete episodes if they no longer exist on Trakt if (seasons.Count > 0) { foreach (Trakt_Episode epTemp in repEpisodes.GetByShowID(show.Trakt_ShowID)) { TraktV2Episode ep = null; TraktV2Season sea = seasons.FirstOrDefault(x => x.number == epTemp.Season); if (sea != null) ep = sea.episodes.FirstOrDefault(x => x.number == epTemp.EpisodeNumber); // if the episode is null, it means it doesn't exist on Trakt, so we should delete it if (ep == null) repEpisodes.Delete(epTemp.Trakt_EpisodeID); } } foreach (TraktV2Season sea in seasons) { Trakt_Season season = repSeasons.GetByShowIDAndSeason(show.Trakt_ShowID, sea.number); if (season == null) season = new Trakt_Season(); season.Season = sea.number; season.URL = string.Format(TraktURIs.WebsiteSeason, show.TraktID, sea.number); season.Trakt_ShowID = show.Trakt_ShowID; repSeasons.Save(season); if (sea.images != null && sea.images.poster != null) { if (!string.IsNullOrEmpty(sea.images.poster.full)) { 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.full; poster.Season = season.Season; poster.Trakt_ShowID = show.Trakt_ShowID; repPosters.Save(poster); } } if (sea.episodes != null) { foreach (TraktV2Episode ep in sea.episodes) { Trakt_Episode episode = repEpisodes.GetByShowIDSeasonAndEpisode(show.Trakt_ShowID, ep.season, ep.number); if (episode == null) episode = new Trakt_Episode(); Console.Write(ep.ids.trakt); if (ep.images.screenshot != null) episode.EpisodeImage = ep.images.screenshot.full; else episode.EpisodeImage = string.Empty; episode.TraktID = ep.ids.TraktID; episode.EpisodeNumber = ep.number; episode.Overview = string.Empty; // this is now part of a separate API call for V2, we get this info from TvDB anyway episode.Season = ep.season; episode.Title = ep.title; episode.URL = string.Format(TraktURIs.WebsiteEpisode, show.TraktID, ep.season, ep.number); episode.Trakt_ShowID = show.Trakt_ShowID; repEpisodes.Save(episode); } } } } catch (Exception ex) { logger.ErrorException("Error in TraktTVHelper.SaveExtendedShowInfo: " + ex.ToString(), ex); } }
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 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; }
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); } }
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; }