Exemplo n.º 1
0
        internal static Filters GenerateFromGroupFilter(NancyContext ctx, SVR_GroupFilter gf, int uid, bool nocast, bool notag, bool all,
                                                        int level)
        {
            Filters f = new Filters();

            f.id   = gf.GroupFilterID;
            f.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
                filters.Add(Filter.GenerateFromGroupFilter(ctx, cgf, uid, nocast, notag, level - 1, all));
            }

            f.filters = filters.OrderBy(a => a.name).ToList <Filter>();
            f.size    = f.filters.Count();
            f.url     = APIHelper.ConstructFilterIdUrl(ctx, f.id);

            return(f);
        }
Exemplo 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;
        }
        public override void ProcessCommand()
        {
            SVR_GroupFilter gf = RepoFactory.GroupFilter.GetByID(GroupFilterID);

            if (gf == null)
            {
                return;
            }
            gf.CalculateGroupsAndSeries();
            RepoFactory.GroupFilter.Save(gf);
        }
Exemplo n.º 4
0
        public override void ProcessCommand()
        {
            SVR_GroupFilter gf = RepoFactory.GroupFilter.GetByID(GroupFilterID);

            if (gf == null)
            {
                return;
            }
            gf.EvaluateAnimeSeries();
            gf.EvaluateAnimeGroups();
            RepoFactory.GroupFilter.Save(gf);
        }
Exemplo n.º 5
0
        public ActionResult <List <Group> > PreviewFilterChanges(Filter.FullFilter filter)
        {
            SVR_GroupFilter gf = filter.ToServerModel();

            gf.CalculateGroupsAndSeries();

            if (!gf.GroupsIds.ContainsKey(User.JMMUserID))
            {
                return(new List <Group>());
            }
            return(gf.GroupsIds[User.JMMUserID].Select(a => RepoFactory.AnimeGroup.GetByID(a))
                   .Where(a => a != null).GroupFilterSort(gf).Select(a => new Group(HttpContext, a)).ToList());
        }
Exemplo n.º 6
0
        public override void ProcessCommand()
        {
            SVR_GroupFilter gf = Repo.Instance.GroupFilter.GetByID(GroupFilterID);

            if (gf == null)
            {
                return;
            }
            using (var upd = Repo.Instance.GroupFilter.BeginAddOrUpdate(() => gf))
            {
                upd.Entity.CalculateGroupsAndSeries();
                upd.Commit();
            }
        }
Exemplo n.º 7
0
        private static void GetValidVideoRecursive(IProvider prov, SVR_GroupFilter f, int userid, Directory pp)
        {
            List <SVR_GroupFilter> gfs = RepoFactory.GroupFilter.GetByParentID(f.GroupFilterID)
                                         .Where(a => a.GroupsIds.ContainsKey(userid) && a.GroupsIds[userid].Count > 0)
                                         .ToList();

            foreach (SVR_GroupFilter gg in gfs.Where(a => (a.FilterType & (int)GroupFilterType.Directory) == 0))
            {
                if (gg.GroupsIds.ContainsKey(userid))
                {
                    HashSet <int> groups = gg.GroupsIds[userid];
                    if (groups.Count != 0)
                    {
                        foreach (int grp in groups.Randomize(f.GroupFilterID))
                        {
                            SVR_AnimeGroup ag = RepoFactory.AnimeGroup.GetByID(grp);
                            Video          v  = ag.GetPlexContract(userid);
                            if (v?.Art == null || v.Thumb == null)
                            {
                                continue;
                            }
                            pp.Art   = prov.ReplaceSchemeHost(v.Art);
                            pp.Thumb = prov.ReplaceSchemeHost(v.Thumb);
                            break;
                        }
                    }
                }
                if (pp.Art != null)
                {
                    break;
                }
            }
            if (pp.Art == null)
            {
                foreach (SVR_GroupFilter gg in gfs
                         .Where(a => (a.FilterType & (int)GroupFilterType.Directory) == (int)GroupFilterType.Directory &&
                                a.InvisibleInClients == 0)
                         .Randomize(f.GroupFilterID))
                {
                    GetValidVideoRecursive(prov, gg, userid, pp);
                    if (pp.Art != null)
                    {
                        break;
                    }
                }
            }
            pp.LeafCount       = gfs.Count;
            pp.ViewedLeafCount = 0;
        }
