Esempio n. 1
0
        public Episode(HttpContext ctx, SVR_AnimeEpisode ep)
        {
            var tvdb = ep.TvDBEpisodes;

            IDs = new EpisodeIDs
            {
                ID    = ep.AnimeEpisodeID,
                AniDB = ep.AniDB_EpisodeID,
                TvDB  = tvdb.Select(a => a.Id).ToList()
            };

            var anidb = ep.AniDB_Episode;

            if (anidb != null)
            {
                Duration = new TimeSpan(0, 0, anidb.LengthSeconds);
            }

            var uid = ctx.GetUser()?.JMMUserID ?? 0;

            Watched = ep.GetVideoLocals().Select(v => v.GetUserRecord(uid)?.WatchedDate).Where(v => v.HasValue).OrderByDescending(v => v).FirstOrDefault();
            Name    = GetEpisodeTitle(ep.AniDB_EpisodeID);

            Size = ep.GetVideoLocals().Count;
        }
Esempio n. 2
0
        public Episode(HttpContext ctx, SVR_AnimeEpisode ep)
        {
            var tvdb = ep.TvDBEpisodes;

            IDs = new EpisodeIDs
            {
                ID    = ep.AnimeEpisodeID,
                AniDB = ep.AniDB_EpisodeID,
                TvDB  = tvdb.Select(a => a.Id).ToList()
            };

            var anidb = ep.AniDB_Episode;

            if (anidb != null)
            {
                Duration = new TimeSpan(0, 0, anidb.LengthSeconds);
            }

            var uid = ctx.GetUser()?.JMMUserID ?? 0;

            Watched = ep.GetUserRecord(uid)?.WatchedDate;
            Name    = ep.Title;

            Size = ep.GetVideoLocals().Count;
        }
Esempio n. 3
0
        public static void CreateAnimeEpisode(this AniDB_Episode episode, ISession session, int animeSeriesID)
        {
            // check if there is an existing episode for this EpisodeID
            SVR_AnimeEpisode existingEp = RepoFactory.AnimeEpisode.GetByAniDBEpisodeID(episode.EpisodeID) ??
                                          new SVR_AnimeEpisode();

            existingEp.Populate(episode);
            existingEp.AnimeSeriesID = animeSeriesID;
            RepoFactory.AnimeEpisode.Save(existingEp);

            // We might have removed our AnimeEpisode_User records when wiping out AnimeEpisodes, recreate them if there's watched files
            var vlUsers = existingEp?.GetVideoLocals()
                          .SelectMany(a => RepoFactory.VideoLocalUser.GetByVideoLocalID(a.VideoLocalID)).ToList();

            // get the list of unique users
            var users = vlUsers.Select(a => a.JMMUserID).Distinct();

            if (vlUsers.Count > 0)
            {
                // per user. An episode is watched if any file is
                foreach (int uid in users)
                {
                    // get the last watched file
                    var vlUser = vlUsers.Where(a => a.JMMUserID == uid && a.WatchedDate != null)
                                 .MaxBy(a => a.WatchedDate).FirstOrDefault();
                    // create or update the record
                    var epUser = RepoFactory.AnimeEpisode_User.GetByUserIDAndEpisodeID(uid, existingEp.AnimeEpisodeID);
                    if (epUser == null)
                    {
                        epUser = new SVR_AnimeEpisode_User
                        {
                            JMMUserID      = uid,
                            WatchedDate    = vlUser?.WatchedDate,
                            PlayedCount    = vlUser != null ? 1 : 0,
                            WatchedCount   = vlUser != null ? 1 : 0,
                            AnimeSeriesID  = animeSeriesID,
                            AnimeEpisodeID = existingEp.AnimeEpisodeID
                        };
                        RepoFactory.AnimeEpisode_User.Save(epUser);
                    }
                }
            }
            else
            {
                // since these are created with VideoLocal_User,
                // these will probably never exist, but if they do, cover our bases
                foreach (var episodeUser in RepoFactory.AnimeEpisode_User.GetByEpisodeID(existingEp.AnimeEpisodeID))
                {
                    RepoFactory.AnimeEpisode_User.Save(episodeUser);
                }
            }
        }
