Example #1
0
        internal static Filters GenerateFromGroupFilter(HttpContext ctx, SVR_GroupFilter gf, int uid, bool nocast, bool notag, int level,
                                                        bool all, bool allpic, int pic, TagFilter.Filter tagfilter)
        {
            Filters f = new Filters
            {
                id   = gf.GroupFilterID,
                name = gf.GroupFilterName
            };

            var _   = new List <string>();
            var gfs = RepoFactory.GroupFilter.GetByParentID(f.id).AsParallel()
                      // Not invisible in clients
                      .Where(a => a.InvisibleInClients == 0 &&
                      // and Has groups or is a directory
                             ((a.GroupsIds.ContainsKey(uid) && a.GroupsIds[uid].Count > 0) ||
                              (a.FilterType & (int)GroupFilterType.Directory) == (int)GroupFilterType.Directory) &&
                             // and is not a blacklisted tag
                             !((a.FilterType & (int)GroupFilterType.Tag) != 0 &&
                               TagFilter.IsTagBlackListed(a.GroupFilterName, tagfilter)));

            if (level > 0)
            {
                var filters = gfs.Select(cgf =>
                                         Filter.GenerateFromGroupFilter(ctx, cgf, uid, nocast, notag, level - 1, all, allpic, pic,
                                                                        tagfilter)).ToList();

                if (gf.FilterType == ((int)GroupFilterType.Season | (int)GroupFilterType.Directory))
                {
                    f.filters = filters.OrderBy(a => a.name, new SeasonComparator()).Cast <Filters>().ToList();
                }
                else
                {
                    f.filters = filters.OrderByNatural(a => a.name).Cast <Filters>().ToList();
                }
                f.size = filters.Count;
            }
            else
            {
                f.size = gfs.Count();
            }

            f.url = APIV2Helper.ConstructFilterIdUrl(ctx, f.id);

            return(f);
        }
Example #2
0
        internal static Episode GenerateFromAnimeEpisode(HttpContext ctx, SVR_AnimeEpisode aep, int uid, int level, int pic = 1)
        {
            Episode ep = new Episode
            {
                id  = aep.AnimeEpisodeID,
                art = new ArtCollection()
            };

            if (aep.AniDB_Episode != null)
            {
                ep.eptype = aep.EpisodeTypeEnum.ToString();
                ep.aid    = aep.AniDB_Episode.AnimeID;
                ep.eid    = aep.AniDB_Episode.EpisodeID;
            }

            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.view_date = cae.WatchedDate;
                ep.epnumber  = cae.EpisodeNumber;
            }

            TvDB_Episode tvep = aep.TvDBEpisode;

            if (tvep != null)
            {
                if (!string.IsNullOrEmpty(tvep.EpisodeName))
                {
                    ep.name = tvep.EpisodeName;
                }

                if (pic > 0)
                {
                    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 (pic > 0 && ep.art.thumb.Count == 0)
            {
                ep.art.thumb.Add(
                    new Art {
                    index = 0, url = APIV2Helper.ConstructSupportImageLink(ctx, "plex_404.png")
                });
                ep.art.fanart.Add(new Art
                {
                    index = 0,
                    url   = APIV2Helper.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, aep);
                        ep.files.Add(file);
                    }
                }
            }

            return(ep);
        }