Exemplo n.º 8
0
        public override void ProcessCommand()
        {
            if (GroupFilterID == 0)
            {
                RepoFactory.GroupFilter.CreateOrVerifyLockedFilters();
                return;
            }
            SVR_GroupFilter gf = RepoFactory.GroupFilter.GetByID(GroupFilterID);

            if (gf == null)
            {
                return;
            }
            gf.CalculateGroupsAndSeries();
            RepoFactory.GroupFilter.Save(gf);
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
0
        public ActionResult <Filter> SaveFilter(Filter.FullFilter filter)
        {
            SVR_GroupFilter gf = null;

            if (filter.IDs.ID != 0)
            {
                gf = RepoFactory.GroupFilter.GetByID(filter.IDs.ID);
                if (gf == null)
                {
                    return(BadRequest("No Filter with ID"));
                }
            }
            gf = filter.ToServerModel(gf);
            gf.CalculateGroupsAndSeries();
            RepoFactory.GroupFilter.Save(gf);

            return(new Filter(HttpContext, gf));
        }
Exemplo n.º 11
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);
        }
Exemplo n.º 12
0
        public static Directory DirectoryFromFilter(IProvider prov, SVR_GroupFilter gg,
                                                    int userid)
        {
            Directory pp = new Directory {
                Type = "show"
            };

            pp.Key       = prov.ConstructFilterIdUrl(userid, gg.GroupFilterID);
            pp.Title     = gg.GroupFilterName;
            pp.Id        = gg.GroupFilterID;
            pp.AnimeType = AnimeTypes.AnimeGroupFilter.ToString();
            if ((gg.FilterType & (int)GroupFilterType.Directory) == (int)GroupFilterType.Directory)
            {
                GetValidVideoRecursive(prov, gg, userid, pp);
            }
            else if (gg.GroupsIds.ContainsKey(userid))
            {
                HashSet <int> groups = gg.GroupsIds[userid];
                if (groups.Count == 0)
                {
                    return(pp);
                }
                pp.LeafCount       = groups.Count;
                pp.ViewedLeafCount = 0;
                foreach (int grp in groups.Randomize())
                {
                    SVR_AnimeGroup ag = RepoFactory.AnimeGroup.GetByID(grp);
                    Video          v  = ag.GetPlexContract(userid);
                    if (v?.Art == null || v.Thumb == null)
                    {
                        continue;
                    }
                    pp.Art   = prov.ReplaceSchemeHost(v.Art);
                    pp.Thumb = prov.ReplaceSchemeHost(v.Thumb);
                    break;
                }
                return(pp);
            }
            return(pp);
        }
Exemplo n.º 13
0
        public ActionResult <Filter> SaveFilter(Filter.FullFilter body)
        {
            SVR_GroupFilter groupFilter = null;

            if (body.IDs.ID != 0)
            {
                groupFilter = RepoFactory.GroupFilter.GetByID(body.IDs.ID);
                if (groupFilter == null)
                {
                    return(NotFound(FilterNotFound));
                }
                if (groupFilter.Locked == 1)
                {
                    return(Forbid("Filter is Locked"));
                }
            }
            groupFilter = body.ToServerModel(groupFilter);
            groupFilter.CalculateGroupsAndSeries();
            RepoFactory.GroupFilter.Save(groupFilter);

            return(new Filter(HttpContext, groupFilter));
        }
Exemplo n.º 14
0
        public static Filter FilterFromGroupFilter(HttpContext ctx, SVR_GroupFilter gg, int uid)
        {
            Filter ob = new Filter
            {
                name = gg.GroupFilterName,
                id   = gg.GroupFilterID,
                url  = APIHelper.ConstructFilterIdUrl(ctx, gg.GroupFilterID)
            };

            if (gg.GroupsIds.ContainsKey(uid))
            {
                HashSet <int> groups = gg.GroupsIds[uid];
                if (groups.Count != 0)
                {
                    ob.size   = groups.Count;
                    ob.viewed = 0;

                    foreach (int grp in groups)
                    {
                        SVR_AnimeGroup ag = Repo.Instance.AnimeGroup.GetByID(grp);
                        Video          v  = ag.GetPlexContract(uid);
                        if (v?.Art != null && v.Thumb != null)
                        {
                            ob.art.fanart.Add(new Art()
                            {
                                url = APIHelper.ConstructImageLinkFromRest(ctx, v.Art), index = 0
                            });
                            ob.art.thumb.Add(new Art()
                            {
                                url   = APIHelper.ConstructImageLinkFromRest(ctx, v.Thumb),
                                index = 0
                            });
                            break;
                        }
                    }
                }
            }
            return(ob);
        }