Esempio n. 4
0
        internal static Episode GenerateFromAnimeEpisode(NancyContext ctx, SVR_AnimeEpisode aep, int uid, int level)
        {
            Episode ep = new Episode
            {
                id     = aep.AnimeEpisodeID,
                eptype = aep.EpisodeTypeEnum.ToString(),
                art    = new ArtCollection()
            };

            var userrating = aep.UserRating;

            if (userrating > 0)
            {
                ep.userrating = userrating.ToString(CultureInfo.InvariantCulture);
            }
            if (double.TryParse(ep.rating, out double rating))
            {
                // 0.1 should be the absolute lowest rating
                if (rating > 10)
                {
                    ep.rating = (rating / 100).ToString(CultureInfo.InvariantCulture);
                }
            }

            CL_AnimeEpisode_User cae = aep.GetUserContract(uid);

            if (cae != null)
            {
                ep.name    = cae.AniDB_EnglishName;
                ep.summary = cae.Description;

                ep.year = cae.AniDB_AirDate?.Year.ToString(CultureInfo.InvariantCulture);
                ep.air  = cae.AniDB_AirDate?.ToPlexDate();

                ep.votes  = cae.AniDB_Votes;
                ep.rating = cae.AniDB_Rating;

                ep.view     = cae.IsWatched() ? 1 : 0;
                ep.epnumber = cae.EpisodeNumber;
            }

            TvDB_Episode tvep = aep.TvDBEpisode;

            if (tvep != null)
            {
                if (!string.IsNullOrEmpty(tvep.EpisodeName))
                {
                    ep.name = tvep.EpisodeName;
                }
                if (Misc.IsImageValid(tvep.GetFullImagePath()))
                {
                    ep.art.thumb.Add(new Art
                    {
                        index = 0,
                        url   = APIHelper.ConstructImageLinkFromTypeAndId(ctx, (int)ImageEntityType.TvDB_Episode,
                                                                          tvep.TvDB_EpisodeID)
                    });
                }
                var fanarts = aep.GetAnimeSeries()?.GetAnime()?.Contract?.AniDBAnime?.Fanarts;
                if (fanarts != null && fanarts.Count > 0)
                {
                    var cont_image =
                        fanarts[new Random().Next(fanarts.Count)];
                    ep.art.fanart.Add(new Art()
                    {
                        url = APIHelper.ConstructImageLinkFromTypeAndId(ctx, cont_image.ImageType,
                                                                        cont_image.AniDB_Anime_DefaultImageID),
                        index = 0
                    });
                }
                else
                {
                    ep.art.fanart.Add(new Art
                    {
                        index = 0,
                        url   = APIHelper.ConstructImageLinkFromTypeAndId(ctx, (int)ImageEntityType.TvDB_Episode,
                                                                          tvep.TvDB_EpisodeID)
                    });
                }
                if (!string.IsNullOrEmpty(tvep.Overview))
                {
                    ep.summary = tvep.Overview;
                }

                int    zeroPadding   = tvep.EpisodeNumber.ToString().Length;
                string episodeNumber = tvep.EpisodeNumber.ToString().PadLeft(zeroPadding, '0');
                zeroPadding = tvep.SeasonNumber.ToString().Length;
                string seasonNumber = tvep.SeasonNumber.ToString().PadLeft(zeroPadding, '0');

                ep.season = $"{seasonNumber}x{episodeNumber}";
                var airdate = tvep.AirDate;
                if (airdate != null)
                {
                    ep.air  = airdate.Value.ToPlexDate();
                    ep.year = airdate.Value.Year.ToString(CultureInfo.InvariantCulture);
                }
            }
            if (string.IsNullOrEmpty(ep.summary))
            {
                ep.summary = string.Intern("Episode Overview not Available");
            }
            if (ep.art.thumb.Count == 0)
            {
                ep.art.thumb.Add(
                    new Art {
                    index = 0, url = APIHelper.ConstructSupportImageLink(ctx, "plex_404.png")
                });
                ep.art.fanart.Add(new Art
                {
                    index = 0,
                    url   = APIHelper.ConstructSupportImageLink(ctx, "plex_404.png")
                });
            }
            if (string.IsNullOrEmpty(ep.year))
            {
                ep.year = aep.GetAnimeSeries().AirDate.Year.ToString(CultureInfo.InvariantCulture);
            }

            if (level > 0)
            {
                List <SVR_VideoLocal> vls = aep.GetVideoLocals();
                if (vls.Count > 0)
                {
                    ep.files = new List <RawFile>();
                    foreach (SVR_VideoLocal vl in vls)
                    {
                        RawFile file = new RawFile(ctx, vl, (level - 1), uid);
                        ep.files.Add(file);
                    }
                }
            }

            return(ep);
        }
