예제 #1
0
파일: Helper.cs 프로젝트: grb19/ShokoServer
        public static Video GenerateFromAnimeGroup(SVR_AnimeGroup grp, int userid, List <SVR_AnimeSeries> allSeries,
                                                   ISessionWrapper session = null)
        {
            CL_AnimeGroup_User cgrp = grp.GetUserContract(userid);
            int subgrpcnt           = grp.GetAllChildGroups().Count;

            if ((cgrp.Stat_SeriesCount == 1) && (subgrpcnt == 0))
            {
                SVR_AnimeSeries     ser    = ShokoServiceImplementation.GetSeriesForGroup(grp.AnimeGroupID, allSeries);
                CL_AnimeSeries_User cserie = ser?.GetUserContract(userid);
                if (cserie == null)
                {
                    return(null);
                }
                Video v = GenerateFromSeries(cserie, ser, ser.GetAnime(), userid, session);
                v.AirDate   = ser.AirDate;
                v.UpdatedAt = ser.LatestEpisodeAirDate.HasValue
                    ? ser.LatestEpisodeAirDate.Value.ToUnixTime()
                    : null;
                v.Group = cgrp;
                return(v);
            }
            else
            {
                SVR_AnimeSeries ser = grp.DefaultAnimeSeriesID.HasValue
                    ? allSeries.FirstOrDefault(a => a.AnimeSeriesID == grp.DefaultAnimeSeriesID.Value)
                    : allSeries.Find(a => a.AirDate != DateTime.MinValue);
                if (ser == null && allSeries.Count > 0)
                {
                    ser = allSeries[0];
                }
                CL_AnimeSeries_User cserie = ser?.GetUserContract(userid);
                Video v = FromGroup(cgrp, cserie, userid, subgrpcnt);
                v.Group     = cgrp;
                v.AirDate   = cgrp.Stat_AirDate_Min ?? DateTime.MinValue;
                v.UpdatedAt = cgrp.LatestEpisodeAirDate?.ToUnixTime();
                v.Rating    = (int)Math.Round((grp.AniDBRating / 100), 1);
                List <Tag> newTags = new List <Tag>();
                foreach (AniDB_Tag tag in grp.Tags)
                {
                    Tag      newTag   = new Tag();
                    TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
                    newTag.Value = textInfo.ToTitleCase(tag.TagName.Trim());
                    if (!newTags.Contains(newTag))
                    {
                        newTags.Add(newTag);
                    }
                }
                v.Genres = newTags;
                if (ser == null)
                {
                    return(v);
                }
                List <AnimeTitle> newTitles = ser.GetAnime()
                                              .GetTitles()
                                              .Select(title => new AnimeTitle
                {
                    Title    = title.Title,
                    Language = title.Language,
                    Type     = title.TitleType
                })
                                              .ToList();
                v.Titles = newTitles;

                v.Roles = new List <RoleTag>();

                //TODO Character implementation is limited in JMM, One Character, could have more than one Seiyuu
                if (ser.GetAnime()?.Contract?.AniDBAnime?.Characters != null)
                {
                    foreach (CL_AniDB_Character c in ser.GetAnime().Contract.AniDBAnime.Characters)
                    {
                        string       ch     = c?.CharName;
                        AniDB_Seiyuu seiyuu = c?.Seiyuu;
                        if (String.IsNullOrEmpty(ch))
                        {
                            continue;
                        }
                        RoleTag t = new RoleTag
                        {
                            Value = seiyuu?.SeiyuuName
                        };
                        if (seiyuu != null)
                        {
                            t.TagPicture = ConstructSeiyuuImage(null, seiyuu.AniDB_SeiyuuID);
                        }
                        t.Role            = ch;
                        t.RoleDescription = c?.CharDescription;
                        t.RolePicture     = ConstructCharacterImage(null, c.CharID);
                        v.Roles.Add(t);
                    }
                }
                if (cserie?.AniDBAnime?.AniDBAnime?.Fanarts != null)
                {
                    v.Fanarts = new List <Contract_ImageDetails>();
                    cserie?.AniDBAnime?.AniDBAnime?.Fanarts.ForEach(
                        a =>
                        v.Fanarts.Add(new Contract_ImageDetails
                    {
                        ImageID   = a.AniDB_Anime_DefaultImageID,
                        ImageType = a.ImageType
                    }));
                }
                if (cserie?.AniDBAnime?.AniDBAnime?.Banners == null)
                {
                    return(v);
                }
                v.Banners = new List <Contract_ImageDetails>();
                cserie?.AniDBAnime?.AniDBAnime?.Banners.ForEach(
                    a =>
                    v.Banners.Add(new Contract_ImageDetails
                {
                    ImageID   = a.AniDB_Anime_DefaultImageID,
                    ImageType = a.ImageType
                }));
                return(v);
            }
        }
예제 #2
0
        internal static Filter GenerateFromGroupFilter(NancyContext ctx, SVR_GroupFilter gf, int uid, bool nocast, bool notag, int level,
                                                       bool all)
        {
            List <Group> groups = new List <Group>();
            Filter       filter = new Filter();

            filter.name = gf.GroupFilterName;
            filter.id   = gf.GroupFilterID;
            filter.size = 0;

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

                    int rand_art_iteration = 0;

                    // Populate Random Art
                    while (rand_art_iteration < 3)
                    {
                        int            index    = new Random().Next(groupsh.Count);
                        SVR_AnimeGroup randGrp  = Repositories.RepoFactory.AnimeGroup.GetByID(groupsh.ToList()[index]);
                        Video          contract = randGrp?.GetPlexContract(uid);
                        if (contract != null)
                        {
                            Random rand = new Random();
                            Contract_ImageDetails art = new Contract_ImageDetails();
                            // contract.Fanarts can be null even if contract isn't
                            if (contract.Fanarts != null && contract.Fanarts.Count > 0)
                            {
                                art = contract.Fanarts[rand.Next(contract.Fanarts.Count)];
                                filter.art.fanart.Add(new Art()
                                {
                                    url   = APIHelper.ConstructImageLinkFromTypeAndId(ctx, art.ImageType, art.ImageID),
                                    index = 0
                                });
                                rand_art_iteration = 3;

                                // we only want banner if we have fanart, other way will desync images
                                if (contract.Banners != null && contract.Banners.Count > 0)
                                {
                                    art = contract.Banners[rand.Next(contract.Banners.Count)];
                                    filter.art.banner.Add(new Art()
                                    {
                                        url   = APIHelper.ConstructImageLinkFromTypeAndId(ctx, art.ImageType, art.ImageID),
                                        index = 0
                                    });
                                    if (!string.IsNullOrEmpty(contract.Thumb))
                                    {
                                        filter.art.thumb.Add(new Art()
                                        {
                                            url   = APIHelper.ConstructImageLinkFromRest(ctx, contract.Thumb),
                                            index = 0
                                        });
                                    }
                                }
                            }
                        }
                        rand_art_iteration++;
                    }

                    Dictionary <CL_AnimeGroup_User, Group> order = new Dictionary <CL_AnimeGroup_User, Group>();
                    if (level > 0)
                    {
                        foreach (int gp in groupsh)
                        {
                            SVR_AnimeGroup ag = Repositories.RepoFactory.AnimeGroup.GetByID(gp);
                            if (ag == null)
                            {
                                continue;
                            }
                            Group group =
                                Group.GenerateFromAnimeGroup(ctx, ag, uid, nocast, notag, (level - 1), all,
                                                             filter.id);
                            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);
        }