Exemplo n.º 15
0
        public Filter(HttpContext ctx, SVR_GroupFilter gf)
        {
            IDs = new IDs {
                ID = gf.GroupFilterID
            };
            Name = gf.GroupFilterName;
            SVR_JMMUser user = ctx.GetUser();

            Directory = ((GroupFilterType)gf.FilterType).HasFlag(GroupFilterType.Directory);
            // This can be used to exclude from client visibility for user
            Size = gf.GroupsIds.ContainsKey(user.JMMUserID) ? gf.GroupsIds[user.JMMUserID].Count : 0;
            if (Directory)
            {
                Size += RepoFactory.GroupFilter.GetByParentID(gf.GroupFilterID).Count;
            }

            ApplyAtSeriesLevel = gf.ApplyToSeries == 1;

            // It's never null, just marked Nullable for some reason
            Locked = gf.Locked != null && gf.Locked.Value == 1;

            HideInAPI = gf.InvisibleInClients == 1;
        }
Exemplo n.º 16
0
        private void CreateInitialGroupFilters()
        {
            // group filters
            // Do to DatabaseFixes, some filters may be made, namely directory filters
            // All, Continue Watching, Years, Seasons, Tags... 6 seems to be enough to tell for now
            // We can't just check the existence of anything specific, as the user can delete most of these
            if (RepoFactory.GroupFilter.GetTopLevel().Count() > 6)
            {
                return;
            }

            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(ServerSettings.Instance.Culture);

            // Favorites
            SVR_GroupFilter gf = new SVR_GroupFilter
            {
                GroupFilterName = Commons.Properties.Resources.Filter_Favorites,
                ApplyToSeries   = 0,
                BaseCondition   = 1,
                Locked          = 0,
                FilterType      = (int)GroupFilterType.UserDefined
            };
            GroupFilterCondition gfc = new GroupFilterCondition
            {
                ConditionType      = (int)GroupFilterConditionType.Favourite,
                ConditionOperator  = (int)GroupFilterOperator.Include,
                ConditionParameter = string.Empty
            };

            gf.Conditions.Add(gfc);
            gf.CalculateGroupsAndSeries();
            RepoFactory.GroupFilter.Save(gf);

            // Missing Episodes
            gf = new SVR_GroupFilter
            {
                GroupFilterName = Commons.Properties.Resources.Filter_MissingEpisodes,
                ApplyToSeries   = 0,
                BaseCondition   = 1,
                Locked          = 0,
                FilterType      = (int)GroupFilterType.UserDefined
            };
            gfc = new GroupFilterCondition
            {
                ConditionType      = (int)GroupFilterConditionType.MissingEpisodesCollecting,
                ConditionOperator  = (int)GroupFilterOperator.Include,
                ConditionParameter = string.Empty
            };
            gf.Conditions.Add(gfc);
            gf.CalculateGroupsAndSeries();
            RepoFactory.GroupFilter.Save(gf);


            // Newly Added Series
            gf = new SVR_GroupFilter
            {
                GroupFilterName = Commons.Properties.Resources.Filter_Added,
                ApplyToSeries   = 0,
                BaseCondition   = 1,
                Locked          = 0,
                FilterType      = (int)GroupFilterType.UserDefined
            };
            gfc = new GroupFilterCondition
            {
                ConditionType      = (int)GroupFilterConditionType.SeriesCreatedDate,
                ConditionOperator  = (int)GroupFilterOperator.LastXDays,
                ConditionParameter = "10"
            };
            gf.Conditions.Add(gfc);
            gf.CalculateGroupsAndSeries();
            RepoFactory.GroupFilter.Save(gf);

            // Newly Airing Series
            gf = new SVR_GroupFilter
            {
                GroupFilterName = Commons.Properties.Resources.Filter_Airing,
                ApplyToSeries   = 0,
                BaseCondition   = 1,
                Locked          = 0,
                FilterType      = (int)GroupFilterType.UserDefined
            };
            gfc = new GroupFilterCondition
            {
                ConditionType      = (int)GroupFilterConditionType.AirDate,
                ConditionOperator  = (int)GroupFilterOperator.LastXDays,
                ConditionParameter = "30"
            };
            gf.Conditions.Add(gfc);
            gf.CalculateGroupsAndSeries();
            RepoFactory.GroupFilter.Save(gf);

            // Votes Needed
            gf = new SVR_GroupFilter
            {
                GroupFilterName = Commons.Properties.Resources.Filter_Votes,
                ApplyToSeries   = 1,
                BaseCondition   = 1,
                Locked          = 0,
                FilterType      = (int)GroupFilterType.UserDefined
            };
            gfc = new GroupFilterCondition
            {
                ConditionType      = (int)GroupFilterConditionType.CompletedSeries,
                ConditionOperator  = (int)GroupFilterOperator.Include,
                ConditionParameter = string.Empty
            };
            gf.Conditions.Add(gfc);
            gfc = new GroupFilterCondition
            {
                ConditionType      = (int)GroupFilterConditionType.HasUnwatchedEpisodes,
                ConditionOperator  = (int)GroupFilterOperator.Exclude,
                ConditionParameter = string.Empty
            };
            gf.Conditions.Add(gfc);
            gfc = new GroupFilterCondition
            {
                ConditionType      = (int)GroupFilterConditionType.UserVotedAny,
                ConditionOperator  = (int)GroupFilterOperator.Exclude,
                ConditionParameter = string.Empty
            };
            gf.Conditions.Add(gfc);
            gf.CalculateGroupsAndSeries();
            RepoFactory.GroupFilter.Save(gf);

            // Recently Watched
            gf = new SVR_GroupFilter
            {
                GroupFilterName = Commons.Properties.Resources.Filter_RecentlyWatched,
                ApplyToSeries   = 0,
                BaseCondition   = 1,
                Locked          = 0,
                FilterType      = (int)GroupFilterType.UserDefined
            };
            gfc = new GroupFilterCondition
            {
                ConditionType      = (int)GroupFilterConditionType.EpisodeWatchedDate,
                ConditionOperator  = (int)GroupFilterOperator.LastXDays,
                ConditionParameter = "10"
            };
            gf.Conditions.Add(gfc);
            gf.CalculateGroupsAndSeries();
            RepoFactory.GroupFilter.Save(gf);

            // TvDB/MovieDB Link Missing
            gf = new SVR_GroupFilter
            {
                GroupFilterName = Commons.Properties.Resources.Filter_LinkMissing,
                ApplyToSeries   = 1, // This makes far more sense as applied to series
                BaseCondition   = 1,
                Locked          = 0,
                FilterType      = (int)GroupFilterType.UserDefined
            };
            gfc = new GroupFilterCondition
            {
                ConditionType      = (int)GroupFilterConditionType.AssignedTvDBOrMovieDBInfo,
                ConditionOperator  = (int)GroupFilterOperator.Exclude,
                ConditionParameter = string.Empty
            };
            gf.Conditions.Add(gfc);
            gf.CalculateGroupsAndSeries();
            RepoFactory.GroupFilter.Save(gf);
        }