Esempio n. 5
0
        public static Video GenerateVideoFromAnimeEpisode(SVR_AnimeEpisode ep, int userID)
        {
            Video l = new Video();
            List <SVR_VideoLocal> vids = ep.GetVideoLocals();

            l.Type      = "episode";
            l.Summary   = "Episode Overview Not Available"; //TODO Intenationalization
            l.Id        = ep.AnimeEpisodeID;
            l.AnimeType = AnimeTypes.AnimeEpisode.ToString();
            if (vids.Count > 0)
            {
                //List<string> hashes = vids.Select(a => a.Hash).Distinct().ToList();
                l.Title                 = Path.GetFileNameWithoutExtension(vids[0].FileName);
                l.AddedAt               = vids[0].DateTimeCreated.ToUnixTime();
                l.UpdatedAt             = vids[0].DateTimeUpdated.ToUnixTime();
                l.OriginallyAvailableAt = vids[0].DateTimeCreated.ToPlexDate();
                l.Year   = vids[0].DateTimeCreated.Year;
                l.Medias = new List <Media>();
                foreach (SVR_VideoLocal v in vids)
                {
                    if (v?.Media == null)
                    {
                        continue;
                    }
                    var legacy = new Media(v.VideoLocalID, v.Media);
                    var place  = v.GetBestVideoLocalPlace();
                    legacy.Parts.ForEach(p =>
                    {
                        if (string.IsNullOrEmpty(p.LocalKey))
                        {
                            p.LocalKey = place.FullServerPath;
                        }
                        string name = UrlSafe.Replace(Path.GetFileName(place.FilePath), " ").CompactWhitespaces()
                                      .Trim();
                        name = UrlSafe2.Replace(name, string.Empty)
                               .Trim()
                               .CompactCharacters('.')
                               .Replace(" ", "_")
                               .CompactCharacters('_')
                               .Replace("_.", ".");
                        while (name.StartsWith("_"))
                        {
                            name = name.Substring(1);
                        }
                        while (name.StartsWith("."))
                        {
                            name = name.Substring(1);
                        }
                        p.Key = ((IProvider)null).ReplaceSchemeHost(
                            ((IProvider)null).ConstructVideoLocalStream(userID, v.VideoLocalID, name, false));
                        if (p.Streams == null)
                        {
                            return;
                        }
                        foreach (Stream s in p.Streams.Where(a => a.File != null && a.StreamType == 3).ToList())
                        {
                            s.Key =
                                ((IProvider)null).ReplaceSchemeHost(
                                    ((IProvider)null).ConstructFileStream(userID, s.File, false));
                        }
                    });
                    l.Medias.Add(legacy);
                }

                string title = ep.Title;
                if (!String.IsNullOrEmpty(title))
                {
                    l.Title = title;
                }

                string romaji = RepoFactory.AniDB_Episode_Title.GetByEpisodeIDAndLanguage(ep.AniDB_EpisodeID, "X-JAT")
                                .FirstOrDefault()?.Title;
                if (!String.IsNullOrEmpty(romaji))
                {
                    l.OriginalTitle = romaji;
                }

                AniDB_Episode aep = ep?.AniDB_Episode;
                if (aep != null)
                {
                    l.EpisodeNumber = aep.EpisodeNumber;
                    l.Index         = aep.EpisodeNumber;
                    l.EpisodeType   = aep.EpisodeType;
                    l.Rating        = (int)Single.Parse(aep.Rating, CultureInfo.InvariantCulture);
                    AniDB_Vote vote =
                        RepoFactory.AniDB_Vote.GetByEntityAndType(ep.AnimeEpisodeID, AniDBVoteType.Episode);
                    if (vote != null)
                    {
                        l.UserRating = (int)(vote.VoteValue / 100D);
                    }

                    if (aep.GetAirDateAsDate().HasValue)
                    {
                        l.Year = aep.GetAirDateAsDate()?.Year ?? 0;
                        l.OriginallyAvailableAt = aep.GetAirDateAsDate()?.ToPlexDate();
                    }

                    #region TvDB

                    TvDB_Episode tvep = ep.TvDBEpisode;
                    if (tvep != null)
                    {
                        l.Thumb   = tvep.GenPoster(null);
                        l.Summary = tvep.Overview;
                        l.Season  = $"{tvep.SeasonNumber}x{tvep.EpisodeNumber:0#}";
                    }
                    #endregion
                }
                if (l.Thumb == null || l.Summary == null)
                {
                    l.Thumb   = ((IProvider)null).ConstructSupportImageLink("plex_404.png");
                    l.Summary = "Episode Overview not Available";
                }
            }
            l.Id = ep.AnimeEpisodeID;
            return(l);
        }
