Ejemplo n.º 1
0
        public static List <Tag> GetTags(HttpContext ctx, SVR_AniDB_Anime anime, TagFilter.Filter filter, bool excludeDescriptions = false)
        {
            var tags = new List <Tag>();

            var allTags      = anime.GetAniDBTags().DistinctBy(a => a.TagName).ToList();
            var filteredTags = TagFilter.ProcessTags(filter, allTags, tag => tag.TagName);

            foreach (AniDB_Tag tag in filteredTags)
            {
                var toAPI = new Tag
                {
                    Name = tag.TagName
                };
                var animeXRef = RepoFactory.AniDB_Anime_Tag.GetByTagID(tag.TagID).FirstOrDefault();
                if (animeXRef != null)
                {
                    toAPI.Weight = animeXRef.Weight;
                }
                if (!excludeDescriptions)
                {
                    toAPI.Description = tag.TagDescription;
                }

                tags.Add(toAPI);
            }

            return(tags);
        }
Ejemplo n.º 2
0
        internal static Filters GenerateFromGroupFilter(NancyContext ctx, SVR_GroupFilter gf, int uid, bool nocast, bool notag, bool all,
            int level, bool allpic, int pic, byte tagfilter)
        {
            Filters f = new Filters
            {
                id = gf.GroupFilterID,
                name = gf.GroupFilterName
            };
            List<SVR_GroupFilter> allGfs = RepoFactory.GroupFilter.GetByParentID(f.id)
                .Where(a => a.InvisibleInClients == 0 &&
                            ((a.GroupsIds.ContainsKey(uid) && a.GroupsIds[uid].Count > 0) ||
                             (a.FilterType & (int) GroupFilterType.Directory) == (int) GroupFilterType.Directory))
                .ToList();
            List<Filter> filters = allGfs
                .Where(cgf =>
                    (cgf.FilterType & (int) GroupFilterType.Tag) != (int) GroupFilterType.Tag ||
                    TagFilter.ProcessTags(tagfilter, new List<string> {cgf.GroupFilterName}).Count != 0)
                .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()).ToList();
            else
                f.filters = filters.OrderByNatural(a => a.name).ToList();

            f.size = f.filters.Count();
            f.url = APIHelper.ConstructFilterIdUrl(ctx, f.id);

            return f;
        }
Ejemplo n.º 3
0
        public static List <Tag> GetTags(HttpContext ctx, SVR_AniDB_Anime anime, TagFilter.Filter filter, bool excludeDescriptions = false)
        {
            // TODO This is probably slow. Make it faster.
            var tags = new List <Tag>();

            var allTags      = anime.GetAniDBTags().DistinctBy(a => a.TagName).ToDictionary(a => a.TagName, a => a);
            var filteredTags = TagFilter.ProcessTags(filter, allTags.Keys.ToList());

            foreach (string filteredTag in filteredTags)
            {
                AniDB_Tag tag = allTags.ContainsKey(filteredTag)
                    ? allTags[filteredTag]
                    : RepoFactory.AniDB_Tag.GetByName(filteredTag).FirstOrDefault();
                if (tag == null)
                {
                    tags.Add(new Tag {
                        Name = filteredTag
                    });
                    continue;
                }
                var toAPI = new Tag
                {
                    Name = tag.TagName
                };
                if (!excludeDescriptions)
                {
                    toAPI.Description = tag.TagDescription;
                }
                tags.Add(toAPI);
            }

            return(tags);
        }
Ejemplo n.º 4
0
        public List <Tag> GetTopTags(int number)
        {
            var tags = RepoFactory.AniDB_Anime_Tag.GetAllForLocalSeries().GroupBy(a => a.TagID)
                       .ToDictionary(a => a.Key, a => a.Count()).OrderByDescending(a => a.Value)
                       .Select(a => RepoFactory.AniDB_Tag.GetByTagID(a.Key))
                       .Where(a => a != null && !User.GetHideCategories().Contains(a.TagName)).Select(a => new Tag
            {
                Name        = a.TagName,
                Description = a.TagDescription,
                Weight      = 0
            }).ToList();
            var tagfilter = TagFilter.Filter.AnidbInternal | TagFilter.Filter.Misc | TagFilter.Filter.Source;

            tags = TagFilter.ProcessTags(tagfilter, tags, tag => tag.Name).Take(10).ToList();
            return(tags);
        }