Exemplo n.º 17
0
        internal static Filter GenerateFromGroupFilter(NancyContext ctx, SVR_GroupFilter gf, int uid, bool nocast, bool notag, int level,
                                                       bool all, bool allpic, int pic)
        {
            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 = groupsh.Select(a => RepoFactory.AnimeGroup.GetByID(a))
                                                       .Where(a => a != null)
                                                       .ToList();

                    List <Video> arts = groupsList.Select(a => a.GetPlexContract(uid)).Where(contract =>
                    {
                        if (!(contract.Fanarts?.Any() ?? false))
                        {
                            return(false);
                        }
                        if (!(contract.Banners?.Any() ?? false))
                        {
                            return(false);
                        }
                        return(!string.IsNullOrEmpty(contract.Art));
                    }).ToList();

                    if (arts.Count > 0)
                    {
                        Random rand   = new Random();
                        Video  art    = arts[rand.Next(arts.Count)];
                        var    fanart = art.Fanarts[rand.Next(art.Fanarts.Count)];
                        filter.art.fanart.Add(new Art()
                        {
                            index = 0,
                            url   = APIHelper.ConstructImageLinkFromTypeAndId(ctx, fanart.ImageType, fanart.ImageID)
                        });
                        var banner = art.Banners[rand.Next(art.Banners.Count)];
                        filter.art.banner.Add(new Art()
                        {
                            index = 0,
                            url   = APIHelper.ConstructImageLinkFromTypeAndId(ctx, banner.ImageType, banner.ImageID)
                        });
                        filter.art.thumb.Add(new Art()
                        {
                            index = 0,
                            url   = APIHelper.ConstructImageLinkFromRest(ctx, art.Thumb)
                        });
                    }

                    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);
                            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    = APIHelper.ConstructFilterIdUrl(ctx, filter.id);

            return(filter);
        }
Exemplo n.º 18
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);
        }