Example #3
0
        internal new static Filter GenerateFromGroupFilter(HttpContext ctx, SVR_GroupFilter gf, int uid, bool nocast, bool notag, int level,
                                                           bool all, bool allpic, int pic, TagFilter.Filter tagfilter)
        {
            List <Group> groups = new List <Group>();
            Filter       filter = new Filter
            {
                name = gf.GroupFilterName,
                id   = gf.GroupFilterID,
                size = 0
            };

            if (gf.GroupsIds.ContainsKey(uid))
            {
                HashSet <int> groupsh = gf.GroupsIds[uid];
                if (groupsh.Count != 0)
                {
                    filter.size = groupsh.Count;

                    // Populate Random Art
                    List <SVR_AnimeGroup> groupsList;

                    List <SVR_AnimeSeries> arts = null;
                    if (gf.SeriesIds.ContainsKey(uid))
                    {
                        var seriesList = gf.SeriesIds[uid].Select(RepoFactory.AnimeSeries.GetByID).ToList();
                        groupsList = seriesList.Select(a => a.AnimeGroupID).Distinct()
                                     .Select(RepoFactory.AnimeGroup.GetByID).ToList();
                        if (pic == 1)
                        {
                            arts = seriesList.Where(SeriesHasCompleteArt).Where(a => a != null).ToList();
                            if (arts.Count == 0)
                            {
                                arts = seriesList.Where(SeriesHasMostlyCompleteArt).Where(a => a != null).ToList();
                            }
                            if (arts.Count == 0)
                            {
                                arts = seriesList;
                            }
                        }
                    }
                    else
                    {
                        groupsList = new List <SVR_AnimeGroup>();
                    }

                    if (arts?.Count > 0)
                    {
                        Random rand  = new Random();
                        var    anime = arts[rand.Next(arts.Count)];

                        var fanarts = GetFanartFromSeries(anime);
                        if (fanarts.Any())
                        {
                            var fanart = fanarts[rand.Next(fanarts.Count)];
                            filter.art.fanart.Add(new Art
                            {
                                index = 0,
                                url   = APIHelper.ConstructImageLinkFromTypeAndId(ctx, (int)ImageEntityType.TvDB_FanArt,
                                                                                  fanart.TvDB_ImageFanartID)
                            });
                        }

                        var banners = GetBannersFromSeries(anime);
                        if (banners.Any())
                        {
                            var banner = banners[rand.Next(banners.Count)];
                            filter.art.banner.Add(new Art
                            {
                                index = 0,
                                url   = APIHelper.ConstructImageLinkFromTypeAndId(ctx, (int)ImageEntityType.TvDB_Banner,
                                                                                  banner.TvDB_ImageWideBannerID)
                            });
                        }

                        filter.art.thumb.Add(new Art
                        {
                            url = APIHelper.ConstructImageLinkFromTypeAndId(ctx, (int)ImageEntityType.AniDB_Cover,
                                                                            anime.AniDB_ID),
                            index = 0
                        });
                    }

                    Dictionary <CL_AnimeGroup_User, Group> order = new Dictionary <CL_AnimeGroup_User, Group>();
                    if (level > 0)
                    {
                        foreach (SVR_AnimeGroup ag in groupsList)
                        {
                            Group group =
                                Group.GenerateFromAnimeGroup(ctx, ag, uid, nocast, notag, (level - 1), all,
                                                             filter.id, allpic, pic, tagfilter);
                            groups.Add(group);
                            order.Add(ag.GetUserContract(uid), group);
                        }
                    }

                    if (groups.Count > 0)
                    {
                        // Proper Sorting!
                        IEnumerable <CL_AnimeGroup_User> grps = order.Keys;
                        grps = gf.SortCriteriaList.Count != 0
                            ? GroupFilterHelper.Sort(grps, gf)
                            : grps.OrderBy(a => a.GroupName);
                        groups        = grps.Select(a => order[a]).ToList();
                        filter.groups = groups;
                    }
                }
            }

            filter.viewed = 0;
            filter.url    = APIV2Helper.ConstructFilterIdUrl(ctx, filter.id);

            return(filter);
        }
Example #4
0
        public RawFile(HttpContext ctx, SVR_VideoLocal vl, int level, int uid, AnimeEpisode e = null)
        {
            if (vl == null)
            {
                return;
            }

            id = vl.VideoLocalID;

            crc32    = vl.CRC32;
            ed2khash = vl.ED2KHash;
            md5      = vl.MD5;
            sha1     = vl.SHA1;

            created  = vl.DateTimeCreated;
            updated  = vl.DateTimeUpdated;
            duration = vl.Duration;

            var releaseGroup = vl.ReleaseGroup;

            if (releaseGroup != null)
            {
                group_full  = releaseGroup.GroupName;
                group_short = releaseGroup.GroupNameShort;
                group_id    = releaseGroup.AniDB_ReleaseGroupID;
            }

            size        = vl.FileSize;
            hash        = vl.Hash;
            hash_source = vl.HashSource;

            is_ignored = vl.IsIgnored;
            var vl_user = vl.GetUserRecord(uid);

            offset = vl_user?.ResumePosition ?? 0;

            var place = vl.GetBestVideoLocalPlace();

            if (place != null)
            {
                filename            = place.FilePath;
                server_path         = place.FullServerPath;
                videolocal_place_id = place.VideoLocal_Place_ID;
                import_folder_id    = place.ImportFolderID;
            }

            url = APIV2Helper.ConstructVideoLocalStream(ctx, uid, vl.VideoLocalID.ToString(),
                                                        "file" + Path.GetExtension(filename), false);

            recognized = e != null || vl.EpisodeCrossRefs.Count != 0;

            if (vl.Media == null || level < 0)
            {
                return;
            }

            MediaInfo new_media = new MediaInfo();

            new_media.AddGeneral(MediaInfo.General.format, vl.Media.Container);
            new_media.AddGeneral(MediaInfo.General.duration, vl.Media.Duration);
            new_media.AddGeneral(MediaInfo.General.id, vl.Media.Id);
            new_media.AddGeneral(MediaInfo.General.overallbitrate, vl.Media.Bitrate);

            if (vl.Media.Parts != null)
            {
                new_media.AddGeneral(MediaInfo.General.size, vl.Media.Parts[0].Size);

                foreach (Stream p in vl.Media.Parts[0].Streams)
                {
                    switch (p.StreamType)
                    {
                    //video
                    case 1:
                        new_media.AddVideo(p);
                        break;

                    //audio
                    case 2:
                        new_media.AddAudio(p);
                        break;

                    //subtitle
                    case 3:
                        new_media.AddSubtitle(p);
                        break;

                    //menu
                    case 4:
                        Dictionary <string, string> mdict = new Dictionary <string, string>();
                        //TODO APIv2: menu object could be usefull for external players
                        new_media.AddMenu(mdict);
                        break;
                    }
                }
            }

            media = new_media;
        }