Ejemplo n.º 5
0
        internal static Filters GenerateFromGroupFilter(NancyContext ctx, SVR_GroupFilter gf, int uid, bool nocast, bool notag, bool all,
                                                        int level, bool allpic, int pic, byte tagfilter)
        {
            Filters f = new Filters
            {
                id   = gf.GroupFilterID,
                name = gf.GroupFilterName
            };
            List <Filter>          filters = new List <Filter>();
            List <SVR_GroupFilter> allGfs  = RepoFactory.GroupFilter.GetByParentID(f.id)
                                             .Where(a => a.InvisibleInClients == 0 &&
                                                    ((a.GroupsIds.ContainsKey(uid) && a.GroupsIds[uid].Count > 0) ||
                                                     (a.FilterType & (int)GroupFilterType.Directory) == (int)GroupFilterType.Directory))
                                             .ToList();

            foreach (SVR_GroupFilter cgf in allGfs)
            {
                // any level higher than 1 can drain cpu
                if ((cgf.FilterType & (int)GroupFilterType.Tag) == (int)GroupFilterType.Tag &&
                    TagFilter.ProcessTags(tagfilter, new List <string> {
                    cgf.GroupFilterName
                }).Count == 0)
                {
                    continue;
                }

                filters.Add(Filter.GenerateFromGroupFilter(ctx, cgf, uid, nocast, notag, level - 1, all, allpic, pic, tagfilter));
            }

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

            f.size = f.filters.Count();
            f.url  = APIHelper.ConstructFilterIdUrl(ctx, f.id);

            return(f);
        }
Ejemplo n.º 6
0
        public object GetStats()
        {
            SVR_JMMUser user = HttpContext.User.Identity as SVR_JMMUser;

            int    series_count;
            int    file_count;
            string size;

            int  watched_files  = 0;
            int  watched_series = 0;
            long hours          = 0;

            List <string> tags;

            if (user != null)
            {
                var series = Repo.Instance.AnimeSeries.GetAll().Where(a =>
                                                                      !a.GetAnime()?.GetAllTags().FindInEnumerable(user.GetHideCategories()) ?? false).ToList();
                series_count = series.Count;

                var files = series.SelectMany(a => a.GetAnimeEpisodes()).SelectMany(a => a.GetVideoLocals())
                            .DistinctBy(a => a.VideoLocalID).ToList();
                file_count = files.Count;
                size       = SizeSuffix(files.Sum(a => a.FileSize));

                var watched = Repo.Instance.VideoLocal_User.GetByUserID(user.JMMUserID)
                              .Where(a => a.WatchedDate != null).ToList();

                watched_files = watched.Count;

                watched_series = Repo.Instance.AnimeSeries.GetAll().Count(a =>
                {
                    var contract = a.GetUserContract(user.JMMUserID);
                    if (contract?.MissingEpisodeCount > 0)
                    {
                        return(false);
                    }
                    return(contract?.UnwatchedEpisodeCount == 0);
                });

                hours = watched.Select(a => Repo.Instance.VideoLocal.GetByID(a.VideoLocalID)).Where(a => a != null)
                        .Sum(a => a.Duration) / 3600000; // 1000ms * 60s * 60m = ?h

                tags = Repo.Instance.AniDB_Anime_Tag.GetAllForLocalSeries().GroupBy(a => a.TagID)
                       .ToDictionary(a => a.Key, a => a.Count()).OrderByDescending(a => a.Value)
                       .Select(a => Repo.Instance.AniDB_Tag.GetByID(a.Key)?.TagName)
                       .Where(a => a != null && !user.GetHideCategories().Contains(a)).ToList();
                var tagfilter = TagFilter.Filter.AnidbInternal | TagFilter.Filter.Misc | TagFilter.Filter.Source;
                tags = TagFilter.ProcessTags(tagfilter, tags).Take(10).ToList();
            }
            else
            {
                var series = Repo.Instance.AnimeSeries.GetAll();
                series_count = series.Count;

                var files = series.SelectMany(a => a.GetAnimeEpisodes()).SelectMany(a => a.GetVideoLocals())
                            .DistinctBy(a => a.VideoLocalID).ToList();
                file_count = files.Count;
                size       = SizeSuffix(files.Sum(a => a.FileSize));

                tags = Repo.Instance.AniDB_Anime_Tag.GetAllForLocalSeries().GroupBy(a => a.TagID)
                       .ToDictionary(a => a.Key, a => a.Count()).OrderByDescending(a => a.Value)
                       .Select(a => Repo.Instance.AniDB_Tag.GetByID(a.Key)?.TagName)
                       .Where(a => a != null).ToList();
                var tagfilter = TagFilter.Filter.AnidbInternal | TagFilter.Filter.Misc | TagFilter.Filter.Source;
                tags = TagFilter.ProcessTags(tagfilter, tags).Take(10).ToList();
            }

            return(new Dictionary <string, object>
            {
                { "queue", Repo.Instance.CommandRequest.GetByClasses().ToDictionary(a => FromLastPoint(a.Key), a => a.Value) },
                { "file_count", file_count },
                { "series_count", series_count },
                { "collection_size", size },
                { "watched_files", watched_files },
                { "watched_series", watched_series },
                { "hours_watched", hours },
                { "tags", tags }
            });
        }