Exemplo n.º 19
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(Repo.Instance.AnimeSeries.GetByID).ToList();
                        groupsList = seriesList.Select(a => a.AnimeGroupID).Distinct()
                                     .Select(Repo.Instance.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    = APIHelper.ConstructFilterIdUrl(ctx, filter.id);

            return(filter);
        }
Exemplo n.º 20
0
 /// <summary>
 /// Get the Sorting Criteria for the Group Filter. ORDER DOES MATTER
 /// </summary>
 /// <param name="gf"></param>
 /// <returns></returns>
 public static List <SortingCriteria> GetSortingCriteria(SVR_GroupFilter gf)
 {
     return(gf.SortCriteriaList.Select(a => new SortingCriteria(a)).ToList());
 }
Exemplo n.º 21
0
 /// <summary>
 /// Get the Conditions for the Group Filter to be calculated
 /// </summary>
 /// <param name="gf"></param>
 /// <returns></returns>
 public static FilterConditions GetConditions(SVR_GroupFilter gf)
 {
     return(new FilterConditions(gf));
 }
Exemplo n.º 22
0
 public FilterConditions(SVR_GroupFilter gf)
 {
     Conditions  = gf.Conditions.Select(a => new Condition(a)).ToList();
     InvertLogic = gf.BaseCondition == (int)GroupFilterBaseCondition.Exclude;
 }
Exemplo n.º 23
0
 private string OrderByName(SVR_GroupFilter group)
 {
     return(group.GroupFilterName);
 }
