Example #1
0
        private async Task <EpisodeInfo> CreateEpisodeInfo(Episode episode, string episodeId = null)
        {
            if (episode == null)
            {
                return(null);
            }
            if (string.IsNullOrEmpty(episodeId))
            {
                episodeId = episode.IDs.ID.ToString();
            }
            var         cacheKey = $"episode:{episodeId}";
            EpisodeInfo info     = null;

            if (DataCache.TryGetValue <EpisodeInfo>(cacheKey, out info))
            {
                return(info);
            }
            var aniDB = (await ShokoAPI.GetEpisodeAniDb(episodeId));

            info = new EpisodeInfo
            {
                Id        = episodeId,
                ExtraType = GetExtraType(aniDB),
                Shoko     = (await ShokoAPI.GetEpisode(episodeId)),
                AniDB     = aniDB,
                TvDB      = ((await ShokoAPI.GetEpisodeTvDb(episodeId))?.FirstOrDefault()),
            };
            DataCache.Set <EpisodeInfo>(cacheKey, info, DefaultTimeSpan);
            return(info);
        }
Example #2
0
        public async Task <FileInfo> GetFileInfo(string fileId)
        {
            var file = await ShokoAPI.GetFile(fileId);

            if (file == null)
            {
                return(null);
            }
            return(CreateFileInfo(file));
        }
Example #3
0
        public async Task <(FileInfo, EpisodeInfo, SeriesInfo, GroupInfo)> GetFileInfoByPath(string path, Ordering.GroupFilterType?filterGroupByType)
        {
            var partialPath = StripMediaFolder(path);
            var result      = await ShokoAPI.GetFileByPath(partialPath);

            var file = result?.FirstOrDefault();

            if (file == null)
            {
                return(null, null, null, null);
            }

            var series    = file?.SeriesIDs.FirstOrDefault();
            var seriesId  = series?.SeriesID.ID.ToString();
            var episodes  = series?.EpisodeIDs?.FirstOrDefault();
            var episodeId = episodes?.ID.ToString();

            if (string.IsNullOrEmpty(seriesId) || string.IsNullOrEmpty(episodeId))
            {
                return(null, null, null, null);
            }

            GroupInfo groupInfo = null;

            if (filterGroupByType != null)
            {
                groupInfo = await GetGroupInfoForSeries(seriesId, (Ordering.GroupFilterType) filterGroupByType);

                if (groupInfo == null)
                {
                    return(null, null, null, null);
                }
            }

            var seriesInfo = await GetSeriesInfo(seriesId);

            if (seriesInfo == null)
            {
                return(null, null, null, null);
            }

            var episodeInfo = await GetEpisodeInfo(episodeId);

            if (episodeInfo == null)
            {
                return(null, null, null, null);
            }

            var fileInfo = CreateFileInfo(file, file.ID.ToString(), series?.EpisodeIDs?.Count ?? 0);

            return(fileInfo, episodeInfo, seriesInfo, groupInfo);
        }
Example #4
0
        public async Task <SeriesInfo> GetSeriesInfo(string seriesId)
        {
            if (string.IsNullOrEmpty(seriesId))
            {
                return(null);
            }
            if (DataCache.TryGetValue <SeriesInfo>($"series:{seriesId}", out var info))
            {
                return(info);
            }
            var series = await ShokoAPI.GetSeries(seriesId);

            return(await CreateSeriesInfo(series, seriesId));
        }
Example #5
0
        public SeriesInfo GetSeriesInfoSync(string seriesId)
        {
            if (string.IsNullOrEmpty(seriesId))
            {
                return(null);
            }
            if (DataCache.TryGetValue <SeriesInfo>($"series:{seriesId}", out var info))
            {
                return(info);
            }
            var series = ShokoAPI.GetSeries(seriesId).GetAwaiter().GetResult();

            return(CreateSeriesInfo(series, seriesId).GetAwaiter().GetResult());
        }
Example #6
0
        public async Task <EpisodeInfo> GetEpisodeInfo(string episodeId)
        {
            if (string.IsNullOrEmpty(episodeId))
            {
                return(null);
            }
            if (DataCache.TryGetValue <EpisodeInfo>($"episode:{episodeId}", out var info))
            {
                return(info);
            }
            var episode = await ShokoAPI.GetEpisode(episodeId);

            return(await CreateEpisodeInfo(episode, episodeId));
        }
