Extract imagetype-to-keytype statements into a utility function and move tvdb specific utils to separate class

This commit is contained in:
Claus Vium 2019-02-13 11:21:56 +01:00
parent 1aaa8de1f9
commit b9efcace79
8 changed files with 60 additions and 65 deletions

View File

@ -7,26 +7,6 @@ namespace MediaBrowser.Controller.Library
/// </summary> /// </summary>
public static class TVUtils public static class TVUtils
{ {
/// <summary>
/// The TVDB API key
/// </summary>
public static readonly string TvdbApiKey = "OG4V3YJ3FAP7FP2K";
public static readonly string TvdbBaseUrl = "https://www.thetvdb.com/";
/// <summary>
/// The banner URL
/// </summary>
public static readonly string BannerUrl = TvdbBaseUrl + "banners/";
public static string NormalizeLanguage(string language)
{
if (string.IsNullOrWhiteSpace(language))
{
return null;
}
// pt-br is just pt to tvdb
return language.Split('-')[0].ToLowerInvariant();
}
/// <summary> /// <summary>
/// Gets the air days. /// Gets the air days.
/// </summary> /// </summary>

View File

@ -11,7 +11,6 @@ using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers; using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers; using MediaBrowser.Model.Providers;
using MediaBrowser.Providers.TV;
using MediaBrowser.Providers.TV.TheTVDB; using MediaBrowser.Providers.TV.TheTVDB;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using TvDbSharper; using TvDbSharper;
@ -89,7 +88,7 @@ namespace MediaBrowser.Providers.People
return new RemoteImageInfo return new RemoteImageInfo
{ {
Url = TVUtils.BannerUrl + actor.Image, Url = TvdbUtils.BannerUrl + actor.Image,
Type = ImageType.Primary, Type = ImageType.Primary,
ProviderName = Name ProviderName = Name
}; };

View File

@ -28,7 +28,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
private TvDbClientManager() private TvDbClientManager()
{ {
tvDbClient = new TvDbClient(); tvDbClient = new TvDbClient();
tvDbClient.Authentication.AuthenticateAsync(TVUtils.TvdbApiKey); tvDbClient.Authentication.AuthenticateAsync(TvdbUtils.TvdbApiKey);
tokenCreatedAt = DateTime.Now; tokenCreatedAt = DateTime.Now;
} }
@ -67,7 +67,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
} }
catch catch
{ {
tvDbClient.Authentication.AuthenticateAsync(TVUtils.TvdbApiKey); tvDbClient.Authentication.AuthenticateAsync(TvdbUtils.TvdbApiKey);
} }
tokenCreatedAt = DateTime.Now; tokenCreatedAt = DateTime.Now;
@ -222,7 +222,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
return cachedValue; return cachedValue;
} }
tvDbClient.AcceptedLanguage = TVUtils.NormalizeLanguage(language) ?? DefaultLanguage; tvDbClient.AcceptedLanguage = TvdbUtils.NormalizeLanguage(language) ?? DefaultLanguage;
var result = await resultFactory.Invoke(); var result = await resultFactory.Invoke();
_cache.Set(key, result, TimeSpan.FromHours(1)); _cache.Set(key, result, TimeSpan.FromHours(1));
return result; return result;

View File

@ -1,13 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Common.Net; using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers; using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers; using MediaBrowser.Model.Providers;
@ -105,7 +102,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
Width = Convert.ToInt32(episode.ThumbWidth), Width = Convert.ToInt32(episode.ThumbWidth),
Height = Convert.ToInt32(episode.ThumbHeight), Height = Convert.ToInt32(episode.ThumbHeight),
ProviderName = Name, ProviderName = Name,
Url = TVUtils.BannerUrl + episode.Filename, Url = TvdbUtils.BannerUrl + episode.Filename,
Type = ImageType.Primary Type = ImageType.Primary
}; };
} }

View File

@ -7,7 +7,6 @@ using System.Threading.Tasks;
using MediaBrowser.Common.Net; using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers; using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers; using MediaBrowser.Model.Providers;
@ -100,10 +99,10 @@ namespace MediaBrowser.Providers.TV.TheTVDB
RatingType = RatingType.Score, RatingType = RatingType.Score,
CommunityRating = (double?)image.RatingsInfo.Average, CommunityRating = (double?)image.RatingsInfo.Average,
VoteCount = image.RatingsInfo.Count, VoteCount = image.RatingsInfo.Count,
Url = TVUtils.BannerUrl + image.FileName, Url = TvdbUtils.BannerUrl + image.FileName,
ProviderName = ProviderName, ProviderName = ProviderName,
Language = languages.FirstOrDefault(lang => lang.Id == image.LanguageId)?.Abbreviation, Language = languages.FirstOrDefault(lang => lang.Id == image.LanguageId)?.Abbreviation,
ThumbnailUrl = TVUtils.BannerUrl + image.Thumbnail ThumbnailUrl = TvdbUtils.BannerUrl + image.Thumbnail
}; };
var resolution = image.Resolution.Split('x'); var resolution = image.Resolution.Split('x');
@ -113,19 +112,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
imageInfo.Height = Convert.ToInt32(resolution[1]); imageInfo.Height = Convert.ToInt32(resolution[1]);
} }
if (string.Equals(image.KeyType, "season", StringComparison.OrdinalIgnoreCase)) imageInfo.Type = TvdbUtils.GetImageTypeFromKeyType(image.KeyType);
{
imageInfo.Type = ImageType.Primary;
}
else if (string.Equals(image.KeyType, "seasonwide", StringComparison.OrdinalIgnoreCase))
{
imageInfo.Type = ImageType.Banner;
}
else if (string.Equals(image.KeyType, "fanart", StringComparison.OrdinalIgnoreCase))
{
imageInfo.Type = ImageType.Backdrop;
}
list.Add(imageInfo); list.Add(imageInfo);
} }
var isLanguageEn = string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase); var isLanguageEn = string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase);