Exemplo n.º 24
0
        private void CreateInitialGroupFilters()
        {
            // group filters

            if (RepoFactory.GroupFilter.GetAll().Count() > 0)
            {
                return;
            }

            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(ServerSettings.Culture);

            // Favorites
            SVR_GroupFilter gf = new SVR_GroupFilter();

            gf.GroupFilterName = Shoko.Commons.Properties.Resources.Filter_Favorites;
            gf.ApplyToSeries   = 0;
            gf.BaseCondition   = 1;
            gf.Locked          = 0;
            gf.FilterType      = (int)GroupFilterType.UserDefined;
            GroupFilterCondition gfc = new GroupFilterCondition();

            gfc.ConditionType      = (int)GroupFilterConditionType.Favourite;
            gfc.ConditionOperator  = (int)GroupFilterOperator.Include;
            gfc.ConditionParameter = "";
            gf.Conditions.Add(gfc);
            gf.EvaluateAnimeGroups();
            gf.EvaluateAnimeSeries();
            RepoFactory.GroupFilter.Save(gf);

            // Missing Episodes
            gf = new SVR_GroupFilter();
            gf.GroupFilterName = Shoko.Commons.Properties.Resources.Filter_MissingEpisodes;
            gf.ApplyToSeries   = 0;
            gf.BaseCondition   = 1;
            gf.Locked          = 0;
            gf.FilterType      = (int)GroupFilterType.UserDefined;
            gfc = new GroupFilterCondition();
            gfc.ConditionType      = (int)GroupFilterConditionType.MissingEpisodesCollecting;
            gfc.ConditionOperator  = (int)GroupFilterOperator.Include;
            gfc.ConditionParameter = "";
            gf.Conditions.Add(gfc);
            gf.EvaluateAnimeGroups();
            gf.EvaluateAnimeSeries();
            RepoFactory.GroupFilter.Save(gf);


            // Newly Added Series
            gf = new SVR_GroupFilter();
            gf.GroupFilterName = Shoko.Commons.Properties.Resources.Filter_Added;
            gf.ApplyToSeries   = 0;
            gf.BaseCondition   = 1;
            gf.Locked          = 0;
            gf.FilterType      = (int)GroupFilterType.UserDefined;
            gfc = new GroupFilterCondition();
            gfc.ConditionType      = (int)GroupFilterConditionType.SeriesCreatedDate;
            gfc.ConditionOperator  = (int)GroupFilterOperator.LastXDays;
            gfc.ConditionParameter = "10";
            gf.Conditions.Add(gfc);
            gf.EvaluateAnimeGroups();
            gf.EvaluateAnimeSeries();
            RepoFactory.GroupFilter.Save(gf);

            // Newly Airing Series
            gf = new SVR_GroupFilter();
            gf.GroupFilterName = Shoko.Commons.Properties.Resources.Filter_Airing;
            gf.ApplyToSeries   = 0;
            gf.BaseCondition   = 1;
            gf.Locked          = 0;
            gf.FilterType      = (int)GroupFilterType.UserDefined;
            gfc = new GroupFilterCondition();
            gfc.ConditionType      = (int)GroupFilterConditionType.AirDate;
            gfc.ConditionOperator  = (int)GroupFilterOperator.LastXDays;
            gfc.ConditionParameter = "30";
            gf.Conditions.Add(gfc);
            gf.EvaluateAnimeGroups();
            gf.EvaluateAnimeSeries();
            RepoFactory.GroupFilter.Save(gf);

            // Votes Needed
            gf = new SVR_GroupFilter();
            gf.GroupFilterName = Shoko.Commons.Properties.Resources.Filter_Votes;
            gf.ApplyToSeries   = 1;
            gf.BaseCondition   = 1;
            gf.Locked          = 0;
            gf.FilterType      = (int)GroupFilterType.UserDefined;
            gfc = new GroupFilterCondition();
            gfc.ConditionType      = (int)GroupFilterConditionType.CompletedSeries;
            gfc.ConditionOperator  = (int)GroupFilterOperator.Include;
            gfc.ConditionParameter = "";
            gf.Conditions.Add(gfc);
            gfc = new GroupFilterCondition();
            gfc.ConditionType      = (int)GroupFilterConditionType.HasUnwatchedEpisodes;
            gfc.ConditionOperator  = (int)GroupFilterOperator.Exclude;
            gfc.ConditionParameter = "";
            gf.Conditions.Add(gfc);
            gfc = new GroupFilterCondition();
            gfc.ConditionType      = (int)GroupFilterConditionType.UserVotedAny;
            gfc.ConditionOperator  = (int)GroupFilterOperator.Exclude;
            gfc.ConditionParameter = "";
            gf.Conditions.Add(gfc);
            gf.EvaluateAnimeGroups();
            gf.EvaluateAnimeSeries();
            RepoFactory.GroupFilter.Save(gf);

            // Recently Watched
            gf = new SVR_GroupFilter();
            gf.GroupFilterName = Shoko.Commons.Properties.Resources.Filter_RecentlyWatched;
            gf.ApplyToSeries   = 0;
            gf.BaseCondition   = 1;
            gf.Locked          = 0;
            gf.FilterType      = (int)GroupFilterType.UserDefined;
            gfc = new GroupFilterCondition();
            gfc.ConditionType      = (int)GroupFilterConditionType.EpisodeWatchedDate;
            gfc.ConditionOperator  = (int)GroupFilterOperator.LastXDays;
            gfc.ConditionParameter = "10";
            gf.Conditions.Add(gfc);
            gf.EvaluateAnimeGroups();
            gf.EvaluateAnimeSeries();
            RepoFactory.GroupFilter.Save(gf);

            // TvDB/MovieDB Link Missing
            gf = new SVR_GroupFilter();
            gf.GroupFilterName = Shoko.Commons.Properties.Resources.Filter_LinkMissing;
            gf.ApplyToSeries   = 1; // This makes far more sense as applied to series
            gf.BaseCondition   = 1;
            gf.Locked          = 0;
            gf.FilterType      = (int)GroupFilterType.UserDefined;
            gfc = new GroupFilterCondition();
            gfc.ConditionType      = (int)GroupFilterConditionType.AssignedTvDBOrMovieDBInfo;
            gfc.ConditionOperator  = (int)GroupFilterOperator.Exclude;
            gfc.ConditionParameter = "";
            gf.Conditions.Add(gfc);
            gf.EvaluateAnimeGroups();
            gf.EvaluateAnimeSeries();
            RepoFactory.GroupFilter.Save(gf);
        }
        public static IEnumerable <SVR_AnimeGroup> GroupFilterSort(this IEnumerable <SVR_AnimeGroup> groups, SVR_GroupFilter gf)
        {
            bool isFirst = true;
            IEnumerable <SVR_AnimeGroup> query = groups;

            foreach (GroupFilterSortingCriteria gfsc in gf.SortCriteriaList)
            {
                query   = Order(query, gfsc, isFirst);
                isFirst = false;
            }
            return(query);
        }
Exemplo n.º 26
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);
        }
Exemplo n.º 27
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 = Repo.Instance.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, ref _)));

            if (_.Count > 0)
            {
                gfs = gfs.Concat(_.Select(tag => Repo.Instance.GroupFilter.GetAll().FirstOrDefault(a =>
                {
                    if (a.FilterType != (int)GroupFilterType.Tag)
                    {
                        return(false);
                    }
                    if (tag.Equals("Original Work"))
                    {
                        return(a.GroupFilterName.Equals("new"));
                    }

                    return(a.GroupFilterName.Equals(tag, StringComparison.InvariantCultureIgnoreCase));
                })).AsParallel());
            }

            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 = APIHelper.ConstructFilterIdUrl(ctx, f.id);

            return(f);
        }