Esempio n. 6
0
        public static Video GenerateVideoFromAnimeEpisode(SVR_AnimeEpisode ep)
        {
            Video l = new Video();
            List <SVR_VideoLocal> vids = ep.GetVideoLocals();

            l.Type      = "episode";
            l.Summary   = "Episode Overview Not Available"; //TODO Intenationalization
            l.Id        = ep.AnimeEpisodeID;
            l.AnimeType = AnimeTypes.AnimeEpisode.ToString();
            if (vids.Count > 0)
            {
                //List<string> hashes = vids.Select(a => a.Hash).Distinct().ToList();
                l.Title                 = Path.GetFileNameWithoutExtension(vids[0].FileName);
                l.AddedAt               = vids[0].DateTimeCreated.ToUnixTime();
                l.UpdatedAt             = vids[0].DateTimeUpdated.ToUnixTime();
                l.OriginallyAvailableAt = vids[0].DateTimeCreated.ToPlexDate();
                l.Year   = vids[0].DateTimeCreated.Year;
                l.Medias = new List <Media>();
                foreach (SVR_VideoLocal v in vids)
                {
                    if (v.Media == null)
                    {
                        continue;
                    }
                    var legacy = new Media(v.VideoLocalID, v.Media);
                    legacy.Parts.ForEach(a =>
                    {
                        if (string.IsNullOrEmpty(a.LocalKey))
                        {
                            a.LocalKey = v?.GetBestVideoLocalPlace()?.FullServerPath ?? null;
                        }
                    });
                    l.Medias.Add(legacy);
                }

                string title = ep.Title;
                if (!string.IsNullOrEmpty(title))
                {
                    l.Title = title;
                }

                string romaji = RepoFactory.AniDB_Episode_Title.GetByEpisodeIDAndLanguage(ep.AniDB_EpisodeID, "X-JAT")
                                .FirstOrDefault()?.Title;
                if (!string.IsNullOrEmpty(romaji))
                {
                    l.OriginalTitle = romaji;
                }

                AniDB_Episode aep = ep?.AniDB_Episode;
                if (aep != null)
                {
                    l.EpisodeNumber = aep.EpisodeNumber;
                    l.Index         = aep.EpisodeNumber;
                    l.EpisodeType   = aep.EpisodeType;
                    l.Rating        = (int)float.Parse(aep.Rating, CultureInfo.InvariantCulture);
                    AniDB_Vote vote =
                        RepoFactory.AniDB_Vote.GetByEntityAndType(ep.AnimeEpisodeID, AniDBVoteType.Episode);
                    if (vote != null)
                    {
                        l.UserRating = (int)(vote.VoteValue / 100D);
                    }

                    if (aep.GetAirDateAsDate().HasValue)
                    {
                        l.Year = aep.GetAirDateAsDate()?.Year ?? 0;
                        l.OriginallyAvailableAt = aep.GetAirDateAsDate()?.ToPlexDate();
                    }

                    #region TvDB

                    TvDB_Episode tvep = ep.TvDBEpisode;
                    if (tvep != null)
                    {
                        l.Thumb   = tvep.GenPoster(null);
                        l.Summary = tvep.Overview;
                        l.Season  = $"{tvep.SeasonNumber}x{tvep.EpisodeNumber:0#}";
                    }
                    #endregion
                }
                if (l.Thumb == null || l.Summary == null)
                {
                    l.Thumb   = ((IProvider)null).ConstructSupportImageLink("plex_404.png");
                    l.Summary = "Episode Overview not Available";
                }
            }
            l.Id = ep.AnimeEpisodeID;
            return(l);
        }
