Example #1
0
        public static SearchResult GenerateFromAnimeSeries(NancyContext ctx, SVR_AnimeSeries ser, int uid, bool nocast, bool notag,
                                                           int level,
                                                           bool all, string match, bool allpic, int pic, byte tagfilter)
        {
            Serie serie = Serie.GenerateFromAnimeSeries(ctx, ser, uid, nocast, notag, level, all, allpic, pic, tagfilter);

            return(new SearchResult(serie, match));
        }
Example #2
0
        public static Group GenerateFromAnimeGroup(HttpContext ctx, SVR_AnimeGroup ag, int uid, bool nocast, bool notag, int level,
                                                   bool all, int filterid, bool allpic, int pic, TagFilter.Filter tagfilter)
        {
            Group g = new Group
            {
                name = ag.GroupName,
                id   = ag.AnimeGroupID,

                //g.videoqualities = ag.VideoQualities; <-- deadly trap
                added  = ag.DateTimeCreated,
                edited = ag.DateTimeUpdated
            };

            SVR_GroupFilter filter = null;

            if (filterid > 0)
            {
                filter = Repo.Instance.GroupFilter.GetByID(filterid);
                if (filter?.ApplyToSeries == 0)
                {
                    filter = null;
                }
            }

            List <SVR_AniDB_Anime> animes;

            if (filter != null)
            {
                animes = filter.SeriesIds[uid].Select(id => Repo.Instance.AnimeSeries.GetByID(id))
                         .Where(ser => ser?.AnimeGroupID == ag.AnimeGroupID).Select(ser => ser.GetAnime())
                         .Where(a => a != null).OrderBy(a => a.BeginYear).ThenBy(a => a.AirDate ?? DateTime.MaxValue)
                         .ToList();
            }
            else
            {
                animes = ag.Anime?.OrderBy(a => a.BeginYear).ThenBy(a => a.AirDate ?? DateTime.MaxValue).ToList();
            }

            if (animes != null && animes.Count > 0)
            {
                var anime = animes.FirstOrDefault(a => a != null);
                if (anime == null)
                {
                    return(g);
                }
                PopulateArtFromAniDBAnime(ctx, animes, g, allpic, pic);

                List <SVR_AnimeEpisode> ael;
                if (filter != null && filter.SeriesIds.ContainsKey(uid))
                {
                    var series = filter.SeriesIds[uid].Select(id => Repo.Instance.AnimeSeries.GetByID(id))
                                 .Where(ser => (ser?.AnimeGroupID ?? 0) == ag.AnimeGroupID).ToList();
                    ael = series.SelectMany(ser => ser?.GetAnimeEpisodes()).Where(a => a != null)
                          .ToList();
                    g.size = series.Count;
                }
                else
                {
                    var series = ag.GetAllSeries();
                    ael    = series.SelectMany(a => a?.GetAnimeEpisodes()).Where(a => a != null).ToList();
                    g.size = series.Count;
                }

                GenerateSizes(g, ael, uid);

                g.air = anime.AirDate?.ToPlexDate() ?? string.Empty;

                g.rating  = Math.Round(ag.AniDBRating / 100, 1).ToString(CultureInfo.InvariantCulture);
                g.summary = anime.Description ?? string.Empty;
                g.titles  = anime.GetTitles().Select(s => new Shoko.Models.PlexAndKodi.AnimeTitle()
                {
                    Type     = s.TitleType,
                    Language = s.Language,
                    Title    = s.Language
                }).ToList();//.ToAPIContract();
                g.year = anime.BeginYear.ToString();

                if (!notag && ag.Contract.Stat_AllTags != null)
                {
                    g.tags = TagFilter.ProcessTags(tagfilter, ag.Contract.Stat_AllTags.ToList());
                }

                if (!nocast)
                {
                    var xref_animestaff =
                        Repo.Instance.CrossRef_Anime_Staff.GetByAnimeIDAndRoleType(anime.AnimeID, StaffRoleType.Seiyuu);
                    foreach (var xref in xref_animestaff)
                    {
                        if (xref.RoleID == null)
                        {
                            continue;
                        }
                        var character = Repo.Instance.AnimeCharacter.GetByID(xref.RoleID.Value);
                        if (character == null)
                        {
                            continue;
                        }
                        var staff = Repo.Instance.AnimeStaff.GetByID(xref.StaffID);
                        if (staff == null)
                        {
                            continue;
                        }
                        var role = new Role
                        {
                            character       = character.Name,
                            character_image = APIHelper.ConstructImageLinkFromTypeAndId(ctx, (int)ImageEntityType.Character,
                                                                                        xref.RoleID.Value),
                            staff       = staff.Name,
                            staff_image = APIHelper.ConstructImageLinkFromTypeAndId(ctx, (int)ImageEntityType.Staff,
                                                                                    xref.StaffID),
                            role = xref.Role,
                            type = ((StaffRoleType)xref.RoleType).ToString()
                        };
                        if (g.roles == null)
                        {
                            g.roles = new List <Role>();
                        }
                        g.roles.Add(role);
                    }
                }
            }

            if (level > 0)
            {
                List <int> series = null;
                if (filter?.SeriesIds.ContainsKey(uid) == true)
                {
                    series = filter.SeriesIds[uid].ToList();
                }
                foreach (SVR_AnimeSeries ada in ag.GetSeries())
                {
                    if (series != null && series.Count > 0 && !series.Contains(ada.AnimeSeriesID))
                    {
                        continue;
                    }
                    g.series.Add(Serie.GenerateFromAnimeSeries(ctx, ada, uid, nocast, notag, (level - 1), all, allpic,
                                                               pic, tagfilter));
                }
                // This should be faster
                g.series.Sort();
            }

            return(g);
        }