Exemplo n.º 28
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);
        }
Exemplo n.º 29
0
        private void CreateInitialGroupFilters()
        {
            // group filters

            if (RepoFactory.GroupFilter.GetAll().Any())
            {
                return;
            }

            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(ServerSettings.Culture);

            // Favorites
            SVR_GroupFilter gf = new SVR_GroupFilter
            {
                GroupFilterName = Commons.Properties.Resources.Filter_Favorites,
                ApplyToSeries   = 0,
                BaseCondition   = 1,
                Locked          = 0,
                FilterType      = (int)GroupFilterType.UserDefined
            };
            GroupFilterCondition gfc = new GroupFilterCondition
            {
                ConditionType      = (int)GroupFilterConditionType.Favourite,
                ConditionOperator  = (int)GroupFilterOperator.Include,
                ConditionParameter = string.Empty
            };

            gf.Conditions.Add(gfc);
            gf.CalculateGroupsAndSeries();
            RepoFactory.GroupFilter.Save(gf);

            // Missing Episodes
            gf = new SVR_GroupFilter
            {
                GroupFilterName = Commons.Properties.Resources.Filter_MissingEpisodes,
                ApplyToSeries   = 0,
                BaseCondition   = 1,
                Locked          = 0,
                FilterType      = (int)GroupFilterType.UserDefined
            };
            gfc = new GroupFilterCondition
            {
                ConditionType      = (int)GroupFilterConditionType.MissingEpisodesCollecting,
                ConditionOperator  = (int)GroupFilterOperator.Include,
                ConditionParameter = string.Empty
            };
            gf.Conditions.Add(gfc);
            gf.CalculateGroupsAndSeries();
            RepoFactory.GroupFilter.Save(gf);


            // Newly Added Series
            gf = new SVR_GroupFilter
            {
                GroupFilterName = Commons.Properties.Resources.Filter_Added,
                ApplyToSeries   = 0,
                BaseCondition   = 1,
                Locked          = 0,
                FilterType      = (int)GroupFilterType.UserDefined
            };
            gfc = new GroupFilterCondition
            {
                ConditionType      = (int)GroupFilterConditionType.SeriesCreatedDate,
                ConditionOperator  = (int)GroupFilterOperator.LastXDays,
                ConditionParameter = "10"
            };
            gf.Conditions.Add(gfc);
            gf.CalculateGroupsAndSeries();
            RepoFactory.GroupFilter.Save(gf);

            // Newly Airing Series
            gf = new SVR_GroupFilter
            {
                GroupFilterName = Commons.Properties.Resources.Filter_Airing,
                ApplyToSeries   = 0,
                BaseCondition   = 1,
                Locked          = 0,
                FilterType      = (int)GroupFilterType.UserDefined
            };
            gfc = new GroupFilterCondition
            {
                ConditionType      = (int)GroupFilterConditionType.AirDate,
                ConditionOperator  = (int)GroupFilterOperator.LastXDays,
                ConditionParameter = "30"
            };
            gf.Conditions.Add(gfc);
            gf.CalculateGroupsAndSeries();
            RepoFactory.GroupFilter.Save(gf);

            // Votes Needed
            gf = new SVR_GroupFilter
            {
                GroupFilterName = Commons.Properties.Resources.Filter_Votes,
                ApplyToSeries   = 1,
                BaseCondition   = 1,
                Locked          = 0,
                FilterType      = (int)GroupFilterType.UserDefined
            };
            gfc = new GroupFilterCondition
            {
                ConditionType      = (int)GroupFilterConditionType.CompletedSeries,
                ConditionOperator  = (int)GroupFilterOperator.Include,
                ConditionParameter = string.Empty
            };
            gf.Conditions.Add(gfc);
            gfc = new GroupFilterCondition
            {
                ConditionType      = (int)GroupFilterConditionType.HasUnwatchedEpisodes,
                ConditionOperator  = (int)GroupFilterOperator.Exclude,
                ConditionParameter = string.Empty
            };
            gf.Conditions.Add(gfc);
            gfc = new GroupFilterCondition
            {
                ConditionType      = (int)GroupFilterConditionType.UserVotedAny,
                ConditionOperator  = (int)GroupFilterOperator.Exclude,
                ConditionParameter = string.Empty
            };
            gf.Conditions.Add(gfc);
            gf.CalculateGroupsAndSeries();
            RepoFactory.GroupFilter.Save(gf);

            // Recently Watched
            gf = new SVR_GroupFilter
            {
                GroupFilterName = Commons.Properties.Resources.Filter_RecentlyWatched,
                ApplyToSeries   = 0,
                BaseCondition   = 1,
                Locked          = 0,
                FilterType      = (int)GroupFilterType.UserDefined
            };
            gfc = new GroupFilterCondition
            {
                ConditionType      = (int)GroupFilterConditionType.EpisodeWatchedDate,
                ConditionOperator  = (int)GroupFilterOperator.LastXDays,
                ConditionParameter = "10"
            };
            gf.Conditions.Add(gfc);
            gf.CalculateGroupsAndSeries();
            RepoFactory.GroupFilter.Save(gf);

            // TvDB/MovieDB Link Missing
            gf = new SVR_GroupFilter
            {
                GroupFilterName = Commons.Properties.Resources.Filter_LinkMissing,
                ApplyToSeries   = 1, // This makes far more sense as applied to series
                BaseCondition   = 1,
                Locked          = 0,
                FilterType      = (int)GroupFilterType.UserDefined
            };
            gfc = new GroupFilterCondition
            {
                ConditionType      = (int)GroupFilterConditionType.AssignedTvDBOrMovieDBInfo,
                ConditionOperator  = (int)GroupFilterOperator.Exclude,
                ConditionParameter = string.Empty
            };
            gf.Conditions.Add(gfc);
            gf.CalculateGroupsAndSeries();
            RepoFactory.GroupFilter.Save(gf);
        }
