Beispiel #1
0
        private async Task <T> TryGetValue <T>(string key, string language, Func <Task <T> > resultFactory)
        {
            if (_cache.TryGetValue(key, out T cachedValue))
            {
                return(cachedValue);
            }

            await _cacheWriteLock.WaitAsync().ConfigureAwait(false);

            try
            {
                if (_cache.TryGetValue(key, out cachedValue))
                {
                    return(cachedValue);
                }

                _tvDbClient.AcceptedLanguage = TvdbUtils.NormalizeLanguage(language) ?? DefaultLanguage;
                var result = await resultFactory.Invoke().ConfigureAwait(false);

                _cache.Set(key, result, TimeSpan.FromHours(1));
                return(result);
            }
            finally
            {
                _cacheWriteLock.Release();
            }
        }
        private IEnumerable <RemoteImageInfo> GetImages(Image[] images, string preferredLanguage)
        {
            var list      = new List <RemoteImageInfo>();
            var languages = _tvDbClientManager.GetLanguagesAsync(CancellationToken.None).Result.Data;

            foreach (Image image in images)
            {
                var imageInfo = new RemoteImageInfo
                {
                    RatingType      = RatingType.Score,
                    CommunityRating = (double?)image.RatingsInfo.Average,
                    VoteCount       = image.RatingsInfo.Count,
                    Url             = TvdbUtils.BannerUrl + image.FileName,
                    ProviderName    = Name,
                    Language        = languages.FirstOrDefault(lang => lang.Id == image.LanguageId)?.Abbreviation,
                    ThumbnailUrl    = TvdbUtils.BannerUrl + image.Thumbnail
                };

                var resolution = image.Resolution.Split('x');
                if (resolution.Length == 2)
                {
                    imageInfo.Width  = Convert.ToInt32(resolution[0]);
                    imageInfo.Height = Convert.ToInt32(resolution[1]);
                }

                imageInfo.Type = TvdbUtils.GetImageTypeFromKeyType(image.KeyType);
                list.Add(imageInfo);
            }
            var isLanguageEn = string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase);

            return(list.OrderByDescending(i =>
            {
                if (string.Equals(preferredLanguage, i.Language, StringComparison.OrdinalIgnoreCase))
                {
                    return 3;
                }

                if (!isLanguageEn)
                {
                    if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
                    {
                        return 2;
                    }
                }

                if (string.IsNullOrEmpty(i.Language))
                {
                    return isLanguageEn ? 3 : 2;
                }

                return 0;
            })
                   .ThenByDescending(i => i.CommunityRating ?? 0)
                   .ThenByDescending(i => i.VoteCount ?? 0));
        }