Esempio n. 7
0
        public override void ProcessCommand()
        {
            CrossRef_File_Episode xref = new CrossRef_File_Episode();

            try
            {
                xref.PopulateManually(vlocal, episode);
                if (Percentage > 0 && Percentage <= 100)
                {
                    xref.Percentage = Percentage;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Error populating XREF: {0}", vlocal.ToStringDetailed());
                throw;
            }
            RepoFactory.CrossRef_File_Episode.Save(xref);
            CommandRequest_WebCacheSendXRefFileEpisode cr = new CommandRequest_WebCacheSendXRefFileEpisode(xref.CrossRef_File_EpisodeID);

            cr.Save();

            if (ServerSettings.FileQualityFilterEnabled)
            {
                List <SVR_VideoLocal> videoLocals = episode.GetVideoLocals();
                if (videoLocals != null)
                {
                    videoLocals.Sort(FileQualityFilter.CompareTo);
                    List <SVR_VideoLocal> keep = videoLocals.Take(FileQualityFilter.Settings.MaxNumberOfFilesToKeep)
                                                 .ToList();
                    foreach (SVR_VideoLocal vl2 in keep)
                    {
                        videoLocals.Remove(vl2);
                    }
                    if (videoLocals.Contains(vlocal))
                    {
                        videoLocals.Remove(vlocal);
                    }
                    videoLocals = videoLocals.Where(FileQualityFilter.CheckFileKeep).ToList();

                    foreach (SVR_VideoLocal toDelete in videoLocals)
                    {
                        toDelete.Places.ForEach(a => a.RemoveAndDeleteFile());
                    }
                }
            }

            vlocal.Places.ForEach(a => { a.RenameAndMoveAsRequired(); });

            SVR_AnimeSeries ser = episode.GetAnimeSeries();

            ser.EpisodeAddedDate = DateTime.Now;
            RepoFactory.AnimeSeries.Save(ser, false, true);

            //Update will re-save
            ser.QueueUpdateStats();


            foreach (SVR_AnimeGroup grp in ser.AllGroupsAbove)
            {
                grp.EpisodeAddedDate = DateTime.Now;
                RepoFactory.AnimeGroup.Save(grp, false, false);
            }

            if (ServerSettings.AniDB_MyList_AddFiles)
            {
                CommandRequest_AddFileToMyList cmdAddFile = new CommandRequest_AddFileToMyList(vlocal.Hash);
                cmdAddFile.Save();
            }
        }
Esempio n. 8
0
        public static Video GenerateVideoFromAnimeEpisode(SVR_AnimeEpisode ep)
        {
            Video l = new Video();
            List <SVR_VideoLocal> vids = ep.GetVideoLocals();

            l.Type      = "episode";
            l.Summary   = "Episode Overview Not Available"; //TODO Intenationalization
            l.Id        = ep.AnimeEpisodeID.ToString();
            l.AnimeType = AnimeTypes.AnimeEpisode.ToString();
            if (vids.Count > 0)
            {
                //List<string> hashes = vids.Select(a => a.Hash).Distinct().ToList();
                l.Title                 = Path.GetFileNameWithoutExtension(vids[0].FileName);
                l.AddedAt               = vids[0].DateTimeCreated.ToUnixTime();
                l.UpdatedAt             = vids[0].DateTimeUpdated.ToUnixTime();
                l.OriginallyAvailableAt = vids[0].DateTimeCreated.ToPlexDate();
                l.Year   = vids[0].DateTimeCreated.Year.ToString();
                l.Medias = new List <Media>();
                foreach (SVR_VideoLocal v in vids)
                {
                    if (string.IsNullOrEmpty(v.Media?.Duration))
                    {
                        SVR_VideoLocal_Place pl = v.GetBestVideoLocalPlace();
                        if (pl?.RefreshMediaInfo() == true)
                        {
                            RepoFactory.VideoLocal.Save(v, true);
                        }
                    }
                    v.Media?.Parts?.Where(a => a != null)
                    ?.ToList()
                    ?.ForEach(a =>
                    {
                        if (string.IsNullOrEmpty(a.LocalKey))
                        {
                            a.LocalKey = v?.GetBestVideoLocalPlace()?.FullServerPath ?? null;
                        }
                    });
                    if (v.Media != null)
                    {
                        l.Medias.Add(v.Media);
                    }
                }

                AniDB_Episode aep = ep?.AniDB_Episode;
                if (aep != null)
                {
                    l.EpisodeNumber = aep.EpisodeNumber.ToString();
                    l.Index         = aep.EpisodeNumber.ToString();
                    l.Title         = aep.EnglishName;
                    l.OriginalTitle = aep.RomajiName;
                    l.EpisodeType   = aep.EpisodeType.ToString();
                    l.Rating        = float.Parse(aep.Rating, CultureInfo.InvariantCulture)
                                      .ToString(CultureInfo.InvariantCulture);
                    AniDB_Vote vote =
                        RepoFactory.AniDB_Vote.GetByEntityAndType(ep.AnimeEpisodeID, AniDBVoteType.Episode);
                    if (vote != null)
                    {
                        l.UserRating = (vote.VoteValue / 100D).ToString(CultureInfo.InvariantCulture);
                    }

                    if (aep.GetAirDateAsDate().HasValue)
                    {
                        l.Year = aep.GetAirDateAsDate()?.Year.ToString();
                        l.OriginallyAvailableAt = aep.GetAirDateAsDate()?.ToPlexDate();
                    }

                    #region TvDB

                    using (var session = DatabaseFactory.SessionFactory.OpenSession())
                    {
                        List <CrossRef_AniDB_TvDBV2> xref_tvdbv2 =
                            RepoFactory.CrossRef_AniDB_TvDBV2.GetByAnimeIDEpTypeEpNumber(aep.AnimeID, aep.EpisodeType,
                                                                                         aep.EpisodeNumber);
                        if (xref_tvdbv2?.Count > 0)
                        {
                            TvDB_Episode tvep = ep?.TvDBEpisode;

                            if (tvep != null)
                            {
                                l.Thumb   = tvep.GenPoster(null);
                                l.Summary = tvep.Overview;
                                l.Season  = $"{tvep.SeasonNumber}x{tvep.EpisodeNumber:0#}";
                            }
                            else
                            {
                                string          anime = "[Blank]";
                                SVR_AnimeSeries ser   = ep.GetAnimeSeries();
                                if (ser?.GetSeriesName() != null)
                                {
                                    anime = ser.GetSeriesName();
                                }
                                LogManager.GetCurrentClassLogger()
                                .Warn(
                                    $"Episode {aep.EpisodeNumber}: {aep.EnglishName} from {anime} is out of range" +
                                    " for its TvDB Link. Please check the TvDB links for it.");
                            }
                        }
                    }

                    #endregion

                    #region TvDB Overrides

                    CrossRef_AniDB_TvDB_Episode xref_tvdb =
                        RepoFactory.CrossRef_AniDB_TvDB_Episode.GetByAniDBEpisodeID(aep.AniDB_EpisodeID);
                    if (xref_tvdb != null)
                    {
                        TvDB_Episode tvdb_ep = RepoFactory.TvDB_Episode.GetByTvDBID(xref_tvdb.TvDBEpisodeID);
                        if (tvdb_ep != null)
                        {
                            l.Thumb   = tvdb_ep.GenPoster(null);
                            l.Summary = tvdb_ep.Overview;
                            l.Season  = $"{tvdb_ep.SeasonNumber}x{tvdb_ep.EpisodeNumber:0#}";
                        }
                    }

                    #endregion
                }
                if (l.Thumb == null || l.Summary == null)
                {
                    l.Thumb   = ((IProvider)null).ConstructSupportImageLink("plex_404.png");
                    l.Summary = "Episode Overview not Available";
                }
            }
            l.Id = ep.AnimeEpisodeID.ToString();
            return(l);
        }
Esempio n. 9
0
        internal static Episode GenerateFromAnimeEpisode(NancyContext ctx, SVR_AnimeEpisode aep, int uid, int level)
        {
            Episode ep = new Episode();
            CL_AnimeEpisode_User cae = aep?.GetUserContract(uid);

            if (cae != null)
            {
                TvDB_Episode tvep = aep.TvDBEpisode;
                ep.id   = aep.AnimeEpisodeID;
                ep.art  = new ArtCollection();
                ep.name = cae.AniDB_EnglishName;

                ep.year = cae.AniDB_AirDate?.Year.ToString(CultureInfo.InvariantCulture);
                ep.air  = cae.AniDB_AirDate?.ToPlexDate();

                ep.votes  = cae.AniDB_Votes;
                ep.rating = cae.AniDB_Rating;
                var userrating = aep.UserRating;
                if (userrating > 0)
                {
                    ep.userrating = userrating.ToString(CultureInfo.InvariantCulture);
                }
                if (double.TryParse(ep.rating, out double rating))
                {
                    // 0.1 should be the absolute lowest rating
                    if (rating > 10)
                    {
                        ep.rating = (rating / 100).ToString(CultureInfo.InvariantCulture);
                    }
                }

                ep.view     = cae.IsWatched() ? 1 : 0;
                ep.epnumber = cae.EpisodeNumber;
                ep.eptype   = aep.EpisodeTypeEnum.ToString();

                ep.art = new ArtCollection();

                if (tvep != null)
                {
                    ep.name = tvep.EpisodeName;
                    ep.art.thumb.Add(new Art {
                        index = 0, url = tvep.GenPoster(null)
                    });
                    ep.art.fanart.Add(new Art {
                        index = 0, url = tvep.GenPoster(null)
                    });
                    ep.summary = tvep.Overview;

                    int    zeroPadding   = tvep.EpisodeNumber.ToString().Length;
                    string episodeNumber = tvep.EpisodeNumber.ToString().PadLeft(zeroPadding, '0');
                    zeroPadding = tvep.SeasonNumber.ToString().Length;
                    string seasonNumber = tvep.SeasonNumber.ToString().PadLeft(zeroPadding, '0');

                    ep.season = $"{seasonNumber}x{episodeNumber}";
                    var airdate = tvep.AirDate;
                    if (airdate != null)
                    {
                        ep.air  = airdate.Value.ToPlexDate();
                        ep.year = airdate.Value.Year.ToString(CultureInfo.InvariantCulture);
                    }
                }
                if (string.IsNullOrEmpty(ep.summary))
                {
                    ep.summary = string.Intern("Episode Overview not Available");
                }
                if (ep.art.thumb.Count == 0)
                {
                    ep.art.thumb.Add(
                        new Art {
                        index = 0, url = APIHelper.ConstructSupportImageLink(ctx, "plex_404.png")
                    });
                    ep.art.fanart.Add(new Art
                    {
                        index = 0,
                        url   = APIHelper.ConstructSupportImageLink(ctx, "plex_404.png")
                    });
                }
                if (string.IsNullOrEmpty(ep.year))
                {
                    ep.year = aep.GetAnimeSeries().AirDate.Year.ToString(CultureInfo.InvariantCulture);
                }

                if (level > 0)
                {
                    List <SVR_VideoLocal> vls = aep.GetVideoLocals();
                    if (vls.Count > 0)
                    {
                        ep.files = new List <RawFile>();
                        foreach (SVR_VideoLocal vl in vls)
                        {
                            RawFile file = new RawFile(ctx, vl, (level - 1), uid);
                            ep.files.Add(file);
                        }
                    }
                }
            }

            return(ep);
        }
        public override void Run(IProgress <ICommand> progress = null)
        {
            try
            {
                ReportInit(progress);
                CrossRef_File_Episode xref = new CrossRef_File_Episode();
                try
                {
                    xref.PopulateManually_RA(vlocal, episode);
                    if (Percentage > 0 && Percentage <= 100)
                    {
                        xref.Percentage = Percentage;
                    }
                }
                catch (Exception ex)
                {
                    ReportError(progress, $"Error populating XREF: {vlocal.ToStringDetailed()}", ex);
                    return;
                }
                ReportUpdate(progress, 20);
                List <ICommand> cmds = new List <ICommand>();
                Repo.Instance.CrossRef_File_Episode.BeginAdd(xref).Commit();
                Queue.Instance.Add(new CmdWebCacheSendXRefFileEpisode(xref.CrossRef_File_EpisodeID));
                ReportUpdate(progress, 40);

                if (ServerSettings.Instance.FileQualityFilterEnabled)
                {
                    List <SVR_VideoLocal> videoLocals = episode.GetVideoLocals();
                    if (videoLocals != null)
                    {
                        videoLocals.Sort(FileQualityFilter.CompareTo);
                        List <SVR_VideoLocal> keep = videoLocals.Take(FileQualityFilter.Settings.MaxNumberOfFilesToKeep)
                                                     .ToList();
                        foreach (SVR_VideoLocal vl2 in keep)
                        {
                            videoLocals.Remove(vl2);
                        }
                        if (videoLocals.Contains(vlocal))
                        {
                            videoLocals.Remove(vlocal);
                        }
                        videoLocals = videoLocals.Where(FileQualityFilter.CheckFileKeep).ToList();

                        foreach (SVR_VideoLocal toDelete in videoLocals)
                        {
                            toDelete.Places.ForEach(a => a.RemoveAndDeleteFile());
                        }
                    }
                }
                ReportUpdate(progress, 60);

                vlocal.Places.ForEach(a => { a.RenameAndMoveAsRequired(); });

                SVR_AnimeSeries ser;
                using (var upd = Repo.Instance.AnimeSeries.BeginAddOrUpdate(() => episode.GetAnimeSeries()))
                {
                    upd.Entity.EpisodeAddedDate = DateTime.Now;
                    ser = upd.Commit((false, true, false, false));
                }

                //Update will re-save
                ser.QueueUpdateStats();
                ReportUpdate(progress, 80);

                Repo.Instance.AnimeGroup.BatchAction(ser.AllGroupsAbove, ser.AllGroupsAbove.Count, (grp, _) => grp.EpisodeAddedDate = DateTime.Now);

                if (ServerSettings.Instance.AniDb.MyList_AddFiles)
                {
                    cmds.Add(new CmdAniDBAddFileToMyList(vlocal.Hash));
                }
                if (cmds.Count > 0)
                {
                    Queue.Instance.AddRange(cmds);
                }
                ReportFinish(progress);
            }
            catch (Exception e)
            {
                ReportError(progress, $"Error processing ServerLinkFileManually: {VideoLocalID} - {EpisodeID} - {e}", e);
            }
        }
Esempio n. 11
0
        internal static Episode GenerateFromAnimeEpisode(NancyContext ctx, SVR_AnimeEpisode aep, int uid, int level)
        {
            Episode ep = new Episode();

            if (aep != null)
            {
                CL_AnimeEpisode_User cae = aep.GetUserContract(uid);
                if (cae != null)
                {
                    ep.id         = aep.AnimeEpisodeID;
                    ep.art        = new ArtCollection();
                    ep.name       = aep.PlexContract?.Title;
                    ep.summary    = aep.PlexContract?.Summary;
                    ep.year       = aep.PlexContract?.Year;
                    ep.air        = aep.PlexContract?.AirDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
                    ep.votes      = cae.AniDB_Votes;
                    ep.rating     = aep.PlexContract?.Rating;
                    ep.userrating = aep.PlexContract?.UserRating;
                    double rating;
                    if (double.TryParse(ep.rating, out rating))
                    {
                        // 0.1 should be the absolute lowest rating
                        if (rating > 10)
                        {
                            ep.rating = (rating / 100).ToString().Replace(',', '.');
                        }
                    }

                    ep.view     = cae.IsWatched() ? 1 : 0;
                    ep.epnumber = cae.EpisodeNumber;
                    ep.eptype   = aep.EpisodeTypeEnum.ToString();

                    ep.season = aep.PlexContract?.Season;

                    // until fanart refactor this will be good for start
                    if (aep.PlexContract?.Thumb != null)
                    {
                        ep.art.thumb.Add(new Art()
                        {
                            url   = APIHelper.ConstructImageLinkFromRest(ctx, aep.PlexContract?.Thumb),
                            index = 0
                        });
                    }
                    if (aep.PlexContract?.Art != null)
                    {
                        ep.art.fanart.Add(new Art()
                        {
                            url   = APIHelper.ConstructImageLinkFromRest(ctx, aep.PlexContract?.Art),
                            index = 0
                        });
                    }

                    if (level > 0)
                    {
                        List <SVR_VideoLocal> vls = aep.GetVideoLocals();
                        if (vls.Count > 0)
                        {
                            ep.files = new List <RawFile>();
                            foreach (SVR_VideoLocal vl in vls)
                            {
                                RawFile file = new RawFile(ctx, vl, (level - 1), uid);
                                ep.files.Add(file);
                            }
                        }
                    }
                }
            }

            return(ep);
        }