1
0
mirror of https://github.com/Radarr/Radarr.git synced 2026-04-22 22:15:17 -04:00

Added the TMDB Configuration service. This allows Image urls to be dynamically generated!

This commit is contained in:
Leonardo Galli
2017-01-05 10:49:55 +01:00
parent 87c7afac16
commit 1325822798
5 changed files with 118 additions and 4 deletions
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NzbDrone.Core.MediaCover;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Http;
using NzbDrone.Common.Cloud;
using NzbDrone.Core.MetadataSource.SkyHook.Resource;
namespace NzbDrone.Core.MetadataSource
{
public interface ITmdbConfigService
{
MediaCover.MediaCover GetCoverForURL(string url, MediaCover.MediaCoverTypes type);
}
class TmdbConfigService : ITmdbConfigService
{
private readonly ICached<ConfigResource> _configurationCache;
private readonly IHttpClient _httpClient;
private readonly IHttpRequestBuilderFactory _tmdbBuilder;
public TmdbConfigService(ICacheManager cacheManager, IHttpClient httpClient, ISonarrCloudRequestBuilder requestBuilder)
{
_configurationCache = cacheManager.GetCache<ConfigResource>(GetType(), "configuration_cache");
_httpClient = httpClient;
_tmdbBuilder = requestBuilder.TMDBSingle;
}
public MediaCover.MediaCover GetCoverForURL(string url, MediaCover.MediaCoverTypes type)
{
if (_configurationCache.Count == 0)
{
RefreshCache();
}
var images = _configurationCache.Find("configuration").images;
var cover = new MediaCover.MediaCover();
cover.CoverType = type;
var realUrl = images.base_url;
switch (type)
{
case MediaCoverTypes.Banner:
realUrl += images.backdrop_sizes.Last();
break;
case MediaCoverTypes.Poster:
realUrl += images.poster_sizes.Last();
break;
default:
realUrl += "original";
break;
}
realUrl += url;
cover.Url = realUrl;
return cover;
}
private void RefreshCache()
{
var request = _tmdbBuilder.Create().SetSegment("route", "configuration").Build();
var response = _httpClient.Get<ConfigResource>(request);
if (response.Resource.images != null)
{
_configurationCache.Set("configuration", response.Resource);
}
}
}
}