Exemplo n.º 30
0
        internal static Filter GenerateFromGroupFilter(NancyContext ctx, SVR_GroupFilter gf, int uid, bool nocast, bool notag, int level,
                                                       bool all, bool allpic, int pic, byte 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 <CL_AniDB_Anime> arts;
                    if (gf.ApplyToSeries == 1 && 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();
                        arts = seriesList.Where(SeriesHasCompleteArt).Select(a => a?.GetAnime()?.Contract?.AniDBAnime)
                               .Where(a => a != null).ToList();
                        if (arts.Count == 0)
                        {
                            arts = seriesList.Where(SeriesHasMostlyCompleteArt)
                                   .Select(a => a?.GetAnime()?.Contract?.AniDBAnime).Where(a => a != null).ToList();
                        }
                        if (arts.Count == 0)
                        {
                            arts = seriesList.Select(a => a?.GetAnime()?.Contract?.AniDBAnime).Where(a => a != null)
                                   .ToList();
                        }
                    }
                    else
                    {
                        groupsList = groupsh.Select(a => RepoFactory.AnimeGroup.GetByID(a))
                                     .Where(a => a != null)
                                     .ToList();
                        arts = groupsList.Where(GroupHasCompleteArt).Select(GetAnimeContractFromGroup).ToList();
                        if (arts.Count == 0)
                        {
                            arts = groupsList.Where(GroupHasMostlyCompleteArt).Select(GetAnimeContractFromGroup)
                                   .ToList();
                        }
                        if (arts.Count == 0)
                        {
                            arts = groupsList.Where(a => (a.Anime?.Count ?? 0) > 0).Select(GetAnimeContractFromGroup)
                                   .ToList();
                        }
                    }

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

                        if (anime.Fanarts?.Count > 0)
                        {
                            var fanart = anime.Fanarts[rand.Next(anime.Fanarts.Count)];
                            filter.art.fanart.Add(new Art
                            {
                                index = 0,
                                url   = APIHelper.ConstructImageLinkFromTypeAndId(ctx, fanart.ImageType,
                                                                                  fanart.AniDB_Anime_DefaultImageID)
                            });
                        }

                        if (anime.Banners?.Count > 0)
                        {
                            var banner = anime.Banners[rand.Next(anime.Banners.Count)];
                            filter.art.banner.Add(new Art()
                            {
                                index = 0,
                                url   = APIHelper.ConstructImageLinkFromTypeAndId(ctx, banner.ImageType,
                                                                                  banner.AniDB_Anime_DefaultImageID)
                            });
                        }

                        filter.art.thumb.Add(new Art()
                        {
                            url = APIHelper.ConstructImageLinkFromTypeAndId(ctx, (int)ImageEntityType.AniDB_Cover,
                                                                            anime.AnimeID),
                            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    = APIHelper.ConstructFilterIdUrl(ctx, filter.id);

            return(filter);
        }