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 void Init(AnimeEpisode episode) { username = ServerSettings.Trakt_Username; password = Utils.CalculateSHA1(ServerSettings.Trakt_Password, Encoding.Default); }
public void Save(AnimeEpisode obj) { using (var session = JMMService.SessionFactory.OpenSession()) { Save(session, obj); } }
public AnimeEpisode GetLastEpisodeWatched(int userID) { AnimeEpisode watchedep = null; AnimeEpisode_User userRecordWatched = null; foreach (AnimeEpisode ep in GetAnimeEpisodes()) { AnimeEpisode_User userRecord = ep.GetUserRecord(userID); if (userRecord != null && ep.EpisodeTypeEnum == AniDBAPI.enEpisodeType.Episode) { if (watchedep == null) { watchedep = ep; userRecordWatched = userRecord; } if (userRecord.WatchedDate > userRecordWatched.WatchedDate) { watchedep = ep; userRecordWatched = userRecord; } } } return(watchedep); }
public void Save(ISession session, AnimeEpisode obj) { // populate the database using (var transaction = session.BeginTransaction()) { session.SaveOrUpdate(obj); transaction.Commit(); } }
public void PopulateManually(VideoLocal vid, AnimeEpisode ep) { Hash = vid.ED2KHash; FileName = Path.GetFileName(vid.FullServerPath); FileSize = vid.FileSize; CrossRefSource = (int)JMMServer.CrossRefSource.User; AnimeID = ep.GetAnimeSeries().AniDB_ID; EpisodeID = ep.AniDB_EpisodeID; Percentage = 100; EpisodeOrder = 1; }
public void CreateAnimeEpisode(ISession session, int animeSeriesID) { // check if there is an existing episode for this EpisodeID AnimeEpisode existingEp = RepoFactory.AnimeEpisode.GetByAniDBEpisodeID(EpisodeID); if (existingEp == null) { AnimeEpisode animeEp = new AnimeEpisode(); animeEp.Populate(this); animeEp.AnimeSeriesID = animeSeriesID; RepoFactory.AnimeEpisode.Save(animeEp); } }
public void CreateAnimeEpisode(ISession session, int animeSeriesID) { // check if there is an existing episode for this EpisodeID AnimeEpisodeRepository repEps = new AnimeEpisodeRepository(); AnimeEpisode existingEp = repEps.GetByAniDBEpisodeID(session, EpisodeID); if (existingEp == null) { AnimeEpisode animeEp = new AnimeEpisode(); animeEp.Populate(this); animeEp.AnimeSeriesID = animeSeriesID; repEps.Save(session, animeEp); } }
private static EpisodeSyncDetails ReconSyncTraktEpisode(AnimeSeries ser, AnimeEpisode ep, TraktSummaryContainer traktSummary, List<JMMUser> traktUsers, List<TraktV2ShowCollectedResult> collected, List<TraktV2ShowWatchedResult> watched, bool sendNow) { try { // get the Trakt Show ID for this episode string traktShowID = string.Empty; int season = -1; int epNumber = -1; GetTraktEpisodeIdV2(ep, ref traktShowID, ref season, ref epNumber); if (string.IsNullOrEmpty(traktShowID) || season < 0 || epNumber < 0) return null; // get the current collected records for this series on Trakt TraktV2CollectedEpisode epTraktCol = null; TraktV2ShowCollectedResult col = collected.FirstOrDefault(x => x.show.ids.slug == traktShowID); if (col != null) { TraktV2CollectedSeason sea = col.seasons.FirstOrDefault(x => x.number == season); if (sea != null) { epTraktCol = sea.episodes.FirstOrDefault(x => x.number == epNumber); } } bool onlineCollection = epTraktCol != null; // get the current watched records for this series on Trakt TraktV2WatchedEpisode epTraktWatched = null; TraktV2ShowWatchedResult wtc = watched.FirstOrDefault(x => x.show.ids.slug == traktShowID); if (wtc != null) { TraktV2WatchedSeason sea = wtc.seasons.FirstOrDefault(x => x.number == season); if (sea != null) { epTraktWatched = sea.episodes.FirstOrDefault(x => x.number == epNumber); } } bool onlineWatched = epTraktWatched != null; bool localCollection = false; bool localWatched = false; if (ep.GetVideoLocals().Count > 0) { // let's check if this episode has a user record against it // if it does, it means a user has watched it localCollection = true; AnimeEpisode_User userRecord = null; foreach (JMMUser juser in traktUsers) { userRecord = ep.GetUserRecord(juser.JMMUserID); if (userRecord != null) break; } if (userRecord != null) localWatched = true; } string msg1 = string.Format("Sync Check Status: AniDB: {0} - {1} - {2} - Collection: {3} - Watched: {4}", ser.AniDB_ID, ep.EpisodeTypeEnum, ep.AniDB_EpisodeID, localCollection, localWatched); string msg2 = string.Format("Sync Check Status: Trakt: {0} - S:{1} - EP:{2} - Collection: {3} - Watched: {4}", traktShowID, season, epNumber, onlineCollection, onlineWatched); logger.Trace(msg1); logger.Trace(msg2); // sync the collection status if (localCollection) { // is in the local collection, but not Trakt, so let's ADD it if (!onlineCollection) { string msg = string.Format("SYNC LOCAL: Adding to Trakt Collection: Slug: {0} - S:{1} - EP:{2}", traktShowID, season, epNumber); logger.Trace(msg); DateTime epDate = GetEpisodeDateForSync(ep, TraktSyncType.CollectionAdd); if (sendNow) SyncEpisodeToTrakt(TraktSyncType.CollectionAdd, traktShowID, season, epNumber, epDate, false); else return new EpisodeSyncDetails(TraktSyncType.CollectionAdd, traktShowID, season, epNumber, epDate); } } else { // is in the trakt collection, but not local, so let's REMOVE it if (onlineCollection) { string msg = string.Format("SYNC LOCAL: Removing from Trakt Collection: Slug: {0} - S:{1} - EP:{2}", traktShowID, season, epNumber); logger.Trace(msg); DateTime epDate = GetEpisodeDateForSync(ep, TraktSyncType.CollectionRemove); if (sendNow) SyncEpisodeToTrakt(TraktSyncType.CollectionRemove, traktShowID, season, epNumber, epDate, false); else return new EpisodeSyncDetails(TraktSyncType.CollectionRemove, traktShowID, season, epNumber, epDate); } } // sync the watched status if (localWatched) { // is watched locally, but not Trakt, so let's ADD it if (!onlineWatched) { string msg = string.Format("SYNC LOCAL: Adding to Trakt History: Slug: {0} - S:{1} - EP:{2}", traktShowID, season, epNumber); logger.Trace(msg); DateTime epDate = GetEpisodeDateForSync(ep, TraktSyncType.HistoryAdd); if (sendNow) SyncEpisodeToTrakt(TraktSyncType.HistoryAdd, traktShowID, season, epNumber, epDate, false); else return new EpisodeSyncDetails(TraktSyncType.HistoryAdd, traktShowID, season, epNumber, epDate); } } else { // is watched on trakt, but not locally, so let's REMOVE it if (onlineWatched) { string msg = string.Format("SYNC LOCAL: Removing from Trakt History: Slug: {0} - S:{1} - EP:{2}", traktShowID, season, epNumber); logger.Trace(msg); DateTime epDate = GetEpisodeDateForSync(ep, TraktSyncType.HistoryRemove); if (sendNow) SyncEpisodeToTrakt(TraktSyncType.HistoryRemove, traktShowID, season, epNumber, epDate, false); else return new EpisodeSyncDetails(TraktSyncType.HistoryRemove, traktShowID, season, epNumber, epDate); } } return null; } catch (Exception ex) { logger.ErrorException("Error in TraktTVHelper.SyncTraktEpisode: " + ex.ToString(), ex); return null; } }
private static int? GetTraktEpisodeIdV2(AnimeEpisode ep, ref string traktID, ref int season, ref int epNumber) { AniDB_Episode aniep = ep.AniDB_Episode; if (aniep == null) return null; AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository(); AniDB_Anime anime = repAnime.GetByAnimeID(aniep.AnimeID); if (anime == null) return null; return GetTraktEpisodeIdV2(anime, aniep, ref traktID, ref season, ref epNumber); }
private static DateTime GetEpisodeDateForSync(AnimeEpisode ep, TraktSyncType syncType) { DateTime epDate = DateTime.Now; if (syncType == TraktSyncType.CollectionAdd || syncType == TraktSyncType.CollectionRemove) { epDate = DateTime.Now; // not relevant for a remove if (syncType == TraktSyncType.CollectionAdd) { // get the the first file that was added to this episode DateTime? thisDate = null; foreach (VideoLocal vid in ep.GetVideoLocals()) { if (!thisDate.HasValue) thisDate = vid.DateTimeCreated; if (vid.DateTimeCreated < thisDate) thisDate = vid.DateTimeCreated; } if (thisDate.HasValue) epDate = thisDate.Value; } } else { epDate = DateTime.Now; // not relevant for a remove if (syncType == TraktSyncType.HistoryAdd) { // get the latest user record and find the latest date this episode was watched DateTime? thisDate = null; JMMUserRepository repUsers = new JMMUserRepository(); List<JMMUser> traktUsers = repUsers.GetTraktUsers(); if (traktUsers.Count > 0) { AnimeEpisode_User userRecord = null; foreach (JMMUser juser in traktUsers) { userRecord = ep.GetUserRecord(juser.JMMUserID); if (userRecord != null) { if (!thisDate.HasValue && userRecord.WatchedDate.HasValue) thisDate = userRecord.WatchedDate; if (userRecord.WatchedDate.HasValue && thisDate.HasValue && userRecord.WatchedDate > thisDate) thisDate = userRecord.WatchedDate; } } if (thisDate.HasValue) epDate = thisDate.Value; } } } return epDate; }
public static void SyncEpisodeToTrakt(AnimeEpisode ep, TraktSyncType syncType, bool secondaryAction = true) { try { if (!ServerSettings.Trakt_IsEnabled || string.IsNullOrEmpty(ServerSettings.Trakt_AuthToken)) return; string traktShowID = string.Empty; int season = -1; int epNumber = -1; GetTraktEpisodeIdV2(ep, ref traktShowID, ref season, ref epNumber); if (string.IsNullOrEmpty(traktShowID) || season < 0 || epNumber < 0) return; DateTime epDate = GetEpisodeDateForSync(ep, syncType); //SyncEpisodeToTrakt(syncType, traktEpisodeId.Value, secondaryAction); SyncEpisodeToTrakt(syncType, traktShowID, season, epNumber, epDate, secondaryAction); } catch (Exception ex) { logger.ErrorException("Error in TraktTVHelper.MarkEpisodeWatched: " + ex.ToString(), ex); } }
public void ToggleWatchedStatus(bool watched, bool updateOnline, DateTime?watchedDate, bool updateStats, bool updateStatsCache, int userID, bool syncTrakt, bool updateWatchedDate) { JMMUser user = RepoFactory.JMMUser.GetByID(userID); if (user == null) { return; } List <JMMUser> aniDBUsers = RepoFactory.JMMUser.GetAniDBUsers(); // update the video file to watched int mywatched = watched ? 1 : 0; if (user.IsAniDBUser == 0) { SaveWatchedStatus(watched, userID, watchedDate, updateWatchedDate); } else { // if the user is AniDB user we also want to update any other AniDB // users to keep them in sync foreach (JMMUser juser in aniDBUsers) { if (juser.IsAniDBUser == 1) { SaveWatchedStatus(watched, juser.JMMUserID, watchedDate, updateWatchedDate); } } } // now lets find all the associated AniDB_File record if there is one if (user.IsAniDBUser == 1) { AniDB_File aniFile = RepoFactory.AniDB_File.GetByHash(this.Hash); if (aniFile != null) { aniFile.IsWatched = mywatched; if (watched) { if (watchedDate.HasValue) { aniFile.WatchedDate = watchedDate; } else { aniFile.WatchedDate = DateTime.Now; } } else { aniFile.WatchedDate = null; } RepoFactory.AniDB_File.Save(aniFile, false); } if (updateOnline) { if ((watched && ServerSettings.AniDB_MyList_SetWatched) || (!watched && ServerSettings.AniDB_MyList_SetUnwatched)) { CommandRequest_UpdateMyListFileStatus cmd = new CommandRequest_UpdateMyListFileStatus( this.Hash, watched, false, watchedDate.HasValue ? Utils.GetAniDBDateAsSeconds(watchedDate) : 0); cmd.Save(); } } } // now find all the episode records associated with this video file // but we also need to check if theer are any other files attached to this episode with a watched // status, AnimeSeries ser = null; // get all files associated with this episode List <CrossRef_File_Episode> xrefs = RepoFactory.CrossRef_File_Episode.GetByHash(this.Hash); Dictionary <int, AnimeSeries> toUpdateSeries = new Dictionary <int, AnimeSeries>(); if (watched) { // find the total watched percentage // eg one file can have a % = 100 // or if 2 files make up one episodes they will each have a % = 50 foreach (CrossRef_File_Episode xref in xrefs) { // get the episodes for this file, may be more than one (One Piece x Toriko) AnimeEpisode ep = RepoFactory.AnimeEpisode.GetByAniDBEpisodeID(xref.EpisodeID); // get all the files for this episode int epPercentWatched = 0; foreach (CrossRef_File_Episode filexref in ep.FileCrossRefs) { VideoLocal_User vidUser = filexref.GetVideoLocalUserRecord(userID); if (vidUser != null && vidUser.WatchedDate.HasValue) { // if not null means it is watched epPercentWatched += filexref.Percentage; } if (epPercentWatched > 95) { break; } } if (epPercentWatched > 95) { ser = ep.GetAnimeSeries(); if (!toUpdateSeries.ContainsKey(ser.AnimeSeriesID)) { toUpdateSeries.Add(ser.AnimeSeriesID, ser); } if (user.IsAniDBUser == 0) { ep.SaveWatchedStatus(true, userID, watchedDate, updateWatchedDate); } else { // if the user is AniDB user we also want to update any other AniDB // users to keep them in sync foreach (JMMUser juser in aniDBUsers) { if (juser.IsAniDBUser == 1) { ep.SaveWatchedStatus(true, juser.JMMUserID, watchedDate, updateWatchedDate); } } } if (syncTrakt && ServerSettings.Trakt_IsEnabled && !string.IsNullOrEmpty(ServerSettings.Trakt_AuthToken)) { CommandRequest_TraktHistoryEpisode cmdSyncTrakt = new CommandRequest_TraktHistoryEpisode(ep.AnimeEpisodeID, TraktSyncAction.Add); cmdSyncTrakt.Save(); } if (!string.IsNullOrEmpty(ServerSettings.MAL_Username) && !string.IsNullOrEmpty(ServerSettings.MAL_Password)) { CommandRequest_MALUpdatedWatchedStatus cmdMAL = new CommandRequest_MALUpdatedWatchedStatus(ser.AniDB_ID); cmdMAL.Save(); } } } } else { // if setting a file to unwatched only set the episode unwatched, if ALL the files are unwatched foreach (CrossRef_File_Episode xrefEp in xrefs) { // get the episodes for this file, may be more than one (One Piece x Toriko) AnimeEpisode ep = RepoFactory.AnimeEpisode.GetByAniDBEpisodeID(xrefEp.EpisodeID); ser = ep.GetAnimeSeries(); if (!toUpdateSeries.ContainsKey(ser.AnimeSeriesID)) { toUpdateSeries.Add(ser.AnimeSeriesID, ser); } // get all the files for this episode int epPercentWatched = 0; foreach (CrossRef_File_Episode filexref in ep.FileCrossRefs) { VideoLocal_User vidUser = filexref.GetVideoLocalUserRecord(userID); if (vidUser != null && vidUser.WatchedDate.HasValue) { epPercentWatched += filexref.Percentage; } if (epPercentWatched > 95) { break; } } if (epPercentWatched < 95) { if (user.IsAniDBUser == 0) { ep.SaveWatchedStatus(false, userID, watchedDate, true); } else { // if the user is AniDB user we also want to update any other AniDB // users to keep them in sync foreach (JMMUser juser in aniDBUsers) { if (juser.IsAniDBUser == 1) { ep.SaveWatchedStatus(false, juser.JMMUserID, watchedDate, true); } } } if (syncTrakt && ServerSettings.Trakt_IsEnabled && !string.IsNullOrEmpty(ServerSettings.Trakt_AuthToken)) { CommandRequest_TraktHistoryEpisode cmdSyncTrakt = new CommandRequest_TraktHistoryEpisode(ep.AnimeEpisodeID, TraktSyncAction.Remove); cmdSyncTrakt.Save(); } } } if (!string.IsNullOrEmpty(ServerSettings.MAL_Username) && !string.IsNullOrEmpty(ServerSettings.MAL_Password)) { CommandRequest_MALUpdatedWatchedStatus cmdMAL = new CommandRequest_MALUpdatedWatchedStatus(ser.AniDB_ID); cmdMAL.Save(); } } // update stats for groups and series if (toUpdateSeries.Count > 0 && updateStats) { foreach (AnimeSeries s in toUpdateSeries.Values) { // update all the groups above this series in the heirarchy s.UpdateStats(true, true, true); } //ser.TopLevelAnimeGroup.UpdateStatsFromTopLevel(true, true, true); } //if (ser != null && updateStatsCache) //StatsCache.Instance.UpdateUsingSeries(ser.AnimeSeriesID); }
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); } }
public void Add(AnimeEpisode ep, bool available) { if ((AnimeType == enAnimeType.OVA) || (AnimeType == enAnimeType.Movie)) { AniDB_Episode aniEp = ep.AniDB_Episode; string ename = aniEp.EnglishName.ToLower(); System.Text.RegularExpressions.Match m = partmatch.Match(ename); StatEpisodes.StatEpisode s = new StatEpisodes.StatEpisode(); s.Available = available; if (m.Success) { int part_number = 0; int part_count = 0; int.TryParse(m.Groups[1].Value, out part_number); int.TryParse(m.Groups[2].Value, out part_count); string rname = partmatch.Replace(ename, string.Empty); rname = remsymbols.Replace(rname, string.Empty); rname = remmultispace.Replace(rname, " "); s.EpisodeType = StatEpisodes.StatEpisode.EpType.Part; s.PartCount = part_count; s.Match = rname.Trim(); if ((s.Match == "complete movie") || (s.Match == "movie") || (s.Match == "ova")) { s.Match = string.Empty; } } else { if ((ename == "complete movie") || (ename == "movie") || (ename == "ova")) { s.Match = string.Empty; } else { string rname = partmatch.Replace(aniEp.EnglishName.ToLower(), string.Empty); rname = remsymbols.Replace(rname, string.Empty); rname = remmultispace.Replace(rname, " "); s.Match = rname.Trim(); } s.EpisodeType = StatEpisodes.StatEpisode.EpType.Complete; s.PartCount = 0; } StatEpisodes fnd = null; foreach (StatEpisodes k in this) { foreach (StatEpisodes.StatEpisode ss in k) { if (ss.Match == s.Match) { fnd = k; break; } } if (fnd != null) { break; } } if (fnd == null) { StatEpisodes eps = new StatEpisodes(); eps.Add(s); Add(eps); } else { fnd.Add(s); } } else { StatEpisodes eps = new StatEpisodes(); StatEpisodes.StatEpisode es = new StatEpisodes.StatEpisode(); es.Match = string.Empty; es.EpisodeType = StatEpisodes.StatEpisode.EpType.Complete; es.PartCount = 0; es.Available = available; eps.Add(es); this.Add(eps); } }
private static void GetTraktEpisodeNumber(AnimeEpisode aniepisode, AnimeSeries ser, Trakt_Show show, int season, ref int traktEpNum, ref int traktSeason) { 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); GetTraktEpisodeNumber(aniepisode, ser, show, season, ref traktEpNum, ref traktSeason, dictTraktEpisodes, dictTraktSpecials, dictTraktSeasons); }
private static void GetTraktEpisodeNumber(AnimeEpisode aniepisode, AnimeSeries ser, Trakt_Show show, int season, ref int traktEpNum, ref int traktSeason, Dictionary<int, Trakt_Episode> dictTraktEpisodes, Dictionary<int, Trakt_Episode> dictTraktSpecials, Dictionary<int, int> dictTraktSeasons) { try { traktEpNum = -1; traktSeason = -1; int epNum = aniepisode.AniDB_Episode.EpisodeNumber; if (season > 0) { //episode if (aniepisode.EpisodeTypeEnum == AniDBAPI.enEpisodeType.Episode) { if (dictTraktEpisodes != null && dictTraktSeasons != null) { if (dictTraktSeasons.ContainsKey(season)) { int absEpisodeNumber = dictTraktSeasons[season] + epNum - 1; if (dictTraktEpisodes.ContainsKey(absEpisodeNumber)) { Trakt_Episode tvep = dictTraktEpisodes[absEpisodeNumber]; traktEpNum = tvep.EpisodeNumber; traktSeason = tvep.Season; } } } } if (aniepisode.EpisodeTypeEnum == AniDBAPI.enEpisodeType.Special) { traktSeason = 0; traktEpNum = epNum; } } else { traktSeason = 0; traktEpNum = epNum; } return; } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); return; } }
public void CreateAnimeEpisode(ISession session, int animeSeriesID) { // check if there is an existing episode for this EpisodeID AnimeEpisodeRepository repEps = new AnimeEpisodeRepository(); List<AnimeEpisode> existingEps = repEps.GetByAniEpisodeIDAndSeriesID(session, EpisodeID, animeSeriesID); if (existingEps.Count == 0) { AnimeEpisode animeEp = new AnimeEpisode(); animeEp.Populate(this); animeEp.AnimeSeriesID = animeSeriesID; repEps.Save(session, animeEp); } }
private void GetTraktEpisodeNumber(AnimeEpisode aniepisode, Trakt_Show show, int season, ref int traktEpNum, ref int traktSeason) { try { traktEpNum = -1; traktSeason = -1; AnimeSeries ser = aniepisode.GetAnimeSeries(); if (ser == null) return; //Dictionary<int, int> dictTraktSeasons = GetDictTraktSeasons(show); //Dictionary<int, Trakt_Episode> dictTraktEpisodes = GetDictTraktEpisodes(show); Dictionary<int, int> dictTraktSeasons = null; Dictionary<int, Trakt_Episode> dictTraktEpisodes = null; GetDictTraktEpisodesAndSeasons(show, ref dictTraktEpisodes, ref dictTraktSeasons); int epNum = aniepisode.AniDB_Episode.EpisodeNumber; //episode if (aniepisode.EpisodeTypeEnum == AniDBAPI.enEpisodeType.Episode) { if (dictTraktEpisodes != null && dictTraktSeasons != null) { if (dictTraktSeasons.ContainsKey(season)) { int absEpisodeNumber = dictTraktSeasons[season] + epNum - 1; if (dictTraktEpisodes.ContainsKey(absEpisodeNumber)) { Trakt_Episode tvep = dictTraktEpisodes[absEpisodeNumber]; traktEpNum = tvep.EpisodeNumber; traktSeason = tvep.Season; } } } } if (aniepisode.EpisodeTypeEnum == AniDBAPI.enEpisodeType.Special) { traktSeason = 0; traktEpNum = epNum; } return; } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); return; } }
public void ToggleWatchedStatus(bool watched, bool updateOnline, DateTime?watchedDate, bool updateStats, bool updateStatsCache, int userID, bool scrobbleTrakt, bool updateWatchedDate) { VideoLocalRepository repVids = new VideoLocalRepository(); AnimeEpisodeRepository repEpisodes = new AnimeEpisodeRepository(); AniDB_FileRepository repAniFile = new AniDB_FileRepository(); CrossRef_File_EpisodeRepository repCross = new CrossRef_File_EpisodeRepository(); VideoLocal_UserRepository repVidUsers = new VideoLocal_UserRepository(); JMMUserRepository repUsers = new JMMUserRepository(); AnimeEpisode_UserRepository repEpisodeUsers = new AnimeEpisode_UserRepository(); JMMUser user = repUsers.GetByID(userID); if (user == null) { return; } List <JMMUser> aniDBUsers = repUsers.GetAniDBUsers(); // update the video file to watched int mywatched = watched ? 1 : 0; if (user.IsAniDBUser == 0) { SaveWatchedStatus(watched, userID, watchedDate, updateWatchedDate); } else { // if the user is AniDB user we also want to update any other AniDB // users to keep them in sync foreach (JMMUser juser in aniDBUsers) { if (juser.IsAniDBUser == 1) { SaveWatchedStatus(watched, juser.JMMUserID, watchedDate, updateWatchedDate); } } } // now lets find all the associated AniDB_File record if there is one if (user.IsAniDBUser == 1) { AniDB_File aniFile = repAniFile.GetByHash(this.Hash); if (aniFile != null) { aniFile.IsWatched = mywatched; if (watched) { if (watchedDate.HasValue) { aniFile.WatchedDate = watchedDate; } else { aniFile.WatchedDate = DateTime.Now; } } else { aniFile.WatchedDate = null; } repAniFile.Save(aniFile, false); } if (updateOnline) { if ((watched && ServerSettings.AniDB_MyList_SetWatched) || (!watched && ServerSettings.AniDB_MyList_SetUnwatched)) { CommandRequest_UpdateMyListFileStatus cmd = new CommandRequest_UpdateMyListFileStatus(this.Hash, watched, false, watchedDate.HasValue ? Utils.GetAniDBDateAsSeconds(watchedDate) : 0); cmd.Save(); } } } // now find all the episode records associated with this video file // but we also need to check if theer are any other files attached to this episode with a watched // status, AnimeSeries ser = null; // get all files associated with this episode List <CrossRef_File_Episode> xrefs = repCross.GetByHash(this.Hash); if (watched) { // find the total watched percentage // eg one file can have a % = 100 // or if 2 files make up one episodes they will each have a % = 50 foreach (CrossRef_File_Episode xref in xrefs) { // get the episode for this file AnimeEpisode ep = repEpisodes.GetByAniDBEpisodeID(xref.EpisodeID); if (ep == null) { continue; } // get all the files for this episode int epPercentWatched = 0; foreach (CrossRef_File_Episode filexref in ep.FileCrossRefs) { VideoLocal_User vidUser = filexref.GetVideoLocalUserRecord(userID); if (vidUser != null) { // if not null means it is watched epPercentWatched += filexref.Percentage; } if (epPercentWatched > 95) { break; } } if (epPercentWatched > 95) { ser = ep.GetAnimeSeries(); if (user.IsAniDBUser == 0) { ep.SaveWatchedStatus(true, userID, watchedDate, updateWatchedDate); } else { // if the user is AniDB user we also want to update any other AniDB // users to keep them in sync foreach (JMMUser juser in aniDBUsers) { if (juser.IsAniDBUser == 1) { ep.SaveWatchedStatus(true, juser.JMMUserID, watchedDate, updateWatchedDate); } } } if (scrobbleTrakt && !string.IsNullOrEmpty(ServerSettings.Trakt_Username) && !string.IsNullOrEmpty(ServerSettings.Trakt_Password)) { CommandRequest_TraktShowScrobble cmdScrobble = new CommandRequest_TraktShowScrobble(ep.AnimeEpisodeID); cmdScrobble.Save(); } if (!string.IsNullOrEmpty(ServerSettings.MAL_Username) && !string.IsNullOrEmpty(ServerSettings.MAL_Password)) { CommandRequest_MALUpdatedWatchedStatus cmdMAL = new CommandRequest_MALUpdatedWatchedStatus(ser.AniDB_ID); cmdMAL.Save(); } } } } else { // if setting a file to unwatched only set the episode unwatched, if ALL the files are unwatched foreach (CrossRef_File_Episode xrefEp in xrefs) { AnimeEpisode ep = repEpisodes.GetByAniDBEpisodeID(xrefEp.EpisodeID); if (ep == null) { continue; } ser = ep.GetAnimeSeries(); // get all the files for this episode int epPercentWatched = 0; foreach (CrossRef_File_Episode filexref in ep.FileCrossRefs) { VideoLocal_User vidUser = filexref.GetVideoLocalUserRecord(userID); if (vidUser != null) { epPercentWatched += filexref.Percentage; } if (epPercentWatched > 95) { break; } } if (epPercentWatched < 95) { if (user.IsAniDBUser == 0) { ep.SaveWatchedStatus(false, userID, watchedDate, true); } else { // if the user is AniDB user we also want to update any other AniDB // users to keep them in sync foreach (JMMUser juser in aniDBUsers) { if (juser.IsAniDBUser == 1) { ep.SaveWatchedStatus(false, juser.JMMUserID, watchedDate, true); } } } CommandRequest_TraktShowEpisodeUnseen cmdUnseen = new CommandRequest_TraktShowEpisodeUnseen(ep.AnimeEpisodeID); cmdUnseen.Save(); } } if (!string.IsNullOrEmpty(ServerSettings.MAL_Username) && !string.IsNullOrEmpty(ServerSettings.MAL_Password)) { CommandRequest_MALUpdatedWatchedStatus cmdMAL = new CommandRequest_MALUpdatedWatchedStatus(ser.AniDB_ID); cmdMAL.Save(); } } // update stats for groups and series if (ser != null && updateStats) { // update all the groups above this series in the heirarchy ser.UpdateStats(true, true, true); //ser.TopLevelAnimeGroup.UpdateStatsFromTopLevel(true, true, true); } if (ser != null && updateStatsCache) { StatsCache.Instance.UpdateUsingSeries(ser.AnimeSeriesID); } }
/*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); } }