Ejemplo n.º 7
0
        public static Serie GenerateFromAniDB_Anime(NancyContext ctx, SVR_AniDB_Anime anime, bool nocast, bool notag, bool allpics, int pic, TagFilter.Filter tagfilter)
        {
            Serie sr = new Serie
            {
                // 0 will load all
                id      = -1,
                aid     = anime.AnimeID,
                summary = anime.Description,
                rating  = Math.Round(anime.Rating / 100D, 1)
                          .ToString(CultureInfo.InvariantCulture),
                votes   = anime.VoteCount.ToString(),
                name    = anime.MainTitle,
                ismovie = anime.AnimeType == (int)AnimeType.Movie ? 1 : 0
            };

            if (anime.AirDate != null)
            {
                sr.year = anime.AirDate.Value.Year.ToString();
                var airdate = anime.AirDate.Value;
                if (airdate != DateTime.MinValue)
                {
                    sr.air = airdate.ToPlexDate();
                }
            }

            AniDB_Vote vote = RepoFactory.AniDB_Vote.GetByEntityAndType(anime.AnimeID, AniDBVoteType.Anime) ??
                              RepoFactory.AniDB_Vote.GetByEntityAndType(anime.AnimeID, AniDBVoteType.AnimeTemp);

            if (vote != null)
            {
                sr.userrating = Math.Round(vote.VoteValue / 100D, 1).ToString(CultureInfo.InvariantCulture);
            }
            sr.titles = anime.GetTitles().Select(title =>
                                                 new AnimeTitle {
                Language = title.Language, Title = title.Title, Type = title.TitleType
            }).ToList();

            PopulateArtFromAniDBAnime(ctx, anime, sr, allpics, pic);

            if (!nocast)
            {
                var xref_animestaff = RepoFactory.CrossRef_Anime_Staff.GetByAnimeIDAndRoleType(anime.AnimeID,
                                                                                               StaffRoleType.Seiyuu);
                foreach (var xref in xref_animestaff)
                {
                    if (xref.RoleID == null)
                    {
                        continue;
                    }
                    var character = RepoFactory.AnimeCharacter.GetByID(xref.RoleID.Value);
                    if (character == null)
                    {
                        continue;
                    }
                    var staff = RepoFactory.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 (sr.roles == null)
                    {
                        sr.roles = new List <Role>();
                    }
                    sr.roles.Add(role);
                }
            }

            if (!notag)
            {
                var tags = anime.GetAllTags();
                if (tags != null)
                {
                    sr.tags = TagFilter.ProcessTags(tagfilter, tags.ToList());
                }
            }

            return(sr);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        public static Serie GenerateFromAnimeSeries(NancyContext ctx, SVR_AnimeSeries ser, int uid, bool nocast, bool notag, int level, bool all, bool allpics, int pic, TagFilter.Filter tagfilter)
        {
            Serie sr = new Serie();

            List <SVR_AnimeEpisode> ael = ser.GetAnimeEpisodes();
            var contract = ser.Contract;

            if (contract == null)
            {
                ser.UpdateContract();
            }

            sr.id      = ser.AnimeSeriesID;
            sr.summary = contract.AniDBAnime.AniDBAnime.Description;
            sr.year    = contract.AniDBAnime.AniDBAnime.BeginYear.ToString();
            var airdate = ser.AirDate;

            if (airdate != DateTime.MinValue)
            {
                sr.air = airdate.ToPlexDate();
            }

            GenerateSizes(sr, ael, uid);

            sr.rating = Math.Round(contract.AniDBAnime.AniDBAnime.Rating / 100D, 1)
                        .ToString(CultureInfo.InvariantCulture);
            AniDB_Vote vote = RepoFactory.AniDB_Vote.GetByEntityAndType(ser.AniDB_ID, AniDBVoteType.Anime) ??
                              RepoFactory.AniDB_Vote.GetByEntityAndType(ser.AniDB_ID, AniDBVoteType.AnimeTemp);

            if (vote != null)
            {
                sr.userrating = Math.Round(vote.VoteValue / 100D, 1).ToString(CultureInfo.InvariantCulture);
            }
            sr.titles = ser.GetAnime().GetTitles().Select(title =>
                                                          new AnimeTitle {
                Language = title.Language, Title = title.Title, Type = title.TitleType
            }).ToList();
            sr.name = ser.GetSeriesNameFromContract(contract);

            var ls = contract.CrossRefAniDBTvDBV2?.OrderBy(a => a.TvDBSeasonNumber).FirstOrDefault();

            if ((ls?.TvDBSeasonNumber ?? 0) != 0)
            {
                sr.season = ls.TvDBSeasonNumber.ToString();
            }

            if (contract.AniDBAnime.AniDBAnime.AnimeType == (int)AnimeType.Movie)
            {
                sr.ismovie = 1;
            }

            #region Images

            var anime = ser.GetAnime();
            if (anime != null)
            {
                Random rand = new Random();
                if (allpics || pic > 1)
                {
                    if (allpics)
                    {
                        pic = 999;
                    }
                    int pic_index = 0;
                    if (anime.AllPosters != null)
                    {
                        foreach (var cont_image in anime.AllPosters)
                        {
                            if (pic_index < pic)
                            {
                                sr.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;
                    if (anime.Contract.AniDBAnime.Fanarts != null)
                    {
                        foreach (var cont_image in anime.Contract.AniDBAnime.Fanarts)
                        {
                            if (pic_index < pic)
                            {
                                sr.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;
                    if (anime.Contract.AniDBAnime.Banners != null)
                    {
                        foreach (var cont_image in anime.Contract.AniDBAnime.Banners)
                        {
                            if (pic_index < pic)
                            {
                                sr.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
                {
                    sr.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)];
                        sr.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)];
                        sr.art.banner.Add(new Art()
                        {
                            url = APIHelper.ConstructImageLinkFromTypeAndId(ctx, art.ImageType,
                                                                            art.AniDB_Anime_DefaultImageID),
                            index = 0
                        });
                    }
                }
            }

            #endregion

            if (!nocast)
            {
                var xref_animestaff = RepoFactory.CrossRef_Anime_Staff.GetByAnimeIDAndRoleType(ser.AniDB_ID,
                                                                                               StaffRoleType.Seiyuu);
                foreach (var xref in xref_animestaff)
                {
                    if (xref.RoleID == null)
                    {
                        continue;
                    }
                    var character = RepoFactory.AnimeCharacter.GetByID(xref.RoleID.Value);
                    if (character == null)
                    {
                        continue;
                    }
                    var staff = RepoFactory.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 (sr.roles == null)
                    {
                        sr.roles = new List <Role>();
                    }
                    sr.roles.Add(role);
                }
            }

            if (!notag)
            {
                var tags = ser.Contract.AniDBAnime.AniDBAnime.GetAllTags();
                if (tags != null)
                {
                    sr.tags = TagFilter.ProcessTags(tagfilter, tags.ToList());
                }
            }

            if (level > 0)
            {
                if (ael.Count > 0)
                {
                    sr.eps = new List <Episode>();
                    foreach (SVR_AnimeEpisode ae in ael)
                    {
                        if (!all && (ae?.GetVideoLocals()?.Count ?? 0) == 0)
                        {
                            continue;
                        }
                        Episode new_ep = Episode.GenerateFromAnimeEpisode(ctx, ae, uid, (level - 1));
                        if (new_ep != null)
                        {
                            sr.eps.Add(new_ep);
                        }
                        if (level - 1 > 0)
                        {
                            foreach (RawFile file in new_ep.files)
                            {
                                sr.filesize += file.size;
                            }
                        }
                    }
                    sr.eps = sr.eps.OrderBy(a => a.epnumber).ToList();
                }
            }

            return(sr);
        }
Ejemplo n.º 10
0
        public static Serie GenerateFromAnimeSeries(HttpContext ctx, SVR_AnimeSeries ser, int uid, bool nocast, bool notag, int level, bool all, bool allpics, int pic, TagFilter.Filter tagfilter)
        {
            Serie sr = GenerateFromAniDB_Anime(ctx, ser.GetAnime(), nocast, notag, allpics, pic, tagfilter);

            List <SVR_AnimeEpisode> ael = ser.GetAnimeEpisodes();
            var contract = ser.Contract;

            if (contract == null)
            {
                ser.UpdateContract();
            }

            sr.id   = ser.AnimeSeriesID;
            sr.name = ser.GetSeriesName();
            GenerateSizes(sr, ael, uid);

            int?season = ael.FirstOrDefault(a =>
                                            a.AniDB_Episode != null && (a.AniDB_Episode.EpisodeType == (int)EpisodeType.Episode && a.AniDB_Episode.EpisodeNumber == 1))
                         ?.TvDBEpisode?.SeasonNumber;

            if (season != null)
            {
                sr.season = season.Value.ToString();
            }

            var tvdbseriesID = ael.Select(a => a.TvDBEpisode).Where(a => a != null).GroupBy(a => a.SeriesID)
                               .MaxBy(a => a.Count()).FirstOrDefault()?.Key;

            if (tvdbseriesID != null)
            {
                var tvdbseries = Repo.Instance.TvDB_Series.GetByTvDBID(tvdbseriesID.Value);
                if (tvdbseries != null)
                {
                    var title = new AnimeTitle {
                        Language = "EN", Title = tvdbseries.SeriesName, Type = "TvDB"
                    };
                    sr.titles.Add(title);
                }
            }

            if (!notag)
            {
                var tags = ser.Contract.AniDBAnime.AniDBAnime.GetAllTags();
                if (tags != null)
                {
                    sr.tags = TagFilter.ProcessTags(tagfilter, tags.ToList());
                }
            }

            if (level > 0)
            {
                if (ael.Count > 0)
                {
                    sr.eps = new List <Episode>();
                    foreach (SVR_AnimeEpisode ae in ael)
                    {
                        if (!all && (ae?.GetVideoLocals()?.Count ?? 0) == 0)
                        {
                            continue;
                        }
                        Episode new_ep = Episode.GenerateFromAnimeEpisode(ctx, ae, uid, (level - 1), pic);
                        if (new_ep == null)
                        {
                            continue;
                        }

                        sr.eps.Add(new_ep);

                        if (level - 1 <= 0)
                        {
                            continue;
                        }
                        foreach (RawFile file in new_ep.files)
                        {
                            sr.filesize += file.size;
                        }
                    }
                    sr.eps = sr.eps.OrderBy(a => a.epnumber).ToList();
                }
            }

            return(sr);
        }
Ejemplo n.º 11
0
        public void GenerateFromAniDB_Anime(HttpContext ctx, SVR_AniDB_Anime anime)
        {
            anidb_id   = anime.AnimeID;
            restricted = anime.Restricted == 1;
            if (ctx.Items.ContainsKey("description"))
            {
                description = new List <Description>
                {
                    new Description {
                        source = "AniDB", language = "en", description = anime.Description
                    }
                };
            }

            ratings = new List <Rating>
            {
                new Rating
                {
                    source     = "AniDB",
                    rating     = (decimal)anime.Rating / 10,
                    max_rating = 100,
                    votes      = anime.VoteCount
                }
            };
            name        = anime.MainTitle;
            series_type = anime.AnimeType.ToString();

            if (anime.AirDate != null)
            {
                var airdate = anime.AirDate.Value;
                if (airdate != DateTime.MinValue)
                {
                    air_date = airdate.ToString("yyyy-MM-dd");
                }
            }
            if (anime.EndDate != null)
            {
                var enddate = anime.EndDate.Value;
                if (enddate != DateTime.MinValue)
                {
                    end_date = enddate.ToString("yyyy-MM-dd");
                }
            }

            AniDB_Vote vote = Repo.Instance.AniDB_Vote.GetByEntityAndType(anime.AnimeID, AniDBVoteType.Anime) ??
                              Repo.Instance.AniDB_Vote.GetByEntityAndType(anime.AnimeID, AniDBVoteType.AnimeTemp);

            if (vote != null)
            {
                string voteType = (AniDBVoteType)vote.VoteType == AniDBVoteType.Anime ? "Permanent" : "Temporary";
                user_rating = new Rating
                {
                    rating = (decimal)Math.Round(vote.VoteValue / 100D, 1), max_rating = 10, type = voteType,
                    source = "User"
                };
            }

            if (ctx.Items.ContainsKey("titles"))
            {
                titles = anime.GetTitles().Select(title => new Title
                {
                    language = title.Language, title = title.Title, type = title.TitleType
                }).ToList();
            }

            PopulateArtFromAniDBAnime(ctx, anime);

            if (ctx.Items.ContainsKey("cast"))
            {
                var xref_animestaff = Repo.Instance.CrossRef_Anime_Staff.GetByAnimeIDAndRoleType(anime.AnimeID,
                                                                                                 StaffRoleType.Seiyuu);
                foreach (var xref in xref_animestaff)
                {
                    if (xref.RoleID == null)
                    {
                        continue;
                    }
                    AnimeCharacter character = Repo.Instance.AnimeCharacter.GetByID(xref.RoleID.Value);
                    if (character == null)
                    {
                        continue;
                    }
                    AnimeStaff staff = Repo.Instance.AnimeStaff.GetByID(xref.StaffID);
                    if (staff == null)
                    {
                        continue;
                    }
                    Role role = new Role
                    {
                        character = new Role.Person
                        {
                            name        = character.Name, alternate_name = character.AlternateName,
                            image       = new Image(ctx, xref.RoleID.Value, ImageEntityType.Character),
                            description = character.Description
                        },
                        staff = new Role.Person
                        {
                            name           = staff.Name,
                            alternate_name = staff.AlternateName,
                            description    = staff.Description,
                            image          = new Image(ctx, xref.StaffID, ImageEntityType.Staff),
                        },
                        role         = ((StaffRoleType)xref.RoleType).ToString(),
                        role_details = xref.Role
                    };
                    if (cast == null)
                    {
                        cast = new List <Role>();
                    }
                    cast.Add(role);
                }
            }

            if (ctx.Items.ContainsKey("tags"))
            {
                var animeTags = anime.GetAllTags();
                if (animeTags != null)
                {
                    if (!ctx.Items.TryGetValue("tagfilter", out object tagfilter))
                    {
                        tagfilter = 0;
                    }
                    tags = TagFilter.ProcessTags((TagFilter.Filter)tagfilter, animeTags.ToList());
                }
            }
        }
Ejemplo n.º 12
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);
        }