View File

@ -1,12 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Common.Net; using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers; using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers; using MediaBrowser.Model.Providers;
@ -95,10 +93,10 @@ namespace MediaBrowser.Providers.TV.TheTVDB
RatingType = RatingType.Score, RatingType = RatingType.Score,
CommunityRating = (double?)image.RatingsInfo.Average, CommunityRating = (double?)image.RatingsInfo.Average,
VoteCount = image.RatingsInfo.Count, VoteCount = image.RatingsInfo.Count,
Url = TVUtils.BannerUrl + image.FileName, Url = TvdbUtils.BannerUrl + image.FileName,
ProviderName = Name, ProviderName = Name,
Language = languages.FirstOrDefault(lang => lang.Id == image.LanguageId)?.Abbreviation, Language = languages.FirstOrDefault(lang => lang.Id == image.LanguageId)?.Abbreviation,
ThumbnailUrl = TVUtils.BannerUrl + image.Thumbnail ThumbnailUrl = TvdbUtils.BannerUrl + image.Thumbnail
}; };
var resolution = image.Resolution.Split('x'); var resolution = image.Resolution.Split('x');
@ -108,20 +106,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
imageInfo.Height = Convert.ToInt32(resolution[1]); imageInfo.Height = Convert.ToInt32(resolution[1]);
} }
imageInfo.Type = TvdbUtils.GetImageTypeFromKeyType(image.KeyType);
if (string.Equals(image.KeyType, "poster", StringComparison.OrdinalIgnoreCase))
{
imageInfo.Type = ImageType.Primary;
}
else if (string.Equals(image.KeyType, "series", StringComparison.OrdinalIgnoreCase))
{
imageInfo.Type = ImageType.Banner;
}
else if (string.Equals(image.KeyType, "fanart", StringComparison.OrdinalIgnoreCase))
{
imageInfo.Type = ImageType.Backdrop;
}
list.Add(imageInfo); list.Add(imageInfo);
} }
var isLanguageEn = string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase); var isLanguageEn = string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase);

View File

@ -232,7 +232,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
Name = tvdbTitles.FirstOrDefault(), Name = tvdbTitles.FirstOrDefault(),
ProductionYear = firstAired.Year, ProductionYear = firstAired.Year,
SearchProviderName = Name, SearchProviderName = Name,
ImageUrl = TVUtils.BannerUrl + seriesSearchResult.Banner ImageUrl = TvdbUtils.BannerUrl + seriesSearchResult.Banner
}; };
try try
@ -383,7 +383,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
Type = PersonType.Actor, Type = PersonType.Actor,
Name = (actor.Name ?? string.Empty).Trim(), Name = (actor.Name ?? string.Empty).Trim(),
Role = actor.Role, Role = actor.Role,
ImageUrl = TVUtils.BannerUrl + actor.Image, ImageUrl = TvdbUtils.BannerUrl + actor.Image,
SortOrder = actor.SortOrder SortOrder = actor.SortOrder
}; };

View File

@ -0,0 +1,47 @@
using System;
using System.ComponentModel;
using MediaBrowser.Model.Entities;
using TvDbSharper.Dto;
namespace MediaBrowser.Providers.TV.TheTVDB
{
/// <summary>
/// Class TVUtils
/// </summary>
public static class TvdbUtils
{
/// <summary>
/// The TVDB API key
/// </summary>
public static readonly string TvdbApiKey = "OG4V3YJ3FAP7FP2K";
public static readonly string TvdbBaseUrl = "https://www.thetvdb.com/";
/// <summary>
/// The banner URL
/// </summary>
public static readonly string BannerUrl = TvdbBaseUrl + "banners/";
public static ImageType GetImageTypeFromKeyType(string keyType)
{
switch (keyType.ToLowerInvariant())
{
case "poster":
case "season": return ImageType.Primary;
case "series":
case "seasonwide": return ImageType.Banner;
case "fanart": return ImageType.Backdrop;
default: throw new ArgumentException($"Invalid or unknown keytype: {keyType}", nameof(keyType));
}
}
public static string NormalizeLanguage(string language)
{
if (string.IsNullOrWhiteSpace(language))
{
return null;
}
// pt-br is just pt to tvdb
return language.Split('-')[0].ToLowerInvariant();
}
}
}