public Group(HttpContext ctx, SVR_AnimeGroup group) { int uid = ctx.GetUser()?.JMMUserID ?? 0; var allSeries = group.GetAllSeries(skipSorting: true); List <SVR_AnimeEpisode> ael = allSeries.SelectMany(a => a.GetAnimeEpisodes()).ToList(); IDs = new GroupIDs { ID = group.AnimeGroupID }; if (group.DefaultAnimeSeriesID != null) { IDs.DefaultSeries = group.DefaultAnimeSeriesID.Value; } if (group.AnimeGroupParentID.HasValue) { IDs.ParentGroup = group.AnimeGroupParentID.Value; } IDs.TopLevelGroup = group.TopLevelAnimeGroup.AnimeGroupID; Name = group.GroupName; SortName = group.SortName; Description = group.Description; Sizes = ModelHelper.GenerateSizes(ael, uid); Size = group.GetSeries().Count; HasCustomName = GetHasCustomName(group); }
public void UpdatePlexKodiContracts(ISessionWrapper session = null) { SVR_AnimeGroup grp = RepoFactory.AnimeGroup.GetByID(AnimeGroupID); if (grp == null) { return; } List <SVR_AnimeSeries> series = grp.GetAllSeries(); PlexContract = Helper.GenerateFromAnimeGroup(grp, JMMUserID, series, session); }
public void UpdatePlexKodiContracts_RA() { SVR_AnimeGroup grp = Repo.Instance.AnimeGroup.GetByID(AnimeGroupID); if (grp == null) { return; } List <SVR_AnimeSeries> series = grp.GetAllSeries(); PlexContract = Helper.GenerateFromAnimeGroup(grp, JMMUserID, series); }
public void RecalculateStatsContractsForGroup(SVR_AnimeGroup group) { using (ISession sessionNotWrapped = DatabaseFactory.SessionFactory.OpenSession()) { var groups = new List <SVR_AnimeGroup> { group }; var session = sessionNotWrapped.Wrap(); var series = group.GetAllSeries(true); // recalculate series _log.Info($"Recalculating Series Stats and Contracts for Group: {group.GroupName} ({group.AnimeGroupID})"); using (ITransaction trans = session.BeginTransaction()) { UpdateAnimeSeriesContractsAndSave(session, series); trans.Commit(); } // Update Cache so that group can recalculate series.ForEach(a => _animeSeriesRepo.Cache.Update(a)); // Recalculate group _log.Info($"Recalculating Group Stats and Contracts for Group: {group.GroupName} ({group.AnimeGroupID})"); using (ITransaction trans = session.BeginTransaction()) { UpdateAnimeGroupsAndTheirContracts(session, groups); trans.Commit(); } // update cache _animeGroupRepo.Cache.Update(group); var groupsUsers = _animeGroupUserRepo.GetByGroupID(group.AnimeGroupID); groupsUsers.ForEach(a => _animeGroupUserRepo.Cache.Update(a)); // update filters _log.Info($"Recalculating Filters for Group: {group.GroupName} ({group.AnimeGroupID})"); UpdateGroupFilters(session); _log.Info($"Done Recalculating Stats and Contracts for Group: {group.GroupName} ({group.AnimeGroupID})"); } }
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); }
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); }
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); }