Example #3
0
        public static Group GenerateFromAnimeGroup(NancyContext ctx, SVR_AnimeGroup ag, int uid, bool nocast, bool notag, int level,
                                                   bool all, int filterid, bool allpic, int pic, byte tagfilter)
        {
            Group g = new Group
            {
                name = ag.GroupName,
                id   = ag.AnimeGroupID,

                //g.videoqualities = ag.VideoQualities; <-- deadly trap
                added  = ag.DateTimeCreated,
                edited = ag.DateTimeUpdated
            };

            SVR_GroupFilter filter = null;

            if (filterid > 0)
            {
                filter = RepoFactory.GroupFilter.GetByID(filterid);
                if (filter?.ApplyToSeries == 0)
                {
                    filter = null;
                }
            }

            List <SVR_AniDB_Anime> animes;

            if (filter != null)
            {
                animes = filter.SeriesIds[uid].Select(id => RepoFactory.AnimeSeries.GetByID(id))
                         .Where(ser => ser?.AnimeGroupID == ag.AnimeGroupID).Select(ser => ser.GetAnime())
                         .Where(a => a != null).OrderBy(a => a.BeginYear).ThenBy(a => a.AirDate ?? DateTime.MaxValue)
                         .ToList();
            }
            else
            {
                animes = ag.Anime?.OrderBy(a => a.BeginYear).ThenBy(a => a.AirDate ?? DateTime.MaxValue).ToList();
            }

            if (animes != null && animes.Count > 0)
            {
                var    anime = animes?.FirstOrDefault();
                Random rand  = new Random();
                if (allpic || pic > 1)
                {
                    if (allpic)
                    {
                        pic = 999;
                    }
                    int pic_index = 0;
                    foreach (var ani in animes)
                    {
                        foreach (var cont_image in ani.AllPosters)
                        {
                            if (pic_index < pic)
                            {
                                g.art.thumb.Add(new Art
                                {
                                    url = APIHelper.ConstructImageLinkFromTypeAndId(ctx, cont_image.ImageType,
                                                                                    cont_image.AniDB_Anime_DefaultImageID),
                                    index = pic_index
                                });
                                pic_index++;
                            }
                            else
                            {
                                break;
                            }
                        }

                        pic_index = 0;
                        foreach (var cont_image in ani.Contract.AniDBAnime.Fanarts)
                        {
                            if (pic_index < pic)
                            {
                                g.art.fanart.Add(new Art
                                {
                                    url = APIHelper.ConstructImageLinkFromTypeAndId(ctx, cont_image.ImageType,
                                                                                    cont_image.AniDB_Anime_DefaultImageID),
                                    index = pic_index
                                });
                                pic_index++;
                            }
                            else
                            {
                                break;
                            }
                        }

                        pic_index = 0;
                        foreach (var cont_image in ani.Contract.AniDBAnime.Banners)
                        {
                            if (pic_index < pic)
                            {
                                g.art.banner.Add(new Art
                                {
                                    url = APIHelper.ConstructImageLinkFromTypeAndId(ctx, cont_image.ImageType,
                                                                                    cont_image.AniDB_Anime_DefaultImageID),
                                    index = pic_index
                                });
                                pic_index++;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
                else
                {
                    if (anime != null)
                    {
                        g.art.thumb.Add(new Art
                        {
                            url = APIHelper.ConstructImageLinkFromTypeAndId(ctx, (int)ImageEntityType.AniDB_Cover,
                                                                            anime.AnimeID),
                            index = 0
                        });

                        var fanarts = anime.Contract.AniDBAnime.Fanarts;
                        if (fanarts != null && fanarts.Count > 0)
                        {
                            var art = fanarts[rand.Next(fanarts.Count)];
                            g.art.fanart.Add(new Art
                            {
                                url = APIHelper.ConstructImageLinkFromTypeAndId(ctx, art.ImageType,
                                                                                art.AniDB_Anime_DefaultImageID),
                                index = 0
                            });
                        }

                        fanarts = anime.Contract.AniDBAnime.Banners;
                        if (fanarts != null && fanarts.Count > 0)
                        {
                            var art = fanarts[rand.Next(fanarts.Count)];
                            g.art.banner.Add(new Art
                            {
                                url = APIHelper.ConstructImageLinkFromTypeAndId(ctx, art.ImageType,
                                                                                art.AniDB_Anime_DefaultImageID),
                                index = 0
                            });
                        }
                    }
                }

                List <SVR_AnimeEpisode> ael;
                if (filter != null)
                {
                    ael = filter.SeriesIds[uid].Select(id => RepoFactory.AnimeSeries.GetByID(id))
                          .Where(ser => ser?.AnimeGroupID == ag.AnimeGroupID).SelectMany(ser => ser.GetAnimeEpisodes())
                          .ToList();
                }
                else
                {
                    ael = ag.GetAllSeries().SelectMany(a => a.GetAnimeEpisodes()).ToList();
                }

                GenerateSizes(g, ael, uid);

                g.air = anime?.AirDate?.ToPlexDate() ?? string.Empty;

                g.rating  = Math.Round(ag.AniDBRating / 100, 1).ToString(CultureInfo.InvariantCulture);
                g.summary = anime?.Description ?? string.Empty;
                g.titles  = anime?.GetTitles().ToAPIContract();
                g.year    = anime?.BeginYear.ToString();

                if (!notag && ag.Contract.Stat_AllTags != null)
                {
                    g.tags = TagFilter.ProcessTags(tagfilter, ag.Contract.Stat_AllTags.ToList())
                             .Select(value => new Tag {
                        tag = value
                    }).ToList();
                }
            }

            if (!nocast)
            {
                Video vag = ag.GetPlexContract(uid);
                if (vag?.Roles != null)
                {
                    g.roles = vag.Roles?.Select(rtg => new Role
                    {
                        name    = !string.IsNullOrEmpty(rtg.Value) ? rtg.Value : string.Empty,
                        namepic = !string.IsNullOrEmpty(rtg.TagPicture)
                            ? APIHelper.ConstructImageLinkFromRest(ctx, rtg.TagPicture)
                            : string.Empty,
                        role     = !string.IsNullOrEmpty(rtg.Role) ? rtg.Role : string.Empty,
                        roledesc = !string.IsNullOrEmpty(rtg.RoleDescription)
                            ? rtg.RoleDescription
                            : string.Empty,
                        rolepic = !string.IsNullOrEmpty(rtg.RolePicture)
                            ? APIHelper.ConstructImageLinkFromRest(ctx, rtg.RolePicture)
                            : string.Empty
                    }).ToList();
                }
            }

            if (level > 0)
            {
                List <int> series = null;
                if (filter?.SeriesIds.ContainsKey(uid) == true)
                {
                    series = filter.SeriesIds[uid].ToList();
                }
                foreach (SVR_AnimeSeries ada in ag.GetSeries())
                {
                    if (series != null && series.Count > 0 && !series.Contains(ada.AnimeSeriesID))
                    {
                        continue;
                    }
                    g.series.Add(Serie.GenerateFromAnimeSeries(ctx, ada, uid, nocast, notag, (level - 1), all, allpic,
                                                               pic, tagfilter));
                }
                // This should be faster
                g.series.Sort();
            }

            return(g);
        }
Example #4
0
        public static Group GenerateFromAnimeGroup(NancyContext ctx, SVR_AnimeGroup ag, int uid, bool nocast, bool notag, int level,
                                                   bool all, int filterid, bool allpic, int pic)
        {
            Group g = new Group
            {
                name = ag.GroupName,
                id   = ag.AnimeGroupID,

                //g.videoqualities = ag.VideoQualities; <-- deadly trap
                added  = ag.DateTimeCreated,
                edited = ag.DateTimeUpdated
            };
            Video vag = ag.GetPlexContract(uid);

            if (vag != null)
            {
                g.air = vag.OriginallyAvailableAt;

                List <SVR_AnimeEpisode> ael = ag.GetAllSeries().SelectMany(a => a.GetAnimeEpisodes()).ToList();

                GenerateSizes(g, ael, uid);

                g.rating     = vag.Rating;
                g.userrating = vag.UserRating;

                g.summary = vag.Summary;
                g.titles  = vag.Titles;
                g.year    = vag.Year;

                Random rand = new Random();
                Contract_ImageDetails art = new Contract_ImageDetails();
                // vag.Fanarts can be null even if contract isn't
                if (vag.Fanarts != null && vag.Fanarts.Count > 0)
                {
                    art = vag.Fanarts[rand.Next(vag.Fanarts.Count)];
                    g.art.fanart.Add(new Art()
                    {
                        url   = APIHelper.ConstructImageLinkFromTypeAndId(ctx, art.ImageType, art.ImageID),
                        index = 0
                    });
                }

                if (vag.Banners != null && vag.Banners.Count > 0)
                {
                    art = vag.Banners[rand.Next(vag.Banners.Count)];
                    g.art.banner.Add(new Art()
                    {
                        url   = APIHelper.ConstructImageLinkFromTypeAndId(ctx, art.ImageType, art.ImageID),
                        index = 0
                    });
                    if (!string.IsNullOrEmpty(vag.Thumb))
                    {
                        g.art.thumb.Add(new Art()
                        {
                            url = APIHelper.ConstructImageLinkFromRest(ctx, vag.Thumb), index = 0
                        });
                    }
                }

                if (!nocast)
                {
                    if (vag.Roles != null)
                    {
                        g.roles = new List <Role>();
                        foreach (RoleTag rtg in vag.Roles)
                        {
                            Role new_role = new Role();
                            if (!String.IsNullOrEmpty(rtg.Value))
                            {
                                new_role.name = rtg.Value;
                            }
                            else
                            {
                                new_role.name = "";
                            }
                            if (!String.IsNullOrEmpty(rtg.TagPicture))
                            {
                                new_role.namepic = APIHelper.ConstructImageLinkFromRest(ctx, rtg.TagPicture);
                            }
                            else
                            {
                                new_role.namepic = "";
                            }
                            if (!String.IsNullOrEmpty(rtg.Role))
                            {
                                new_role.role = rtg.Role;
                            }
                            else
                            {
                                rtg.Role = "";
                            }
                            if (!String.IsNullOrEmpty(rtg.RoleDescription))
                            {
                                new_role.roledesc = rtg.RoleDescription;
                            }
                            else
                            {
                                new_role.roledesc = "";
                            }
                            if (!String.IsNullOrEmpty(rtg.RolePicture))
                            {
                                new_role.rolepic = APIHelper.ConstructImageLinkFromRest(ctx, rtg.RolePicture);
                            }
                            else
                            {
                                new_role.rolepic = "";
                            }
                            g.roles.Add(new_role);
                        }
                    }
                }

                if (!notag)
                {
                    if (vag.Genres != null)
                    {
                        foreach (Shoko.Models.PlexAndKodi.Tag otg in vag.Genres)
                        {
                            Tag new_tag = new Tag
                            {
                                tag = otg.Value
                            };
                            g.tags.Add(new_tag);
                        }
                    }
                }
            }

            if (level > 0)
            {
                List <int> series = null;
                if (filterid > 0)
                {
                    SVR_GroupFilter filter = RepoFactory.GroupFilter.GetByID(filterid);
                    if (filter?.ApplyToSeries > 0)
                    {
                        if (filter.SeriesIds.ContainsKey(uid))
                        {
                            series = filter.SeriesIds[uid].ToList();
                        }
                    }
                }
                foreach (SVR_AnimeSeries ada in ag.GetSeries())
                {
                    if (series != null && series.Count > 0)
                    {
                        if (!series.Contains(ada.AnimeSeriesID))
                        {
                            continue;
                        }
                    }
                    g.series.Add(Serie.GenerateFromAnimeSeries(ctx, ada, uid, nocast, notag, (level - 1), all, allpic, pic));
                }
                // This should be faster
                g.series.Sort();
            }

            return(g);
        }