public static string GetNewFileName(VideoLocal vid, string script) { string[] lines = script.Split(Environment.NewLine.ToCharArray()); string newFileName = string.Empty; AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository(); List<AniDB_Episode> episodes = new List<AniDB_Episode>(); AniDB_Anime anime = null; VideoInfo vi = vid.VideoInfo; if (vi == null) return string.Empty; // get all the data so we don't need to get multiple times AniDB_File aniFile = vid.GetAniDBFile(); if (aniFile == null) { List<AnimeEpisode> animeEps = vid.GetAnimeEpisodes(); if (animeEps.Count == 0) return string.Empty; episodes.Add(animeEps[0].AniDB_Episode); anime = repAnime.GetByAnimeID(episodes[0].AnimeID); if (anime == null) return string.Empty; } else { episodes = aniFile.Episodes; if (episodes.Count == 0) return string.Empty; anime = repAnime.GetByAnimeID(episodes[0].AnimeID); if (anime == null) return string.Empty; } foreach (string line in lines) { string thisLine = line.Trim(); if (thisLine.Length == 0) continue; // remove all comments from this line int comPos = thisLine.IndexOf("//"); if (comPos >= 0) { thisLine = thisLine.Substring(0, comPos); } // check if this line has no tests (applied to all files) if (thisLine.StartsWith(Constants.FileRenameReserved.Do, StringComparison.InvariantCultureIgnoreCase)) { string action = GetAction(thisLine); PerformActionOnFileName(ref newFileName, action, vid, aniFile, episodes, anime, vi); } else { try { if (EvaluateTest(thisLine, vid, aniFile, episodes, anime, vi)) { Debug.WriteLine(string.Format("Line passed: {0}", thisLine)); // if the line has passed the tests, then perform the action string action = GetAction(thisLine); // if the action is fail, we don't want to rename if (action.ToUpper().Trim().Equals(Constants.FileRenameReserved.Fail, StringComparison.InvariantCultureIgnoreCase)) return string.Empty; PerformActionOnFileName(ref newFileName, action, vid, aniFile, episodes, anime, vi); } else { Debug.WriteLine(string.Format("Line failed: {0}", thisLine)); } } catch (Exception ex) { throw; } } } if (string.IsNullOrEmpty(newFileName)) return string.Empty; // finally add back the extension return string.Format("{0}{1}", newFileName, Path.GetExtension(vid.FullServerPath)); }
public override void ProcessCommand() { logger.Info("Processing CommandRequest_AddFileToMyList: {0}", Hash); try { vid = RepoFactory.VideoLocal.GetByHash(this.Hash); List<AnimeEpisode> animeEpisodes = new List<AnimeEpisode>(); if (vid != null) animeEpisodes = vid.GetAnimeEpisodes(); if (vid != null) { // when adding a file via the API, newWatchedStatus will return with current watched status on AniDB // if the file is already on the user's list bool isManualLink = false; List<CrossRef_File_Episode> xrefs = vid.EpisodeCrossRefs; if (xrefs.Count > 0) isManualLink = xrefs[0].CrossRefSource != (int) CrossRefSource.AniDB; // mark the video file as watched DateTime? watchedDate = null; bool newWatchedStatus = false; if (isManualLink) newWatchedStatus = JMMService.AnidbProcessor.AddFileToMyList(xrefs[0].AnimeID, xrefs[0].Episode.EpisodeNumber, ref watchedDate); else newWatchedStatus = JMMService.AnidbProcessor.AddFileToMyList(vid, ref watchedDate); // do for all AniDB users List<JMMUser> aniDBUsers = RepoFactory.JMMUser.GetAniDBUsers(); if (aniDBUsers.Count > 0) { JMMUser juser = aniDBUsers[0]; vid.ToggleWatchedStatus(newWatchedStatus, false, watchedDate, false, false, juser.JMMUserID, false, true); logger.Info("Adding file to list: {0} - {1}", vid.ToString(), watchedDate); // if the the episode is watched we may want to set the file to watched as well if (ServerSettings.Import_UseExistingFileWatchedStatus && !newWatchedStatus) { if (animeEpisodes.Count > 0) { AnimeEpisode ep = animeEpisodes[0]; AnimeEpisode_User epUser = null; foreach (JMMUser tempuser in aniDBUsers) { // only find the first user who watched this if (epUser == null) epUser = ep.GetUserRecord(tempuser.JMMUserID); } if (epUser != null) { logger.Info( "Setting file as watched, because episode was already watched: {0} - user: {1}", vid.ToString(), juser.Username); vid.ToggleWatchedStatus(true, true, epUser.WatchedDate, false, false, epUser.JMMUserID, false, true); } } } } AnimeSeries ser = animeEpisodes[0].GetAnimeSeries(); // all the eps should belong to the same anime ser.QueueUpdateStats(); //StatsCache.Instance.UpdateUsingSeries(ser.AnimeSeriesID); // lets also try adding to the users trakt collecion if (ser != null && ServerSettings.Trakt_IsEnabled && !string.IsNullOrEmpty(ServerSettings.Trakt_AuthToken)) { foreach (AnimeEpisode aep in animeEpisodes) { CommandRequest_TraktCollectionEpisode cmdSyncTrakt = new CommandRequest_TraktCollectionEpisode ( aep.AnimeEpisodeID, TraktSyncAction.Add); cmdSyncTrakt.Save(); } } // sync the series on MAL if (ser != null && !string.IsNullOrEmpty(ServerSettings.MAL_Username) && !string.IsNullOrEmpty(ServerSettings.MAL_Password)) { CommandRequest_MALUpdatedWatchedStatus cmdMAL = new CommandRequest_MALUpdatedWatchedStatus(ser.AniDB_ID); cmdMAL.Save(); } } } catch (Exception ex) { logger.Error("Error processing CommandRequest_AddFileToMyList: {0} - {1}", Hash, ex.ToString()); return; } }
public Serie GenerateFromVideoLocal(VideoLocal vl, int uid, int nocast, int notag, int level, int all) { Serie sr = new Serie(); if (vl != null) { foreach (AnimeEpisode ep in vl.GetAnimeEpisodes()) { sr = GenerateFromAnimeSeries(ep.GetAnimeSeries(), uid, nocast, notag, level, all); } } return sr; }
public override void ProcessCommand() { logger.Info("Processing CommandRequest_AddFileToMyList: {0}", Hash); try { VideoLocalRepository repVids = new VideoLocalRepository(); AnimeEpisodeRepository repEpisodes = new AnimeEpisodeRepository(); vid = repVids.GetByHash(this.Hash); if (vid != null) { // when adding a file via the API, newWatchedStatus will return with current watched status on AniDB // if the file is already on the user's list bool isManualLink = false; List<CrossRef_File_Episode> xrefs = vid.EpisodeCrossRefs; if (xrefs.Count > 0) isManualLink = xrefs[0].CrossRefSource != (int)CrossRefSource.AniDB; // mark the video file as watched DateTime? watchedDate = null; bool newWatchedStatus = false; if (isManualLink) newWatchedStatus = JMMService.AnidbProcessor.AddFileToMyList(xrefs[0].AnimeID, xrefs[0].Episode.EpisodeNumber, ref watchedDate); else newWatchedStatus = JMMService.AnidbProcessor.AddFileToMyList(vid, ref watchedDate); // do for all AniDB users JMMUserRepository repUsers = new JMMUserRepository(); List<JMMUser> aniDBUsers = repUsers.GetAniDBUsers(); List<AnimeEpisode> animeEpisodes = vid.GetAnimeEpisodes(); if (aniDBUsers.Count > 0) { JMMUser juser = aniDBUsers[0]; vid.ToggleWatchedStatus(newWatchedStatus, false, watchedDate, false, false, juser.JMMUserID, false, true); logger.Info("Adding file to list: {0} - {1}", vid.ToString(), watchedDate); // if the the episode is watched we may want to set the file to watched as well if (ServerSettings.Import_UseExistingFileWatchedStatus && !newWatchedStatus) { if (animeEpisodes.Count > 0) { AnimeEpisode ep = animeEpisodes[0]; AnimeEpisode_User epUser = null; foreach (JMMUser tempuser in aniDBUsers) { // only find the first user who watched this if (epUser == null) epUser = ep.GetUserRecord(tempuser.JMMUserID); } if (epUser != null) { logger.Info("Setting file as watched, because episode was already watched: {0} - user: {1}", vid.ToString(), juser.Username); vid.ToggleWatchedStatus(true, true, epUser.WatchedDate, false, false, epUser.JMMUserID, false, true); } } } } AnimeSeries ser = animeEpisodes[0].GetAnimeSeries(); // all the eps should belong to the same anime ser.UpdateStats(true, true, true); StatsCache.Instance.UpdateUsingSeries(ser.AnimeSeriesID); } } catch (Exception ex) { logger.Error("Error processing CommandRequest_AddFileToMyList: {0} - {1}", Hash, ex.ToString()); return; } }