Example #7
0
        public async Task <GroupInfo> GetGroupInfo(string groupId, Ordering.GroupFilterType filterByType = Ordering.GroupFilterType.Default)
        {
            if (string.IsNullOrEmpty(groupId))
            {
                return(null);
            }

            if (DataCache.TryGetValue <GroupInfo>($"group:{filterByType}:{groupId}", out var info))
            {
                return(info);
            }

            var group = await ShokoAPI.GetGroup(groupId);

            return(await CreateGroupInfo(group, groupId, filterByType));
        }
Example #8
0
        public async Task <IEnumerable <PersonInfo> > GetPeople(string seriesId)
        {
            var list  = new List <PersonInfo>();
            var roles = await ShokoAPI.GetSeriesCast(seriesId);

            foreach (var role in roles)
            {
                list.Add(new PersonInfo
                {
                    Type     = PersonType.Actor,
                    Name     = role.Staff.Name,
                    Role     = role.Character.Name,
                    ImageUrl = role.Staff.Image?.ToURLString(),
                });
            }
            return(list);
        }
Example #9
0
        public async Task <GroupInfo> GetGroupInfoForSeries(string seriesId, Ordering.GroupFilterType filterByType = Ordering.GroupFilterType.Default)
        {
            string groupId;

            if (SeriesIdToGroupIdDictionary.ContainsKey(seriesId))
            {
                groupId = SeriesIdToGroupIdDictionary[seriesId];
            }
            else
            {
                var group = await ShokoAPI.GetGroupFromSeries(seriesId);

                if (group == null)
                {
                    return(null);
                }
                groupId = group.IDs.ID.ToString();
            }

            return(await GetGroupInfo(groupId, filterByType));
        }
Example #10
0
        public async Task <GroupInfo> GetGroupInfoByPath(string path, Ordering.GroupFilterType filterByType = Ordering.GroupFilterType.Default)
        {
            var partialPath = StripMediaFolder(path);
            var result      = await ShokoAPI.GetSeriesPathEndsWith(partialPath);

            var seriesId = result?.FirstOrDefault()?.IDs?.ID.ToString();

            if (string.IsNullOrEmpty(seriesId))
            {
                return(null);
            }

            var groupInfo = await GetGroupInfoForSeries(seriesId, filterByType);

            if (groupInfo == null)
            {
                return(null);
            }

            return(groupInfo);
        }
Example #11
0
        private async Task <SeriesInfo> CreateSeriesInfo(Series series, string seriesId = null)
        {
            if (series == null)
            {
                return(null);
            }

            if (string.IsNullOrEmpty(seriesId))
            {
                seriesId = series.IDs.ID.ToString();
            }

            SeriesInfo info     = null;
            var        cacheKey = $"series:{seriesId}";

            if (DataCache.TryGetValue <SeriesInfo>(cacheKey, out info))
            {
                return(info);
            }

            var seriesGuid = GetSeriesGuid(seriesId);
            var aniDb      = await ShokoAPI.GetSeriesAniDb(seriesId);

            var episodeList = await ShokoAPI.GetEpisodesFromSeries(seriesId)
                              .ContinueWith(async task => await Task.WhenAll(task.Result.Select(e => CreateEpisodeInfo(e)))).Unwrap()
                              .ContinueWith(l => l.Result.Where(s => s != null).ToList());

            var filteredSpecialEpisodesList = episodeList.Where(e => e.AniDB.Type == EpisodeType.Special && e.ExtraType == null).ToList();

            info = new SeriesInfo {
                Id          = seriesId,
                Guid        = seriesGuid,
                Shoko       = series,
                AniDB       = aniDb,
                EpisodeList = episodeList,
                FilteredSpecialEpisodesList = filteredSpecialEpisodesList,
            };
            DataCache.Set <SeriesInfo>(cacheKey, info, DefaultTimeSpan);
            return(info);
        }
Example #12
0
        public async Task <SeriesInfo> GetSeriesInfoByPath(string path)
        {
            var    partialPath = StripMediaFolder(path);
            string seriesId;

            if (SeriesPathToIdDictionary.ContainsKey(path))
            {
                seriesId = SeriesPathToIdDictionary[path];
            }
            else
            {
                var result = await ShokoAPI.GetSeriesPathEndsWith(partialPath);

                seriesId = result?.FirstOrDefault()?.IDs?.ID.ToString();

                SeriesPathToIdDictionary[path] = seriesId;
                if (!string.IsNullOrEmpty(seriesId))
                {
                    SeriesIdToPathDictionary[seriesId] = path;
                }
            }

            if (string.IsNullOrEmpty(seriesId))
            {
                return(null);
            }

            if (DataCache.TryGetValue <SeriesInfo>($"series:{seriesId}", out var info))
            {
                return(info);
            }

            var series = await ShokoAPI.GetSeries(seriesId);

            return(await CreateSeriesInfo(series, seriesId));
        }
Example #13
0
        private async Task <GroupInfo> CreateGroupInfo(Group group, string groupId, Ordering.GroupFilterType filterByType)
        {
            if (group == null)
            {
                return(null);
            }

            if (string.IsNullOrEmpty(groupId))
            {
                groupId = group.IDs.ID.ToString();
            }

            var       cacheKey  = $"group:{filterByType}:{groupId}";
            GroupInfo groupInfo = null;

            if (DataCache.TryGetValue <GroupInfo>(cacheKey, out groupInfo))
            {
                return(groupInfo);
            }

            var seriesList = await ShokoAPI.GetSeriesInGroup(groupId)
                             .ContinueWith(async task => await Task.WhenAll(task.Result.Select(s => CreateSeriesInfo(s)))).Unwrap()
                             .ContinueWith(l => l.Result.Where(s => s != null).ToList());

            if (seriesList != null && seriesList.Count > 0)
            {
                switch (filterByType)
                {
                default:
                    break;

                case Ordering.GroupFilterType.Movies:
                    seriesList = seriesList.Where(s => s.AniDB.Type == SeriesType.Movie).ToList();
                    break;

                case Ordering.GroupFilterType.Others:
                    seriesList = seriesList.Where(s => s.AniDB.Type != SeriesType.Movie).ToList();
                    break;
                }
            }

            // Return ealty if no series matched the filter or if the list was empty.
            if (seriesList == null || seriesList.Count == 0)
            {
                return(null);
            }

            // Order series list
            var orderingType = filterByType == Ordering.GroupFilterType.Movies ? Plugin.Instance.Configuration.MovieOrdering : Plugin.Instance.Configuration.SeasonOrdering;

            switch (orderingType)
            {
            case Ordering.OrderType.Default:
                break;

            case Ordering.OrderType.ReleaseDate:
                seriesList = seriesList.OrderBy(s => s?.AniDB?.AirDate ?? System.DateTime.MaxValue).ToList();
                break;

            // Should not be selectable unless a user fiddles with DevTools in the browser to select the option.
            case Ordering.OrderType.Chronological:
                throw new System.Exception("Not implemented yet");
            }

            // Select the targeted id if a group spesify a default series.
            int foundIndex = -1;
            int targetId   = (group.IDs.DefaultSeries ?? 0);

            if (targetId != 0)
            {
                foundIndex = seriesList.FindIndex(s => s.Shoko.IDs.ID == targetId);
            }
            // Else select the default series as first-to-be-released.
            else
            {
                switch (orderingType)
                {
                // The list is already sorted by release date, so just return the first index.
                case Ordering.OrderType.ReleaseDate:
                    foundIndex = 0;
                    break;

                // We don't know how Shoko may have sorted it, so just find the earliest series
                case Ordering.OrderType.Default:
                // We can't be sure that the the series in the list was _released_ chronologically, so find the earliest series, and use that as a base.
                case Ordering.OrderType.Chronological: {
                    var earliestSeries = seriesList.Aggregate((cur, nxt) => (cur == null || (nxt?.AniDB.AirDate ?? System.DateTime.MaxValue) < (cur.AniDB.AirDate ?? System.DateTime.MaxValue)) ? nxt : cur);
                    foundIndex = seriesList.FindIndex(s => s == earliestSeries);
                    break;
                }
                }
            }

            // Throw if we can't get a base point for seasons.
            if (foundIndex == -1)
            {
                throw new System.Exception("Unable to get a base-point for seasions withing the group");
            }

            var groupGuid = GetGroupGuid(groupId);

            groupInfo = new GroupInfo {
                Id                 = groupId,
                Guid               = groupGuid,
                Shoko              = group,
                SeriesList         = seriesList,
                DefaultSeries      = seriesList[foundIndex],
                DefaultSeriesIndex = foundIndex,
            };
            foreach (var series in seriesList)
            {
                SeriesIdToGroupIdDictionary[series.Id] = groupId;
            }
            DataCache.Set <GroupInfo>(cacheKey, groupInfo, DefaultTimeSpan);
            return(groupInfo);
        }
Example #14
0
 public async Task <string[]> GetTags(string seriesId)
 {
     return((await ShokoAPI.GetSeriesTags(seriesId, GetTagFilter()))?.Select(tag => tag.Name).ToArray() ?? new string[0]);
 }