Merge pull request #776 from cvium/update_tvdb

Update tvdb provider to v2 api
pull/981/head
Vasily 6 years ago committed by GitHub
commit e7e7d96f51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -102,6 +102,7 @@ using MediaBrowser.Model.Xml;
using MediaBrowser.Providers.Chapters;
using MediaBrowser.Providers.Manager;
using MediaBrowser.Providers.Subtitles;
using MediaBrowser.Providers.TV.TheTVDB;
using MediaBrowser.WebDashboard.Api;
using MediaBrowser.XbmcMetadata.Providers;
using Microsoft.Extensions.Configuration;
@ -623,12 +624,13 @@ namespace Emby.Server.Implementations
/// </summary>
protected async Task RegisterResources(IServiceCollection serviceCollection)
{
serviceCollection.AddMemoryCache();
serviceCollection.AddSingleton(ConfigurationManager);
serviceCollection.AddSingleton<IApplicationHost>(this);
serviceCollection.AddSingleton<IApplicationPaths>(ApplicationPaths);
serviceCollection.AddSingleton(JsonSerializer);
serviceCollection.AddSingleton(LoggerFactory);
@ -638,6 +640,7 @@ namespace Emby.Server.Implementations
serviceCollection.AddSingleton(EnvironmentInfo);
serviceCollection.AddSingleton(FileSystemManager);
serviceCollection.AddSingleton<TvDbClientManager>();
HttpClient = CreateHttpClient();
serviceCollection.AddSingleton(HttpClient);

@ -7,16 +7,6 @@ namespace MediaBrowser.Controller.Library
/// </summary>
public static class TVUtils
{
/// <summary>
/// The TVDB API key
/// </summary>
public static readonly string TvdbApiKey = "72930AE1CB7E2DB3";
public static readonly string TvdbBaseUrl = "https://www.thetvdb.com/";
/// <summary>
/// The banner URL
/// </summary>
public static readonly string BannerUrl = TvdbBaseUrl + "banners/";
/// <summary>
/// Gets the air days.
/// </summary>
@ -28,24 +18,24 @@ namespace MediaBrowser.Controller.Library
{
if (string.Equals(day, "Daily", StringComparison.OrdinalIgnoreCase))
{
return new DayOfWeek[]
{
DayOfWeek.Sunday,
DayOfWeek.Monday,
DayOfWeek.Tuesday,
DayOfWeek.Wednesday,
DayOfWeek.Thursday,
DayOfWeek.Friday,
DayOfWeek.Saturday
};
return new[]
{
DayOfWeek.Sunday,
DayOfWeek.Monday,
DayOfWeek.Tuesday,
DayOfWeek.Wednesday,
DayOfWeek.Thursday,
DayOfWeek.Friday,
DayOfWeek.Saturday
};
}
if (Enum.TryParse(day, true, out DayOfWeek value))
{
return new DayOfWeek[]
{
value
};
return new[]
{
value
};
}
return new DayOfWeek[] { };

@ -11,8 +11,10 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.2.0" />
<PackageReference Include="OptimizedPriorityQueue" Version="4.2.0" />
<PackageReference Include="PlaylistsNET" Version="1.0.2" />
<PackageReference Include="TvDbSharper" Version="2.0.0" />
</ItemGroup>
<PropertyGroup>

@ -1,42 +1,35 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Providers;
using MediaBrowser.Model.Xml;
using MediaBrowser.Providers.TV;
using MediaBrowser.Providers.TV.TheTVDB;
using Microsoft.Extensions.Logging;
using TvDbSharper;
namespace MediaBrowser.Providers.People
{
public class TvdbPersonImageProvider : IRemoteImageProvider, IHasOrder
{
private readonly IServerConfigurationManager _config;
private readonly ILibraryManager _libraryManager;
private readonly IHttpClient _httpClient;
private readonly IFileSystem _fileSystem;
private readonly IXmlReaderSettingsFactory _xmlSettings;
private readonly ILogger _logger;
private readonly ILibraryManager _libraryManager;
private readonly TvDbClientManager _tvDbClientManager;
public TvdbPersonImageProvider(IServerConfigurationManager config, ILibraryManager libraryManager, IHttpClient httpClient, IFileSystem fileSystem, IXmlReaderSettingsFactory xmlSettings)
public TvdbPersonImageProvider(ILibraryManager libraryManager, IHttpClient httpClient, ILogger<TvdbPersonImageProvider> logger, TvDbClientManager tvDbClientManager)
{
_config = config;
_libraryManager = libraryManager;
_httpClient = httpClient;
_fileSystem = fileSystem;
_xmlSettings = xmlSettings;
_logger = logger;
_tvDbClientManager = tvDbClientManager;
}
public string Name => ProviderName;
@ -56,7 +49,7 @@ namespace MediaBrowser.Providers.People
};
}
public Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
{
var seriesWithPerson = _libraryManager.GetItemList(new InternalItemsQuery
{
@ -71,152 +64,44 @@ namespace MediaBrowser.Providers.People
.Where(i => TvdbSeriesProvider.IsValidSeries(i.ProviderIds))
.ToList();
var infos = seriesWithPerson.Select(i => GetImageFromSeriesData(i, item.Name, cancellationToken))
var infos = (await Task.WhenAll(seriesWithPerson.Select(async i =>
await GetImageFromSeriesData(i, item.Name, cancellationToken).ConfigureAwait(false)))
.ConfigureAwait(false))
.Where(i => i != null)
.Take(1);
return Task.FromResult(infos);
return infos;
}
private RemoteImageInfo GetImageFromSeriesData(Series series, string personName, CancellationToken cancellationToken)
private async Task<RemoteImageInfo> GetImageFromSeriesData(Series series, string personName, CancellationToken cancellationToken)
{
var tvdbPath = TvdbSeriesProvider.GetSeriesDataPath(_config.ApplicationPaths, series.ProviderIds);
var actorXmlPath = Path.Combine(tvdbPath, "actors.xml");
var tvdbId = Convert.ToInt32(series.GetProviderId(MetadataProviders.Tvdb));
try
{
return GetImageInfo(actorXmlPath, personName, cancellationToken);
}
catch (FileNotFoundException)
{
return null;
}
catch (IOException)
{
return null;
}
}
private RemoteImageInfo GetImageInfo(string xmlFile, string personName, CancellationToken cancellationToken)
{
var settings = _xmlSettings.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
using (var fileStream = _fileSystem.GetFileStream(xmlFile, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
{
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
{
// Use XmlReader for best performance
using (var reader = XmlReader.Create(streamReader, settings))
{
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
{
cancellationToken.ThrowIfCancellationRequested();
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "Actor":
{
if (reader.IsEmptyElement)
{
reader.Read();
continue;
}
using (var subtree = reader.ReadSubtree())
{
var info = FetchImageInfoFromActorNode(personName, subtree);
if (info != null)
{
return info;
}
}
break;
}
default:
reader.Skip();
break;
}
}
else
{
reader.Read();
}
}
}
}
}
return null;
}
/// <summary>
/// Fetches the data from actor node.
/// </summary>
/// <param name="personName">Name of the person.</param>
/// <param name="reader">The reader.</param>
/// <returns>System.String.</returns>
private RemoteImageInfo FetchImageInfoFromActorNode(string personName, XmlReader reader)
{
string name = null;
string image = null;
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "Name":
{
name = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
break;
}
case "Image":
{
image = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
break;
}
default:
reader.Skip();
break;
}
}
else
var actorsResult = await _tvDbClientManager
.GetActorsAsync(tvdbId, series.GetPreferredMetadataLanguage(), cancellationToken)
.ConfigureAwait(false);
var actor = actorsResult.Data.FirstOrDefault(a =>
string.Equals(a.Name, personName, StringComparison.OrdinalIgnoreCase) &&
!string.IsNullOrEmpty(a.Image));
if (actor == null)
{
reader.Read();
return null;
}
}
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(image) &&
string.Equals(name, personName, StringComparison.OrdinalIgnoreCase))
{
return new RemoteImageInfo
{
Url = TVUtils.BannerUrl + image,
Url = TvdbUtils.BannerUrl + actor.Image,
Type = ImageType.Primary,
ProviderName = Name
};
}
return null;
catch (TvDbServerException e)
{
_logger.LogError(e, "Failed to retrieve actor {ActorName} from series {SeriesTvdbId}", personName, tvdbId);
return null;
}
}
public int Order => 1;

@ -15,7 +15,6 @@ using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Xml;
using MediaBrowser.Providers.TV.TheTVDB;
using Microsoft.Extensions.Logging;
@ -28,77 +27,58 @@ namespace MediaBrowser.Providers.TV
private readonly ILibraryManager _libraryManager;
private readonly ILocalizationManager _localization;
private readonly IFileSystem _fileSystem;
private readonly TvDbClientManager _tvDbClientManager;
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private readonly IXmlReaderSettingsFactory _xmlSettings;
private const double UnairedEpisodeThresholdDays = 2;
public MissingEpisodeProvider(ILogger logger, IServerConfigurationManager config, ILibraryManager libraryManager, ILocalizationManager localization, IFileSystem fileSystem, IXmlReaderSettingsFactory xmlSettings)
public MissingEpisodeProvider(
ILogger logger,
IServerConfigurationManager config,
ILibraryManager libraryManager,
ILocalizationManager localization,
IFileSystem fileSystem,
TvDbClientManager tvDbClientManager)
{
_logger = logger;
_config = config;
_libraryManager = libraryManager;
_localization = localization;
_fileSystem = fileSystem;
_xmlSettings = xmlSettings;
_tvDbClientManager = tvDbClientManager;
}
public async Task<bool> Run(Series series, bool addNewItems, CancellationToken cancellationToken)
{
var tvdbId = series.GetProviderId(MetadataProviders.Tvdb);
// Todo: Support series by imdb id
var seriesProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
seriesProviderIds[MetadataProviders.Tvdb.ToString()] = tvdbId;
var seriesDataPath = TvdbSeriesProvider.GetSeriesDataPath(_config.ApplicationPaths, seriesProviderIds);
// Doesn't have required provider id's
if (string.IsNullOrWhiteSpace(seriesDataPath))
{
return false;
}
// Check this in order to avoid logging an exception due to directory not existing
if (!Directory.Exists(seriesDataPath))
if (string.IsNullOrEmpty(tvdbId))
{
return false;
}
var episodeFiles = _fileSystem.GetFilePaths(seriesDataPath)
.Where(i => string.Equals(Path.GetExtension(i), ".xml", StringComparison.OrdinalIgnoreCase))
.Select(Path.GetFileNameWithoutExtension)
.Where(i => i.StartsWith("episode-", StringComparison.OrdinalIgnoreCase))
.ToList();
var episodes = await _tvDbClientManager.GetAllEpisodesAsync(Convert.ToInt32(tvdbId), series.GetPreferredMetadataLanguage(), cancellationToken);
var episodeLookup = episodeFiles
var episodeLookup = episodes
.Select(i =>
{
var parts = i.Split('-');
if (parts.Length == 3)
{
if (int.TryParse(parts[1], NumberStyles.Integer, _usCulture, out var seasonNumber))
{
if (int.TryParse(parts[2], NumberStyles.Integer, _usCulture, out var episodeNumber))
{
return new ValueTuple<int, int>(seasonNumber, episodeNumber);
}
}
}
return new ValueTuple<int, int>(-1, -1);
DateTime.TryParse(i.FirstAired, out var firstAired);
var seasonNumber = i.AiredSeason.GetValueOrDefault(-1);
var episodeNumber = i.AiredEpisodeNumber.GetValueOrDefault(-1);
return (seasonNumber: seasonNumber, episodeNumber: episodeNumber, firstAired: firstAired);
})
.Where(i => i.Item1 != -1 && i.Item2 != -1)
.Where(i => i.seasonNumber != -1 && i.episodeNumber != -1)
.OrderBy(i => i.seasonNumber)
.ThenBy(i => i.episodeNumber)
.ToList();
var allRecursiveChildren = series.GetRecursiveChildren();
var hasBadData = HasInvalidContent(series, allRecursiveChildren);
var hasBadData = HasInvalidContent(allRecursiveChildren);
// Be conservative here to avoid creating missing episodes for ones they already have
var addMissingEpisodes = !hasBadData && _libraryManager.GetLibraryOptions(series).ImportMissingEpisodes;
var anySeasonsRemoved = RemoveObsoleteOrMissingSeasons(series, allRecursiveChildren, episodeLookup);
var anySeasonsRemoved = RemoveObsoleteOrMissingSeasons(allRecursiveChildren, episodeLookup);
if (anySeasonsRemoved)
{
@ -106,7 +86,7 @@ namespace MediaBrowser.Providers.TV
allRecursiveChildren = series.GetRecursiveChildren();
}
var anyEpisodesRemoved = RemoveObsoleteOrMissingEpisodes(series, allRecursiveChildren, episodeLookup, addMissingEpisodes);
var anyEpisodesRemoved = RemoveObsoleteOrMissingEpisodes(allRecursiveChildren, episodeLookup, addMissingEpisodes);
if (anyEpisodesRemoved)
{
@ -118,7 +98,7 @@ namespace MediaBrowser.Providers.TV
if (addNewItems && series.IsMetadataFetcherEnabled(_libraryManager.GetLibraryOptions(series), TvdbSeriesProvider.Current.Name))
{
hasNewEpisodes = await AddMissingEpisodes(series, allRecursiveChildren, addMissingEpisodes, seriesDataPath, episodeLookup, cancellationToken)
hasNewEpisodes = await AddMissingEpisodes(series, allRecursiveChildren, addMissingEpisodes, episodeLookup, cancellationToken)
.ConfigureAwait(false);
}
@ -134,7 +114,7 @@ namespace MediaBrowser.Providers.TV
/// Returns true if a series has any seasons or episodes without season or episode numbers
/// If this data is missing no virtual items will be added in order to prevent possible duplicates
/// </summary>
private bool HasInvalidContent(Series series, IList<BaseItem> allItems)
private bool HasInvalidContent(IList<BaseItem> allItems)
{
return allItems.OfType<Season>().Any(i => !i.IndexNumber.HasValue) ||
allItems.OfType<Episode>().Any(i =>
@ -149,43 +129,24 @@ namespace MediaBrowser.Providers.TV
});
}
private const double UnairedEpisodeThresholdDays = 2;
/// <summary>
/// Adds the missing episodes.
/// </summary>
/// <param name="series">The series.</param>
/// <returns>Task.</returns>
private async Task<bool> AddMissingEpisodes(Series series,
IList<BaseItem> allItems,
private async Task<bool> AddMissingEpisodes(
Series series,
IEnumerable<BaseItem> allItems,
bool addMissingEpisodes,
string seriesDataPath,
IEnumerable<ValueTuple<int, int>> episodeLookup,
IReadOnlyCollection<(int seasonNumber, int episodenumber, DateTime firstAired)> episodeLookup,
CancellationToken cancellationToken)
{
var existingEpisodes = allItems.OfType<Episode>()
.ToList();
var existingEpisodes = allItems.OfType<Episode>().ToList();
var lookup = episodeLookup as IList<ValueTuple<int, int>> ?? episodeLookup.ToList();
var seasonCounts = (from e in lookup
group e by e.Item1 into g
select g)
.ToDictionary(g => g.Key, g => g.Count());
var seasonCounts = episodeLookup.GroupBy(e => e.seasonNumber).ToDictionary(g => g.Key, g => g.Count());
var hasChanges = false;
foreach (var tuple in lookup)
foreach (var tuple in episodeLookup)
{
if (tuple.Item1 <= 0)
{
// Ignore season zeros
continue;
}
if (tuple.Item2 <= 0)
if (tuple.seasonNumber <= 0 || tuple.episodenumber <= 0)
{
// Ignore episode zeros
// Ignore episode/season zeros
continue;
}
@ -196,33 +157,15 @@ namespace MediaBrowser.Providers.TV
continue;
}
var airDate = GetAirDate(seriesDataPath, tuple.Item1, tuple.Item2);
if (!airDate.HasValue)
{
continue;
}
var now = DateTime.UtcNow;
var airDate = tuple.firstAired;
now = now.AddDays(0 - UnairedEpisodeThresholdDays);
if (airDate.Value < now)
{
if (addMissingEpisodes)
{
// tvdb has a lot of nearly blank episodes
_logger.LogInformation("Creating virtual missing episode {0} {1}x{2}", series.Name, tuple.Item1, tuple.Item2);
await AddEpisode(series, tuple.Item1, tuple.Item2, cancellationToken).ConfigureAwait(false);
var now = DateTime.UtcNow.AddDays(-UnairedEpisodeThresholdDays);
hasChanges = true;
}
}
else if (airDate.Value > now)
if (airDate < now && addMissingEpisodes || airDate > now)
{
// tvdb has a lot of nearly blank episodes
_logger.LogInformation("Creating virtual unaired episode {0} {1}x{2}", series.Name, tuple.Item1, tuple.Item2);
await AddEpisode(series, tuple.Item1, tuple.Item2, cancellationToken).ConfigureAwait(false);
_logger.LogInformation("Creating virtual missing/unaired episode {0} {1}x{2}", series.Name, tuple.seasonNumber, tuple.episodenumber);
await AddEpisode(series, tuple.seasonNumber, tuple.episodenumber, cancellationToken).ConfigureAwait(false);
hasChanges = true;
}
@ -234,59 +177,58 @@ namespace MediaBrowser.Providers.TV
/// <summary>
/// Removes the virtual entry after a corresponding physical version has been added
/// </summary>
private bool RemoveObsoleteOrMissingEpisodes(Series series,
IList<BaseItem> allRecursiveChildren,
IEnumerable<ValueTuple<int, int>> episodeLookup,
private bool RemoveObsoleteOrMissingEpisodes(
IEnumerable<BaseItem> allRecursiveChildren,
IEnumerable<(int seasonNumber, int episodeNumber, DateTime firstAired)> episodeLookup,
bool allowMissingEpisodes)
{
var existingEpisodes = allRecursiveChildren.OfType<Episode>()
.ToList();
var physicalEpisodes = existingEpisodes
.Where(i => i.LocationType != LocationType.Virtual)
.ToList();
var existingEpisodes = allRecursiveChildren.OfType<Episode>();
var virtualEpisodes = existingEpisodes
.Where(i => i.LocationType == LocationType.Virtual)
.ToList();
var physicalEpisodes = new List<Episode>();
var virtualEpisodes = new List<Episode>();
foreach (var episode in existingEpisodes)
{
if (episode.LocationType == LocationType.Virtual)
{
virtualEpisodes.Add(episode);
}
else
{
physicalEpisodes.Add(episode);
}
}
var episodesToRemove = virtualEpisodes
.Where(i =>
{
if (i.IndexNumber.HasValue && i.ParentIndexNumber.HasValue)
if (!i.IndexNumber.HasValue || !i.ParentIndexNumber.HasValue)
{
var seasonNumber = i.ParentIndexNumber.Value;
var episodeNumber = i.IndexNumber.Value;
// If there's a physical episode with the same season and episode number, delete it
if (physicalEpisodes.Any(p =>
p.ParentIndexNumber.HasValue && (p.ParentIndexNumber.Value) == seasonNumber &&
p.ContainsEpisodeNumber(episodeNumber)))
{
return true;
}
return true;
}
// If the episode no longer exists in the remote lookup, delete it
if (!episodeLookup.Any(e => e.Item1 == seasonNumber && e.Item2 == episodeNumber))
{
return true;
}
var seasonNumber = i.ParentIndexNumber.Value;
var episodeNumber = i.IndexNumber.Value;
if (!allowMissingEpisodes && i.IsMissingEpisode)
{
// If it's missing, but not unaired, remove it
if (!i.PremiereDate.HasValue || i.PremiereDate.Value.ToLocalTime().Date.AddDays(UnairedEpisodeThresholdDays) < DateTime.Now.Date)
{
return true;
}
}
// If there's a physical episode with the same season and episode number, delete it
if (physicalEpisodes.Any(p =>
p.ParentIndexNumber.HasValue && p.ParentIndexNumber.Value == seasonNumber &&
p.ContainsEpisodeNumber(episodeNumber)))
{
return true;
}
return false;
// If the episode no longer exists in the remote lookup, delete it
if (!episodeLookup.Any(e => e.seasonNumber == seasonNumber && e.episodeNumber == episodeNumber))
{
return true;
}
return true;
})
.ToList();
// If it's missing, but not unaired, remove it
return !allowMissingEpisodes && i.IsMissingEpisode &&
(!i.PremiereDate.HasValue ||
i.PremiereDate.Value.ToLocalTime().Date.AddDays(UnairedEpisodeThresholdDays) <
DateTime.Now.Date);
});
var hasChanges = false;
@ -295,7 +237,6 @@ namespace MediaBrowser.Providers.TV
_libraryManager.DeleteItem(episodeToRemove, new DeleteOptions
{
DeleteFileLocation = true
}, false);
hasChanges = true;
@ -307,22 +248,27 @@ namespace MediaBrowser.Providers.TV
/// <summary>
/// Removes the obsolete or missing seasons.
/// </summary>
/// <param name="series">The series.</param>
/// <param name="allRecursiveChildren"></param>
/// <param name="episodeLookup">The episode lookup.</param>
/// <returns>Task{System.Boolean}.</returns>
private bool RemoveObsoleteOrMissingSeasons(Series series,
IList<BaseItem> allRecursiveChildren,
IEnumerable<ValueTuple<int, int>> episodeLookup)
private bool RemoveObsoleteOrMissingSeasons(IList<BaseItem> allRecursiveChildren,
IEnumerable<(int seasonNumber, int episodeNumber, DateTime firstAired)> episodeLookup)
{
var existingSeasons = allRecursiveChildren.OfType<Season>().ToList();
var physicalSeasons = existingSeasons
.Where(i => i.LocationType != LocationType.Virtual)
.ToList();
var virtualSeasons = existingSeasons
.Where(i => i.LocationType == LocationType.Virtual)
.ToList();
var physicalSeasons = new List<Season>();
var virtualSeasons = new List<Season>();
foreach (var season in existingSeasons)
{
if (season.LocationType == LocationType.Virtual)
{
virtualSeasons.Add(season);
}
else
{
physicalSeasons.Add(season);
}
}
var allEpisodes = allRecursiveChildren.OfType<Episode>().ToList();
@ -334,28 +280,19 @@ namespace MediaBrowser.Providers.TV
var seasonNumber = i.IndexNumber.Value;
// If there's a physical season with the same number, delete it
if (physicalSeasons.Any(p => p.IndexNumber.HasValue && (p.IndexNumber.Value) == seasonNumber && string.Equals(p.Series.PresentationUniqueKey, i.Series.PresentationUniqueKey, StringComparison.Ordinal)))
if (physicalSeasons.Any(p => p.IndexNumber.HasValue && p.IndexNumber.Value == seasonNumber && string.Equals(p.Series.PresentationUniqueKey, i.Series.PresentationUniqueKey, StringComparison.Ordinal)))
{
return true;
}
// If the season no longer exists in the remote lookup, delete it, but only if an existing episode doesn't require it
if (episodeLookup.All(e => e.Item1 != seasonNumber))
{
if (allEpisodes.All(s => s.ParentIndexNumber != seasonNumber || s.IsInSeasonFolder))
{
return true;
}
}
return false;
return episodeLookup.All(e => e.seasonNumber != seasonNumber) && allEpisodes.All(s => s.ParentIndexNumber != seasonNumber || s.IsInSeasonFolder);
}
// Season does not have a number
// Remove if there are no episodes directly in series without a season number
return allEpisodes.All(s => s.ParentIndexNumber.HasValue || s.IsInSeasonFolder);
})
.ToList();
});
var hasChanges = false;
@ -392,21 +329,19 @@ namespace MediaBrowser.Providers.TV
season = await provider.AddSeason(series, seasonNumber, true, cancellationToken).ConfigureAwait(false);
}
var name = string.Format("Episode {0}", episodeNumber.ToString(_usCulture));
var name = $"Episode {episodeNumber.ToString(_usCulture)}";
var episode = new Episode
{
Name = name,
IndexNumber = episodeNumber,
ParentIndexNumber = seasonNumber,
Id = _libraryManager.GetNewItemId((series.Id + seasonNumber.ToString(_usCulture) + name), typeof(Episode)),
Id = _libraryManager.GetNewItemId(series.Id + seasonNumber.ToString(_usCulture) + name, typeof(Episode)),
IsVirtualItem = true,
SeasonId = season == null ? Guid.Empty : season.Id,
SeasonId = season?.Id ?? Guid.Empty,
SeriesId = series.Id
};
episode.SetParent(season);
season.AddChild(episode, cancellationToken);
await episode.RefreshMetadata(new MetadataRefreshOptions(new DirectoryService(_logger, _fileSystem)), cancellationToken).ConfigureAwait(false);
@ -417,25 +352,31 @@ namespace MediaBrowser.Providers.TV
/// </summary>
/// <param name="existingEpisodes">The existing episodes.</param>
/// <param name="seasonCounts"></param>
/// <param name="tuple">The tuple.</param>
/// <param name="episodeTuple"></param>
/// <returns>Episode.</returns>
private Episode GetExistingEpisode(IList<Episode> existingEpisodes, Dictionary<int, int> seasonCounts, ValueTuple<int, int> tuple)
private Episode GetExistingEpisode(IList<Episode> existingEpisodes, IReadOnlyDictionary<int, int> seasonCounts, (int seasonNumber, int episodeNumber, DateTime firstAired) episodeTuple)
{
var s = tuple.Item1;
var e = tuple.Item2;
var seasonNumber = episodeTuple.seasonNumber;
var episodeNumber = episodeTuple.episodeNumber;
while (true)
{
var episode = GetExistingEpisode(existingEpisodes, s, e);
var episode = GetExistingEpisode(existingEpisodes, seasonNumber, episodeNumber);
if (episode != null)
{
return episode;
}
s--;
seasonNumber--;
if (seasonCounts.ContainsKey(s))
e += seasonCounts[s];
if (seasonCounts.ContainsKey(seasonNumber))
{
episodeNumber += seasonCounts[seasonNumber];
}
else
{
break;
}
}
return null;
@ -446,88 +387,5 @@ namespace MediaBrowser.Providers.TV
return existingEpisodes
.FirstOrDefault(i => i.ParentIndexNumber == season && i.ContainsEpisodeNumber(episode));
}
/// <summary>
/// Gets the air date.
/// </summary>
/// <param name="seriesDataPath">The series data path.</param>
/// <param name="seasonNumber">The season number.</param>
/// <param name="episodeNumber">The episode number.</param>
/// <returns>System.Nullable{DateTime}.</returns>
private DateTime? GetAirDate(string seriesDataPath, int seasonNumber, int episodeNumber)
{
// First open up the tvdb xml file and make sure it has valid data
var filename = string.Format("episode-{0}-{1}.xml", seasonNumber.ToString(_usCulture), episodeNumber.ToString(_usCulture));
var xmlPath = Path.Combine(seriesDataPath, filename);
DateTime? airDate = null;
using (var fileStream = _fileSystem.GetFileStream(xmlPath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
{
// It appears the best way to filter out invalid entries is to only include those with valid air dates
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
{
var settings = _xmlSettings.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
// Use XmlReader for best performance
using (var reader = XmlReader.Create(streamReader, settings))
{
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "EpisodeName":
{
var val = reader.ReadElementContentAsString();
if (string.IsNullOrWhiteSpace(val))
{
// Not valid, ignore these
return null;
}
break;
}
case "FirstAired":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
if (DateTime.TryParse(val, out var date))
{
airDate = date.ToUniversalTime();
}
}
break;
}
default:
{
reader.Skip();
break;
}
}
}
else
{
reader.Read();
}
}
}
}
}
return airDate;
}
}
}

@ -10,6 +10,7 @@ using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Xml;
using MediaBrowser.Providers.Manager;
using MediaBrowser.Providers.TV.TheTVDB;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Providers.TV
@ -18,11 +19,24 @@ namespace MediaBrowser.Providers.TV
{
private readonly ILocalizationManager _localization;
private readonly IXmlReaderSettingsFactory _xmlSettings;
private readonly TvDbClientManager _tvDbClientManager;
public SeriesMetadataService(IServerConfigurationManager serverConfigurationManager, ILogger logger, IProviderManager providerManager, IFileSystem fileSystem, IUserDataManager userDataManager, ILibraryManager libraryManager, ILocalizationManager localization, IXmlReaderSettingsFactory xmlSettings) : base(serverConfigurationManager, logger, providerManager, fileSystem, userDataManager, libraryManager)
public SeriesMetadataService(
IServerConfigurationManager serverConfigurationManager,
ILogger logger,
IProviderManager providerManager,
IFileSystem fileSystem,
IUserDataManager userDataManager,
ILibraryManager libraryManager,
ILocalizationManager localization,
IXmlReaderSettingsFactory xmlSettings,
TvDbClientManager tvDbClientManager
)
: base(serverConfigurationManager, logger, providerManager, fileSystem, userDataManager, libraryManager)
{
_localization = localization;
_xmlSettings = xmlSettings;
_tvDbClientManager = tvDbClientManager;
}
protected override async Task AfterMetadataRefresh(Series item, MetadataRefreshOptions refreshOptions, CancellationToken cancellationToken)
@ -32,12 +46,13 @@ namespace MediaBrowser.Providers.TV
var seasonProvider = new DummySeasonProvider(ServerConfigurationManager, Logger, _localization, LibraryManager, FileSystem);
await seasonProvider.Run(item, cancellationToken).ConfigureAwait(false);
// TODO why does it not register this itself omg
var provider = new MissingEpisodeProvider(Logger,
ServerConfigurationManager,
LibraryManager,
_localization,
FileSystem,
_xmlSettings);
_tvDbClientManager);
try
{

@ -0,0 +1,244 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using Microsoft.Extensions.Caching.Memory;
using TvDbSharper;
using TvDbSharper.Dto;
namespace MediaBrowser.Providers.TV.TheTVDB
{
public class TvDbClientManager
{
private readonly SemaphoreSlim _cacheWriteLock = new SemaphoreSlim(1, 1);
private readonly IMemoryCache _cache;
private readonly TvDbClient _tvDbClient;
private DateTime _tokenCreatedAt;
private const string DefaultLanguage = "en";
public TvDbClientManager(IMemoryCache memoryCache)
{
_cache = memoryCache;
_tvDbClient = new TvDbClient();
_tvDbClient.Authentication.AuthenticateAsync(TvdbUtils.TvdbApiKey);
_tokenCreatedAt = DateTime.Now;
}
public TvDbClient TvDbClient
{
get
{
// Refresh if necessary
if (_tokenCreatedAt > DateTime.Now.Subtract(TimeSpan.FromHours(20)))
{
try
{
_tvDbClient.Authentication.RefreshTokenAsync();
}
catch
{
_tvDbClient.Authentication.AuthenticateAsync(TvdbUtils.TvdbApiKey);
}
_tokenCreatedAt = DateTime.Now;
}
return _tvDbClient;
}
}
public Task<TvDbResponse<SeriesSearchResult[]>> GetSeriesByNameAsync(string name, string language,
CancellationToken cancellationToken)
{
var cacheKey = GenerateKey("series", name, language);
return TryGetValue(cacheKey, language,() => TvDbClient.Search.SearchSeriesByNameAsync(name, cancellationToken));
}
public Task<TvDbResponse<Series>> GetSeriesByIdAsync(int tvdbId, string language,
CancellationToken cancellationToken)
{
var cacheKey = GenerateKey("series", tvdbId, language);
return TryGetValue(cacheKey, language,() => TvDbClient.Series.GetAsync(tvdbId, cancellationToken));
}
public Task<TvDbResponse<EpisodeRecord>> GetEpisodesAsync(int episodeTvdbId, string language,
CancellationToken cancellationToken)
{
var cacheKey = GenerateKey("episode", episodeTvdbId, language);
return TryGetValue(cacheKey, language,() => TvDbClient.Episodes.GetAsync(episodeTvdbId, cancellationToken));
}
public async Task<List<EpisodeRecord>> GetAllEpisodesAsync(int tvdbId, string language,
CancellationToken cancellationToken)
{
// Traverse all episode pages and join them together
var episodes = new List<EpisodeRecord>();
var episodePage = await GetEpisodesPageAsync(tvdbId, new EpisodeQuery(), language, cancellationToken)
.ConfigureAwait(false);
episodes.AddRange(episodePage.Data);
if (!episodePage.Links.Next.HasValue || !episodePage.Links.Last.HasValue)
{
return episodes;
}
int next = episodePage.Links.Next.Value;
int last = episodePage.Links.Last.Value;
for (var page = next; page <= last; ++page)
{
episodePage = await GetEpisodesPageAsync(tvdbId, page, new EpisodeQuery(), language, cancellationToken)
.ConfigureAwait(false);
episodes.AddRange(episodePage.Data);
}
return episodes;
}
public Task<TvDbResponse<SeriesSearchResult[]>> GetSeriesByImdbIdAsync(string imdbId, string language,
CancellationToken cancellationToken)
{
var cacheKey = GenerateKey("series", imdbId, language);
return TryGetValue(cacheKey, language,() => TvDbClient.Search.SearchSeriesByImdbIdAsync(imdbId, cancellationToken));
}
public Task<TvDbResponse<SeriesSearchResult[]>> GetSeriesByZap2ItIdAsync(string zap2ItId, string language,
CancellationToken cancellationToken)
{
var cacheKey = GenerateKey("series", zap2ItId, language);
return TryGetValue( cacheKey, language,() => TvDbClient.Search.SearchSeriesByZap2ItIdAsync(zap2ItId, cancellationToken));
}
public Task<TvDbResponse<Actor[]>> GetActorsAsync(int tvdbId, string language,
CancellationToken cancellationToken)
{
var cacheKey = GenerateKey("actors", tvdbId, language);
return TryGetValue(cacheKey, language,() => TvDbClient.Series.GetActorsAsync(tvdbId, cancellationToken));
}
public Task<TvDbResponse<Image[]>> GetImagesAsync(int tvdbId, ImagesQuery imageQuery, string language,
CancellationToken cancellationToken)
{
var cacheKey = GenerateKey("images", tvdbId, language, imageQuery);
return TryGetValue(cacheKey, language,() => TvDbClient.Series.GetImagesAsync(tvdbId, imageQuery, cancellationToken));
}
public Task<TvDbResponse<Language[]>> GetLanguagesAsync(CancellationToken cancellationToken)
{
return TryGetValue("languages", null,() => TvDbClient.Languages.GetAllAsync(cancellationToken));
}
public Task<TvDbResponse<EpisodesSummary>> GetSeriesEpisodeSummaryAsync(int tvdbId, string language,
CancellationToken cancellationToken)
{
var cacheKey = GenerateKey("seriesepisodesummary", tvdbId, language);
return TryGetValue(cacheKey, language,
() => TvDbClient.Series.GetEpisodesSummaryAsync(tvdbId, cancellationToken));
}
public Task<TvDbResponse<EpisodeRecord[]>> GetEpisodesPageAsync(int tvdbId, int page, EpisodeQuery episodeQuery,
string language, CancellationToken cancellationToken)
{
var cacheKey = GenerateKey(language, tvdbId, episodeQuery);
return TryGetValue(cacheKey, language,
() => TvDbClient.Series.GetEpisodesAsync(tvdbId, page, episodeQuery, cancellationToken));
}
public Task<string> GetEpisodeTvdbId(EpisodeInfo searchInfo, string language,
CancellationToken cancellationToken)
{
searchInfo.SeriesProviderIds.TryGetValue(MetadataProviders.Tvdb.ToString(),
out var seriesTvdbId);
var episodeQuery = new EpisodeQuery();
// Prefer SxE over premiere date as it is more robust
if (searchInfo.IndexNumber.HasValue && searchInfo.ParentIndexNumber.HasValue)
{
episodeQuery.AiredEpisode = searchInfo.IndexNumber.Value;
episodeQuery.AiredSeason = searchInfo.ParentIndexNumber.Value;
}
else if (searchInfo.PremiereDate.HasValue)
{
// tvdb expects yyyy-mm-dd format
episodeQuery.FirstAired = searchInfo.PremiereDate.Value.ToString("yyyy-MM-dd");
}
return GetEpisodeTvdbId(Convert.ToInt32(seriesTvdbId), episodeQuery, language, cancellationToken);
}
public async Task<string> GetEpisodeTvdbId(int seriesTvdbId, EpisodeQuery episodeQuery,
string language,
CancellationToken cancellationToken)
{
var episodePage =
await GetEpisodesPageAsync(Convert.ToInt32(seriesTvdbId), episodeQuery, language, cancellationToken)
.ConfigureAwait(false);
return episodePage.Data.FirstOrDefault()?.Id.ToString();
}
public Task<TvDbResponse<EpisodeRecord[]>> GetEpisodesPageAsync(int tvdbId, EpisodeQuery episodeQuery,
string language, CancellationToken cancellationToken)
{
return GetEpisodesPageAsync(tvdbId, 1, episodeQuery, language, cancellationToken);
}
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 static string GenerateKey(params object[] objects)
{
var key = string.Empty;
foreach (var obj in objects)
{
var objType = obj.GetType();
if (objType.IsPrimitive || objType == typeof(string))
{
key += obj + ";";
}
else
{
foreach (PropertyInfo propertyInfo in objType.GetProperties())
{
var currentValue = propertyInfo.GetValue(obj, null);
if (currentValue == null)
{
continue;
}
key += propertyInfo.Name + "=" + currentValue + ";";
}
}
}
return key;
}
}
}

@ -1,33 +1,30 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Providers;
using Microsoft.Extensions.Logging;
using TvDbSharper;
using TvDbSharper.Dto;
namespace MediaBrowser.Providers.TV.TheTVDB
{
public class TvdbEpisodeImageProvider : IRemoteImageProvider
{
private readonly IServerConfigurationManager _config;
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private readonly IHttpClient _httpClient;
private readonly IFileSystem _fileSystem;
private readonly ILogger _logger;
private readonly TvDbClientManager _tvDbClientManager;
public TvdbEpisodeImageProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem)
public TvdbEpisodeImageProvider(IHttpClient httpClient, ILogger<TvdbEpisodeImageProvider> logger, TvDbClientManager tvDbClientManager)
{
_config = config;
_httpClient = httpClient;
_fileSystem = fileSystem;
_logger = logger;
_tvDbClientManager = tvDbClientManager;
}
public string Name => "TheTVDB";
@ -45,113 +42,70 @@ namespace MediaBrowser.Providers.TV.TheTVDB
};
}
public Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
{
var episode = (Episode)item;
var series = episode.Series;
var imageResult = new List<RemoteImageInfo>();
var language = item.GetPreferredMetadataLanguage();
if (series != null && TvdbSeriesProvider.IsValidSeries(series.ProviderIds))
{
// Process images
var seriesDataPath = TvdbSeriesProvider.GetSeriesDataPath(_config.ApplicationPaths, series.ProviderIds);
var nodes = TvdbEpisodeProvider.Current.GetEpisodeXmlNodes(seriesDataPath, episode.GetLookupInfo());
var result = nodes.Select(i => GetImageInfo(i, cancellationToken))
.Where(i => i != null)
.ToList();
var episodeTvdbId = episode.GetProviderId(MetadataProviders.Tvdb);
return Task.FromResult<IEnumerable<RemoteImageInfo>>(result);
}
return Task.FromResult<IEnumerable<RemoteImageInfo>>(new RemoteImageInfo[] { });
}
private RemoteImageInfo GetImageInfo(XmlReader reader, CancellationToken cancellationToken)
{
var height = 225;
var width = 400;
var url = string.Empty;
// Use XmlReader for best performance
using (reader)
{
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
// Process images
try
{
if (reader.NodeType == XmlNodeType.Element)
if (string.IsNullOrEmpty(episodeTvdbId))
{
cancellationToken.ThrowIfCancellationRequested();
switch (reader.Name)
var episodeInfo = new EpisodeInfo
{
case "thumb_width":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
// int.TryParse is local aware, so it can be probamatic, force us culture
if (int.TryParse(val, NumberStyles.Integer, _usCulture, out var rval))
{
width = rval;
}
}
break;
}
case "thumb_height":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
// int.TryParse is local aware, so it can be probamatic, force us culture
if (int.TryParse(val, NumberStyles.Integer, _usCulture, out var rval))
{
height = rval;
}
}
break;
}
case "filename":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
url = TVUtils.BannerUrl + val;
}
break;
}
default:
{
reader.Skip();
break;
}
IndexNumber = episode.IndexNumber.Value,
ParentIndexNumber = episode.ParentIndexNumber.Value,
SeriesProviderIds = series.ProviderIds
};
episodeTvdbId = await _tvDbClientManager
.GetEpisodeTvdbId(episodeInfo, language, cancellationToken).ConfigureAwait(false);
if (string.IsNullOrEmpty(episodeTvdbId))
{
_logger.LogError("Episode {SeasonNumber}x{EpisodeNumber} not found for series {SeriesTvdbId}",
episodeInfo.ParentIndexNumber, episodeInfo.IndexNumber, series.GetProviderId(MetadataProviders.Tvdb));
return imageResult;
}
}
else
var episodeResult =
await _tvDbClientManager
.GetEpisodesAsync(Convert.ToInt32(episodeTvdbId), language, cancellationToken)
.ConfigureAwait(false);
var image = GetImageInfo(episodeResult.Data);
if (image != null)
{
reader.Read();
imageResult.Add(image);
}
}
catch (TvDbServerException e)
{
_logger.LogError(e, "Failed to retrieve episode images for {TvDbId}", episodeTvdbId);
}
}
if (string.IsNullOrEmpty(url))
return imageResult;
}
private RemoteImageInfo GetImageInfo(EpisodeRecord episode)
{
if (string.IsNullOrEmpty(episode.Filename))
{
return null;
}
return new RemoteImageInfo
{
Width = width,
Height = height,
Width = Convert.ToInt32(episode.ThumbWidth),
Height = Convert.ToInt32(episode.ThumbHeight),
ProviderName = Name,
Url = url,
Url = TvdbUtils.BannerUrl + episode.Filename,
Type = ImageType.Primary
};
}

@ -1,22 +1,16 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Providers;
using MediaBrowser.Model.Xml;
using Microsoft.Extensions.Logging;
using TvDbSharper;
using TvDbSharper.Dto;
namespace MediaBrowser.Providers.TV.TheTVDB
{
@ -24,44 +18,52 @@ namespace MediaBrowser.Providers.TV.TheTVDB
/// <summary>
/// Class RemoteEpisodeProvider
/// </summary>
class TvdbEpisodeProvider : IRemoteMetadataProvider<Episode, EpisodeInfo>
class TvdbEpisodeProvider : IRemoteMetadataProvider<Episode, EpisodeInfo>, IHasOrder
{
private static readonly string FullIdKey = MetadataProviders.Tvdb + "-Full";
internal static TvdbEpisodeProvider Current;
private readonly IFileSystem _fileSystem;
private readonly IServerConfigurationManager _config;
private readonly IHttpClient _httpClient;
private readonly ILogger _logger;
private readonly IXmlReaderSettingsFactory _xmlSettings;
private readonly TvDbClientManager _tvDbClientManager;
public TvdbEpisodeProvider(IFileSystem fileSystem, IServerConfigurationManager config, IHttpClient httpClient, ILogger logger, IXmlReaderSettingsFactory xmlSettings)
public TvdbEpisodeProvider(IHttpClient httpClient, ILogger<TvdbEpisodeProvider> logger, TvDbClientManager tvDbClientManager)
{
_fileSystem = fileSystem;
_config = config;
_httpClient = httpClient;
_logger = logger;
_xmlSettings = xmlSettings;
Current = this;
_tvDbClientManager = tvDbClientManager;
}
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(EpisodeInfo searchInfo, CancellationToken cancellationToken)
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(EpisodeInfo searchInfo, CancellationToken cancellationToken)
{
var list = new List<RemoteSearchResult>();
// The search query must either provide an episode number or date
if (!searchInfo.IndexNumber.HasValue && !searchInfo.PremiereDate.HasValue)
if (!searchInfo.IndexNumber.HasValue || !searchInfo.PremiereDate.HasValue)
{
return Task.FromResult((IEnumerable<RemoteSearchResult>)list);
return list;
}
if (TvdbSeriesProvider.IsValidSeries(searchInfo.SeriesProviderIds))
{
var seriesDataPath = TvdbSeriesProvider.GetSeriesDataPath(_config.ApplicationPaths, searchInfo.SeriesProviderIds);
try
{
var metadataResult = FetchEpisodeData(searchInfo, seriesDataPath, cancellationToken);
var episodeTvdbId = searchInfo.GetProviderId(MetadataProviders.Tvdb);
if (string.IsNullOrEmpty(episodeTvdbId))
{
searchInfo.SeriesProviderIds.TryGetValue(MetadataProviders.Tvdb.ToString(),
out var seriesTvdbId);
episodeTvdbId = await _tvDbClientManager
.GetEpisodeTvdbId(searchInfo, searchInfo.MetadataLanguage, cancellationToken)
.ConfigureAwait(false);
if (string.IsNullOrEmpty(episodeTvdbId))
{
_logger.LogError("Episode {SeasonNumber}x{EpisodeNumber} not found for series {SeriesTvdbId}",
searchInfo.ParentIndexNumber, searchInfo.IndexNumber, seriesTvdbId);
return list;
}
}
var episodeResult = await _tvDbClientManager.GetEpisodesAsync(Convert.ToInt32(episodeTvdbId),
searchInfo.MetadataLanguage, cancellationToken).ConfigureAwait(false);
var metadataResult = MapEpisodeToResult(searchInfo, episodeResult.Data);
if (metadataResult.HasMetadata)
{
@ -80,689 +82,117 @@ namespace MediaBrowser.Providers.TV.TheTVDB
});
}
}
catch (FileNotFoundException)
{
// Don't fail the provider because this will just keep on going and going.
}
catch (IOException)
catch (TvDbServerException e)
{
// Don't fail the provider because this will just keep on going and going.
_logger.LogError(e, "Failed to retrieve episode with id {TvDbId}", searchInfo.IndexNumber);
}
}
return Task.FromResult((IEnumerable<RemoteSearchResult>)list);
return list;
}
public string Name => "TheTVDB";
public async Task<MetadataResult<Episode>> GetMetadata(EpisodeInfo searchInfo, CancellationToken cancellationToken)
{
var result = new MetadataResult<Episode>();
result.QueriedById = true;
var result = new MetadataResult<Episode>
{
QueriedById = true
};
if (TvdbSeriesProvider.IsValidSeries(searchInfo.SeriesProviderIds) &&
(searchInfo.IndexNumber.HasValue || searchInfo.PremiereDate.HasValue))
{
var seriesDataPath = await TvdbSeriesProvider.Current.EnsureSeriesInfo(searchInfo.SeriesProviderIds, null, null, searchInfo.MetadataLanguage, cancellationToken).ConfigureAwait(false);
if (string.IsNullOrEmpty(seriesDataPath))
{
return result;
}
var tvdbId = searchInfo.GetProviderId(MetadataProviders.Tvdb);
try
{
result = FetchEpisodeData(searchInfo, seriesDataPath, cancellationToken);
}
catch (FileNotFoundException)
{
// Don't fail the provider because this will just keep on going and going.
if (string.IsNullOrEmpty(tvdbId))
{
tvdbId = await _tvDbClientManager
.GetEpisodeTvdbId(searchInfo, searchInfo.MetadataLanguage, cancellationToken)
.ConfigureAwait(false);
if (string.IsNullOrEmpty(tvdbId))
{
_logger.LogError("Episode {SeasonNumber}x{EpisodeNumber} not found for series {SeriesTvdbId}",
searchInfo.ParentIndexNumber, searchInfo.IndexNumber, tvdbId);
return result;
}
}
var episodeResult = await _tvDbClientManager.GetEpisodesAsync(
Convert.ToInt32(tvdbId), searchInfo.MetadataLanguage,
cancellationToken).ConfigureAwait(false);
result = MapEpisodeToResult(searchInfo, episodeResult.Data);
}
catch (IOException)
catch (TvDbServerException e)
{
// Don't fail the provider because this will just keep on going and going.
_logger.LogError(e, "Failed to retrieve episode with id {TvDbId}", tvdbId);
}
}
else
{
_logger.LogDebug("No series identity found for {0}", searchInfo.Name);
_logger.LogDebug("No series identity found for {EpisodeName}", searchInfo.Name);
}
return result;
}
/// <summary>
/// Gets the episode XML files.
/// </summary>
/// <param name="seriesDataPath">The series data path.</param>
/// <param name="searchInfo">The search information.</param>
/// <returns>List{FileInfo}.</returns>
internal List<XmlReader> GetEpisodeXmlNodes(string seriesDataPath, EpisodeInfo searchInfo)
{
var seriesXmlPath = TvdbSeriesProvider.Current.GetSeriesXmlPath(searchInfo.SeriesProviderIds, searchInfo.MetadataLanguage);
try
{
return GetXmlNodes(seriesXmlPath, searchInfo);
}
catch (FileNotFoundException)
{
return new List<XmlReader>();
}
catch (IOException)
{
return new List<XmlReader>();
}
}
/// <summary>
/// Fetches the episode data.
/// </summary>
/// <param name="id">The identifier.</param>
/// <param name="seriesDataPath">The series data path.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{System.Boolean}.</returns>
private MetadataResult<Episode> FetchEpisodeData(EpisodeInfo id, string seriesDataPath, CancellationToken cancellationToken)
private static MetadataResult<Episode> MapEpisodeToResult(EpisodeInfo id, EpisodeRecord episode)
{
var result = new MetadataResult<Episode>()
var result = new MetadataResult<Episode>
{
HasMetadata = true,
Item = new Episode
{
IndexNumber = id.IndexNumber,
ParentIndexNumber = id.ParentIndexNumber,
IndexNumberEnd = id.IndexNumberEnd
}
};
var xmlNodes = GetEpisodeXmlNodes(seriesDataPath, id);
if (xmlNodes.Count > 0)
{
FetchMainEpisodeInfo(result, xmlNodes[0], id.SeriesDisplayOrder, cancellationToken);
result.HasMetadata = true;
}
foreach (var node in xmlNodes.Skip(1))
{
FetchAdditionalPartInfo(result, node, cancellationToken);
}
return result;
}
private List<XmlReader> GetXmlNodes(string xmlFile, EpisodeInfo searchInfo)
{
var list = new List<XmlReader>();
if (searchInfo.IndexNumber.HasValue)
{
var files = GetEpisodeXmlFiles(searchInfo.SeriesDisplayOrder, searchInfo.ParentIndexNumber, searchInfo.IndexNumber, searchInfo.IndexNumberEnd, Path.GetDirectoryName(xmlFile));
list = files.Select(GetXmlReader).ToList();
}
if (list.Count == 0 && searchInfo.PremiereDate.HasValue)
{
list = GetXmlNodesByPremiereDate(xmlFile, searchInfo.PremiereDate.Value);
}
return list;
}
private string GetEpisodeFileName(string seriesDisplayOrder, int? seasonNumber, int? episodeNumber)
{
if (string.Equals(seriesDisplayOrder, "absolute", StringComparison.OrdinalIgnoreCase))
{
return string.Format("episode-abs-{0}.xml", episodeNumber);
}
else if (string.Equals(seriesDisplayOrder, "dvd", StringComparison.OrdinalIgnoreCase))
{
return string.Format("episode-dvd-{0}-{1}.xml", seasonNumber.Value, episodeNumber);
}
else
{
return string.Format("episode-{0}-{1}.xml", seasonNumber.Value, episodeNumber);
}
}
private FileSystemMetadata GetEpisodeFileInfoWithFallback(string seriesDataPath, string seriesDisplayOrder, int? seasonNumber, int? episodeNumber)
{
var file = Path.Combine(seriesDataPath, GetEpisodeFileName(seriesDisplayOrder, seasonNumber, episodeNumber));
var fileInfo = _fileSystem.GetFileInfo(file);
if (fileInfo.Exists)
{
return fileInfo;
}
if (!seasonNumber.HasValue)
{
return fileInfo;
}
// revert to aired order
if (string.Equals(seriesDisplayOrder, "absolute", StringComparison.OrdinalIgnoreCase) || string.Equals(seriesDisplayOrder, "dvd", StringComparison.OrdinalIgnoreCase))
{
file = Path.Combine(seriesDataPath, GetEpisodeFileName(null, seasonNumber, episodeNumber));
return _fileSystem.GetFileInfo(file);
}
return fileInfo;
}
private List<FileSystemMetadata> GetEpisodeXmlFiles(string seriesDisplayOrder, int? seasonNumber, int? episodeNumber, int? endingEpisodeNumber, string seriesDataPath)
{
var files = new List<FileSystemMetadata>();
if (episodeNumber == null)
{
return files;
}
if (!seasonNumber.HasValue)
{
seriesDisplayOrder = "absolute";
}
var fileInfo = GetEpisodeFileInfoWithFallback(seriesDataPath, seriesDisplayOrder, seasonNumber, episodeNumber);
if (fileInfo.Exists)
{
files.Add(fileInfo);
}
var end = endingEpisodeNumber ?? episodeNumber;
episodeNumber++;
while (episodeNumber <= end)
{
fileInfo = GetEpisodeFileInfoWithFallback(seriesDataPath, seriesDisplayOrder, seasonNumber, episodeNumber);
if (fileInfo.Exists)
{
files.Add(fileInfo);
}
else
{
break;
}
episodeNumber++;
}
return files;
}
private XmlReader GetXmlReader(FileSystemMetadata xmlFile)
{
return GetXmlReader(File.ReadAllText(xmlFile.FullName, Encoding.UTF8));
}
private XmlReader GetXmlReader(string xml)
{
var streamReader = new StringReader(xml);
var settings = _xmlSettings.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
return XmlReader.Create(streamReader, settings);
}
private List<XmlReader> GetXmlNodesByPremiereDate(string xmlFile, DateTime premiereDate)
{
var list = new List<XmlReader>();
using (var fileStream = _fileSystem.GetFileStream(xmlFile, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
{
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
{
// Use XmlReader for best performance
var settings = _xmlSettings.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
using (var reader = XmlReader.Create(streamReader, settings))
{
reader.MoveToContent();
reader.Read();
IndexNumberEnd = id.IndexNumberEnd,
AirsBeforeEpisodeNumber = episode.AirsBeforeEpisode,
AirsAfterSeasonNumber = episode.AirsAfterSeason,
AirsBeforeSeasonNumber = episode.AirsBeforeSeason,
Name = episode.EpisodeName,
Overview = episode.Overview,
CommunityRating = (float?)episode.SiteRating,
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "Episode":
{
var outerXml = reader.ReadOuterXml();
var airDate = GetEpisodeAirDate(outerXml);
if (airDate.HasValue && premiereDate.Date == airDate.Value.Date)
{
list.Add(GetXmlReader(outerXml));
return list;
}
break;
}
default:
reader.Skip();
break;
}
}
else
{
reader.Read();
}
}
}
}
}
return list;
}
private DateTime? GetEpisodeAirDate(string xml)
{
using (var streamReader = new StringReader(xml))
{
var settings = _xmlSettings.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
// Use XmlReader for best performance
using (var reader = XmlReader.Create(streamReader, settings))
{
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "FirstAired":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
if (DateTime.TryParse(val, out var date))
{
date = date.ToUniversalTime();
return date;
}
}
break;
}
default:
reader.Skip();
break;
}
}
else
{
reader.Read();
}
}
}
}
return null;
}
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
};
result.ResetPeople();
private void FetchMainEpisodeInfo(MetadataResult<Episode> result, XmlReader reader, string seriesOrder, CancellationToken cancellationToken)
{
var item = result.Item;
item.SetProviderId(MetadataProviders.Tvdb, episode.Id.ToString());
item.SetProviderId(MetadataProviders.Imdb, episode.ImdbId);
int? episodeNumber = null;
int? seasonNumber = null;
int? combinedEpisodeNumber = null;
int? combinedSeasonNumber = null;
// Use XmlReader for best performance
using (reader)
if (string.Equals(id.SeriesDisplayOrder, "dvd", StringComparison.OrdinalIgnoreCase))
{
result.ResetPeople();
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
{
cancellationToken.ThrowIfCancellationRequested();
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "id":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
item.SetProviderId(MetadataProviders.Tvdb, val);
}
break;
}
case "IMDB_ID":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
item.SetProviderId(MetadataProviders.Imdb, val);
}
break;
}
case "EpisodeNumber":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
// int.TryParse is local aware, so it can be probamatic, force us culture
if (int.TryParse(val, NumberStyles.Integer, _usCulture, out var rval))
{
episodeNumber = rval;
}
}
break;
}
case "SeasonNumber":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
// int.TryParse is local aware, so it can be probamatic, force us culture
if (int.TryParse(val, NumberStyles.Integer, _usCulture, out var rval))
{
seasonNumber = rval;
}
}
break;
}
case "Combined_episodenumber":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
if (float.TryParse(val, NumberStyles.Any, _usCulture, out var num))
{
combinedEpisodeNumber = Convert.ToInt32(num);
}
}
break;
}
case "Combined_season":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
if (float.TryParse(val, NumberStyles.Any, _usCulture, out var num))
{
combinedSeasonNumber = Convert.ToInt32(num);
}
}
break;
}
case "airsbefore_episode":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
// int.TryParse is local aware, so it can be probamatic, force us culture
if (int.TryParse(val, NumberStyles.Integer, _usCulture, out var rval))
{
item.AirsBeforeEpisodeNumber = rval;
}
}
break;
}
case "airsafter_season":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
// int.TryParse is local aware, so it can be probamatic, force us culture
if (int.TryParse(val, NumberStyles.Integer, _usCulture, out var rval))
{
item.AirsAfterSeasonNumber = rval;
}
}
break;
}
case "airsbefore_season":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
// int.TryParse is local aware, so it can be probamatic, force us culture
if (int.TryParse(val, NumberStyles.Integer, _usCulture, out var rval))
{
item.AirsBeforeSeasonNumber = rval;
}
}
break;
}
case "EpisodeName":
{
var val = reader.ReadElementContentAsString();
if (!item.LockedFields.Contains(MetadataFields.Name))
{
if (!string.IsNullOrWhiteSpace(val))
{
item.Name = val;
}
}
break;
}
case "Overview":
{
var val = reader.ReadElementContentAsString();
if (!item.LockedFields.Contains(MetadataFields.Overview))
{
if (!string.IsNullOrWhiteSpace(val))
{
item.Overview = val;
}
}
break;
}
case "Rating":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
// float.TryParse is local aware, so it can be probamatic, force us culture
if (float.TryParse(val, NumberStyles.AllowDecimalPoint, _usCulture, out var rval))
{
item.CommunityRating = rval;
}
}
break;
}
case "RatingCount":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
// int.TryParse is local aware, so it can be probamatic, force us culture
if (int.TryParse(val, NumberStyles.Integer, _usCulture, out var rval))
{
//item.VoteCount = rval;
}
}
break;
}
case "FirstAired":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
if (DateTime.TryParse(val, out var date))
{
date = date.ToUniversalTime();
item.PremiereDate = date;
item.ProductionYear = date.Year;
}
}
break;
}
case "Director":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
if (!item.LockedFields.Contains(MetadataFields.Cast))
{
AddPeople(result, val, PersonType.Director);
}
}
break;
}
case "GuestStars":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
if (!item.LockedFields.Contains(MetadataFields.Cast))
{
AddGuestStars(result, val);
}
}
break;
}
case "Writer":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
if (!item.LockedFields.Contains(MetadataFields.Cast))
{
//AddPeople(result, val, PersonType.Writer);
}
}
break;
}
case "Language":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
result.ResultLanguage = val;
}
break;
}
default:
reader.Skip();
break;
}
}
else
{
reader.Read();
}
}
item.IndexNumber = Convert.ToInt32(episode.DvdEpisodeNumber ?? episode.AiredEpisodeNumber);
item.ParentIndexNumber = episode.DvdSeason ?? episode.AiredSeason;
}
if (string.Equals(seriesOrder, "dvd", StringComparison.OrdinalIgnoreCase))
else if (episode.AiredEpisodeNumber.HasValue)
{
episodeNumber = combinedEpisodeNumber ?? episodeNumber;
seasonNumber = combinedSeasonNumber ?? seasonNumber;
item.IndexNumber = episode.AiredEpisodeNumber;
}
if (episodeNumber.HasValue)
else if (episode.AiredSeason.HasValue)
{
item.IndexNumber = episodeNumber;
item.ParentIndexNumber = episode.AiredSeason;
}
if (seasonNumber.HasValue)
if (DateTime.TryParse(episode.FirstAired, out var date))
{
item.ParentIndexNumber = seasonNumber;
// dates from tvdb are UTC but without offset or Z
item.PremiereDate = date;
item.ProductionYear = date.Year;
}
}
private void AddPeople<T>(MetadataResult<T> result, string val, string personType)
{
// Sometimes tvdb actors have leading spaces
foreach (var person in val.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
.Where(i => !string.IsNullOrWhiteSpace(i))
.Select(str => new PersonInfo { Type = personType, Name = str.Trim() }))
foreach (var director in episode.Directors)
{
result.AddPerson(person);
result.AddPerson(new PersonInfo
{
Name = director,
Type = PersonType.Director
});
}
}
private void AddGuestStars<T>(MetadataResult<T> result, string val)
where T : BaseItem
{
// example:
// <GuestStars>|Mark C. Thomas| Dennis Kiefer| David Nelson (David)| Angela Nicholas| Tzi Ma| Kevin P. Kearns (Pasco)|</GuestStars>
var persons = val.Split('|')
.Select(i => i.Trim())
.Where(i => !string.IsNullOrWhiteSpace(i))
.ToList();
foreach (var person in persons)
foreach (var person in episode.GuestStars)
{
var index = person.IndexOf('(');
string role = null;
@ -782,106 +212,17 @@ namespace MediaBrowser.Providers.TV.TheTVDB
Role = role
});
}
}
private void FetchAdditionalPartInfo(MetadataResult<Episode> result, XmlReader reader, CancellationToken cancellationToken)
{
var item = result.Item;
// Use XmlReader for best performance
using (reader)
foreach (var writer in episode.Writers)
{
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
result.AddPerson(new PersonInfo
{
cancellationToken.ThrowIfCancellationRequested();
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "EpisodeName":
{
var val = reader.ReadElementContentAsString();
if (!item.LockedFields.Contains(MetadataFields.Name))
{
if (!string.IsNullOrWhiteSpace(val))
{
item.Name += ", " + val;
}
}
break;
}
case "Overview":
{
var val = reader.ReadElementContentAsString();
if (!item.LockedFields.Contains(MetadataFields.Overview))
{
if (!string.IsNullOrWhiteSpace(val))
{
item.Overview += Environment.NewLine + Environment.NewLine + val;
}
}
break;
}
case "Director":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
if (!item.LockedFields.Contains(MetadataFields.Cast))
{
AddPeople(result, val, PersonType.Director);
}
}
break;
}
case "GuestStars":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
if (!item.LockedFields.Contains(MetadataFields.Cast))
{
AddGuestStars(result, val);
}
}
break;
}
case "Writer":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
if (!item.LockedFields.Contains(MetadataFields.Cast))
{
//AddPeople(result, val, PersonType.Writer);
}
}
break;
}
default:
reader.Skip();
break;
}
}
else
{
reader.Read();
}
}
Name = writer,
Type = PersonType.Writer
});
}
result.ResultLanguage = episode.Language.EpisodeName;
return result;
}
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)

@ -1,398 +0,0 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Xml;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Providers.TV.TheTVDB
{
/// <summary>
/// Class TvdbPrescanTask
/// </summary>
public class TvdbPrescanTask : ILibraryPostScanTask
{
public const string TvdbBaseUrl = "https://thetvdb.com/";
/// <summary>
/// The server time URL
/// </summary>
private const string ServerTimeUrl = TvdbBaseUrl + "api/Updates.php?type=none";
/// <summary>
/// The updates URL
/// </summary>
private const string UpdatesUrl = TvdbBaseUrl + "api/Updates.php?type=all&time={0}";
/// <summary>
/// The _HTTP client
/// </summary>
private readonly IHttpClient _httpClient;
/// <summary>
/// The _logger
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// The _config
/// </summary>
private readonly IServerConfigurationManager _config;
private readonly IFileSystem _fileSystem;
private readonly ILibraryManager _libraryManager;
private readonly IXmlReaderSettingsFactory _xmlSettings;
/// <summary>
/// Initializes a new instance of the <see cref="TvdbPrescanTask"/> class.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="httpClient">The HTTP client.</param>
/// <param name="config">The config.</param>
public TvdbPrescanTask(ILogger logger, IHttpClient httpClient, IServerConfigurationManager config, IFileSystem fileSystem, ILibraryManager libraryManager, IXmlReaderSettingsFactory xmlSettings)
{
_logger = logger;
_httpClient = httpClient;
_config = config;
_fileSystem = fileSystem;
_libraryManager = libraryManager;
_xmlSettings = xmlSettings;
}
protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
/// <summary>
/// Runs the specified progress.
/// </summary>
/// <param name="progress">The progress.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
{
var path = TvdbSeriesProvider.GetSeriesDataPath(_config.CommonApplicationPaths);
Directory.CreateDirectory(path);
var timestampFile = Path.Combine(path, "time.txt");
var timestampFileInfo = _fileSystem.GetFileInfo(timestampFile);
// Don't check for tvdb updates anymore frequently than 24 hours
if (timestampFileInfo.Exists && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(timestampFileInfo)).TotalDays < 1)
{
return;
}
// Find out the last time we queried tvdb for updates
var lastUpdateTime = timestampFileInfo.Exists ? File.ReadAllText(timestampFile, Encoding.UTF8) : string.Empty;
string newUpdateTime;
var existingDirectories = _fileSystem.GetDirectoryPaths(path)
.Select(Path.GetFileName)
.ToList();
var seriesList = _libraryManager.GetItemList(new InternalItemsQuery()
{
IncludeItemTypes = new[] { typeof(Series).Name },
Recursive = true,
GroupByPresentationUniqueKey = false,
DtoOptions = new DtoOptions(false)
{
EnableImages = false
}
}).Cast<Series>()
.ToList();
var seriesIdsInLibrary = seriesList
.Where(i => !string.IsNullOrEmpty(i.GetProviderId(MetadataProviders.Tvdb)))
.Select(i => i.GetProviderId(MetadataProviders.Tvdb))
.ToList();
var missingSeries = seriesIdsInLibrary.Except(existingDirectories, StringComparer.OrdinalIgnoreCase)
.ToList();
var enableInternetProviders = seriesList.Count == 0 ? false : seriesList[0].IsMetadataFetcherEnabled(_libraryManager.GetLibraryOptions(seriesList[0]), TvdbSeriesProvider.Current.Name);
if (!enableInternetProviders)
{
progress.Report(100);
return;
}
// If this is our first time, update all series
if (string.IsNullOrEmpty(lastUpdateTime))
{
// First get tvdb server time
using (var response = await _httpClient.SendAsync(new HttpRequestOptions
{
Url = ServerTimeUrl,
CancellationToken = cancellationToken,
EnableHttpCompression = true,
BufferContent = false
}, "GET").ConfigureAwait(false))
{
// First get tvdb server time
using (var stream = response.Content)
{
newUpdateTime = GetUpdateTime(stream);
}
}
existingDirectories.AddRange(missingSeries);
await UpdateSeries(existingDirectories, path, null, progress, cancellationToken).ConfigureAwait(false);
}
else
{
var seriesToUpdate = await GetSeriesIdsToUpdate(existingDirectories, lastUpdateTime, cancellationToken).ConfigureAwait(false);
newUpdateTime = seriesToUpdate.Item2;
long.TryParse(lastUpdateTime, NumberStyles.Any, UsCulture, out var lastUpdateValue);
var nullableUpdateValue = lastUpdateValue == 0 ? (long?)null : lastUpdateValue;
var listToUpdate = seriesToUpdate.Item1.ToList();
listToUpdate.AddRange(missingSeries);
await UpdateSeries(listToUpdate, path, nullableUpdateValue, progress, cancellationToken).ConfigureAwait(false);
}
File.WriteAllText(timestampFile, newUpdateTime, Encoding.UTF8);
progress.Report(100);
}
/// <summary>
/// Gets the update time.
/// </summary>
/// <param name="response">The response.</param>
/// <returns>System.String.</returns>
private string GetUpdateTime(Stream response)
{
var settings = _xmlSettings.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
using (var streamReader = new StreamReader(response, Encoding.UTF8))
{
// Use XmlReader for best performance
using (var reader = XmlReader.Create(streamReader, settings))
{
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "Time":
{
return (reader.ReadElementContentAsString() ?? string.Empty).Trim();
}
default:
reader.Skip();
break;
}
}
else
{
reader.Read();
}
}
}
}
return null;
}
/// <summary>
/// Gets the series ids to update.
/// </summary>
/// <param name="existingSeriesIds">The existing series ids.</param>
/// <param name="lastUpdateTime">The last update time.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{IEnumerable{System.String}}.</returns>
private async Task<Tuple<IEnumerable<string>, string>> GetSeriesIdsToUpdate(IEnumerable<string> existingSeriesIds, string lastUpdateTime, CancellationToken cancellationToken)
{
// First get last time
using (var response = await _httpClient.SendAsync(new HttpRequestOptions
{
Url = string.Format(UpdatesUrl, lastUpdateTime),
CancellationToken = cancellationToken,
EnableHttpCompression = true,
BufferContent = false
}, "GET").ConfigureAwait(false))
{
using (var stream = response.Content)
{
var data = GetUpdatedSeriesIdList(stream);
var existingDictionary = existingSeriesIds.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
var seriesList = data.Item1
.Where(i => !string.IsNullOrWhiteSpace(i) && existingDictionary.ContainsKey(i));
return new Tuple<IEnumerable<string>, string>(seriesList, data.Item2);
}
}
}
private Tuple<List<string>, string> GetUpdatedSeriesIdList(Stream stream)
{
string updateTime = null;
var idList = new List<string>();
var settings = _xmlSettings.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
using (var streamReader = new StreamReader(stream, Encoding.UTF8))
{
// Use XmlReader for best performance
using (var reader = XmlReader.Create(streamReader, settings))
{
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "Time":
{
updateTime = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
break;
}
case "Series":
{
var id = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
idList.Add(id);
break;
}
default:
reader.Skip();
break;
}
}
else
{
reader.Read();
}
}
}
}
return new Tuple<List<string>, string>(idList, updateTime);
}
/// <summary>
/// Updates the series.
/// </summary>
/// <param name="seriesIds">The series ids.</param>
/// <param name="seriesDataPath">The series data path.</param>
/// <param name="lastTvDbUpdateTime">The last tv db update time.</param>
/// <param name="progress">The progress.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
private async Task UpdateSeries(List<string> seriesIds, string seriesDataPath, long? lastTvDbUpdateTime, IProgress<double> progress, CancellationToken cancellationToken)
{
var numComplete = 0;
var seriesList = _libraryManager.GetItemList(new InternalItemsQuery()
{
IncludeItemTypes = new[] { typeof(Series).Name },
Recursive = true,
GroupByPresentationUniqueKey = false,
DtoOptions = new DtoOptions(false)
{
EnableImages = false
}
}).Cast<Series>();
// Gather all series into a lookup by tvdb id
var allSeries = seriesList
.Where(i => !string.IsNullOrEmpty(i.GetProviderId(MetadataProviders.Tvdb)))
.ToLookup(i => i.GetProviderId(MetadataProviders.Tvdb));
foreach (var seriesId in seriesIds)
{
// Find the preferred language(s) for the movie in the library
var languages = allSeries[seriesId]
.Select(i => i.GetPreferredMetadataLanguage())
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
foreach (var language in languages)
{
try
{
await UpdateSeries(seriesId, seriesDataPath, lastTvDbUpdateTime, language, cancellationToken).ConfigureAwait(false);
}
catch (HttpException ex)
{
_logger.LogError(ex, "Error updating tvdb series id {ID}, language {Language}", seriesId, language);
// Already logged at lower levels, but don't fail the whole operation, unless timed out
// We have to fail this to make it run again otherwise new episode data could potentially be missing
if (ex.IsTimedOut)
{
throw;
}
}
}
numComplete++;
double percent = numComplete;
percent /= seriesIds.Count;
percent *= 100;
progress.Report(percent);
}
}
/// <summary>
/// Updates the series.
/// </summary>
/// <param name="id">The id.</param>
/// <param name="seriesDataPath">The series data path.</param>
/// <param name="lastTvDbUpdateTime">The last tv db update time.</param>
/// <param name="preferredMetadataLanguage">The preferred metadata language.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
private Task UpdateSeries(string id, string seriesDataPath, long? lastTvDbUpdateTime, string preferredMetadataLanguage, CancellationToken cancellationToken)
{
_logger.LogInformation("Updating series from tvdb " + id + ", language " + preferredMetadataLanguage);
seriesDataPath = Path.Combine(seriesDataPath, id);
Directory.CreateDirectory(seriesDataPath);
return TvdbSeriesProvider.Current.DownloadSeriesZip(id, MetadataProviders.Tvdb.ToString(), null, null, seriesDataPath, lastTvDbUpdateTime, preferredMetadataLanguage, cancellationToken);
}
}
}

@ -1,41 +1,32 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Providers;
using MediaBrowser.Model.Xml;
using Microsoft.Extensions.Logging;
using TvDbSharper;
using TvDbSharper.Dto;
using RatingType = MediaBrowser.Model.Dto.RatingType;
namespace MediaBrowser.Providers.TV.TheTVDB
{
public class TvdbSeasonImageProvider : IRemoteImageProvider, IHasOrder
{
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
private readonly IServerConfigurationManager _config;
private readonly IHttpClient _httpClient;
private readonly IFileSystem _fileSystem;
private readonly IXmlReaderSettingsFactory _xmlSettings;
private readonly ILogger _logger;
private readonly TvDbClientManager _tvDbClientManager;
public TvdbSeasonImageProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem, IXmlReaderSettingsFactory xmlSettings)
public TvdbSeasonImageProvider(IHttpClient httpClient, ILogger<TvdbSeasonImageProvider> logger, TvDbClientManager tvDbClientManager)
{
_config = config;
_httpClient = httpClient;
_fileSystem = fileSystem;
_xmlSettings = xmlSettings;
_logger = logger;
_tvDbClientManager = tvDbClientManager;
}
public string Name => ProviderName;
@ -62,91 +53,66 @@ namespace MediaBrowser.Providers.TV.TheTVDB
var season = (Season)item;
var series = season.Series;
if (series != null && season.IndexNumber.HasValue && TvdbSeriesProvider.IsValidSeries(series.ProviderIds))
if (series == null || !season.IndexNumber.HasValue || !TvdbSeriesProvider.IsValidSeries(series.ProviderIds))
{
var seriesProviderIds = series.ProviderIds;
var seasonNumber = season.IndexNumber.Value;
return new RemoteImageInfo[] { };
}
var seriesDataPath = await TvdbSeriesProvider.Current.EnsureSeriesInfo(seriesProviderIds, series.Name, series.ProductionYear, series.GetPreferredMetadataLanguage(), cancellationToken).ConfigureAwait(false);
var tvdbId = Convert.ToInt32(series.GetProviderId(MetadataProviders.Tvdb));
var seasonNumber = season.IndexNumber.Value;
var language = item.GetPreferredMetadataLanguage();
var remoteImages = new List<RemoteImageInfo>();
if (!string.IsNullOrEmpty(seriesDataPath))
var keyTypes = new[] { KeyType.Season, KeyType.Seasonwide, KeyType.Fanart };
foreach (var keyType in keyTypes)
{
var imageQuery = new ImagesQuery
{
var path = Path.Combine(seriesDataPath, "banners.xml");
try
{
return GetImages(path, item.GetPreferredMetadataLanguage(), seasonNumber, _xmlSettings, _fileSystem, cancellationToken);
}
catch (FileNotFoundException)
{
// No tvdb data yet. Don't blow up
}
catch (IOException)
{
// No tvdb data yet. Don't blow up
}
KeyType = keyType,
SubKey = seasonNumber.ToString()
};
try
{
var imageResults = await _tvDbClientManager
.GetImagesAsync(tvdbId, imageQuery, language, cancellationToken).ConfigureAwait(false);
remoteImages.AddRange(GetImages(imageResults.Data, language));
}
catch (TvDbServerException)
{
_logger.LogDebug("No images of type {KeyType} found for series {TvdbId}", keyType, tvdbId);
}
}
return new RemoteImageInfo[] { };
return remoteImages;
}
internal static IEnumerable<RemoteImageInfo> GetImages(string xmlPath, string preferredLanguage, int seasonNumber, IXmlReaderSettingsFactory xmlReaderSettingsFactory, IFileSystem fileSystem, CancellationToken cancellationToken)
private IEnumerable<RemoteImageInfo> GetImages(Image[] images, string preferredLanguage)
{
var settings = xmlReaderSettingsFactory.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
var list = new List<RemoteImageInfo>();
using (var fileStream = fileSystem.GetFileStream(xmlPath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
var languages = _tvDbClientManager.GetLanguagesAsync(CancellationToken.None).Result.Data;
foreach (Image image in images)
{
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
var imageInfo = new RemoteImageInfo
{
// Use XmlReader for best performance
using (var reader = XmlReader.Create(streamReader, settings))
{
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
{
cancellationToken.ThrowIfCancellationRequested();
RatingType = RatingType.Score,
CommunityRating = (double?)image.RatingsInfo.Average,
VoteCount = image.RatingsInfo.Count,
Url = TvdbUtils.BannerUrl + image.FileName,
ProviderName = ProviderName,
Language = languages.FirstOrDefault(lang => lang.Id == image.LanguageId)?.Abbreviation,
ThumbnailUrl = TvdbUtils.BannerUrl + image.Thumbnail
};
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "Banner":
{
if (reader.IsEmptyElement)
{
reader.Read();
continue;
}
using (var subtree = reader.ReadSubtree())
{
AddImage(subtree, list, seasonNumber);
}
break;
}
default:
reader.Skip();
break;
}
}
else
{
reader.Read();
}
}
}
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 =>
@ -155,6 +121,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
{
return 3;
}
if (!isLanguageEn)
{
if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
@ -162,177 +129,18 @@ namespace MediaBrowser.Providers.TV.TheTVDB
return 2;
}
}
if (string.IsNullOrEmpty(i.Language))
{
return isLanguageEn ? 3 : 2;
}
return 0;
})
.ThenByDescending(i => i.CommunityRating ?? 0)
.ThenByDescending(i => i.VoteCount ?? 0);
}
private static void AddImage(XmlReader reader, List<RemoteImageInfo> images, int seasonNumber)
{
reader.MoveToContent();
string bannerType = null;
string bannerType2 = null;
string url = null;
int? bannerSeason = null;
int? width = null;
int? height = null;
string language = null;
double? rating = null;
int? voteCount = null;
string thumbnailUrl = null;
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "Rating":
{
var val = reader.ReadElementContentAsString() ?? string.Empty;
if (double.TryParse(val, NumberStyles.Any, UsCulture, out var rval))
{
rating = rval;
}
break;
}
case "RatingCount":
{
var val = reader.ReadElementContentAsString() ?? string.Empty;
if (int.TryParse(val, NumberStyles.Integer, UsCulture, out var rval))
{
voteCount = rval;
}
break;
}
case "Language":
{
language = reader.ReadElementContentAsString() ?? string.Empty;
break;
}
case "ThumbnailPath":
{
thumbnailUrl = reader.ReadElementContentAsString() ?? string.Empty;
break;
}
case "BannerType":
{
bannerType = reader.ReadElementContentAsString() ?? string.Empty;
break;
}
case "BannerType2":
{
bannerType2 = reader.ReadElementContentAsString() ?? string.Empty;
// Sometimes the resolution is stuffed in here
var resolutionParts = bannerType2.Split('x');
if (resolutionParts.Length == 2)
{
if (int.TryParse(resolutionParts[0], NumberStyles.Integer, UsCulture, out var rval))
{
width = rval;
}
if (int.TryParse(resolutionParts[1], NumberStyles.Integer, UsCulture, out rval))
{
height = rval;
}
}
break;
}
case "BannerPath":
{
url = reader.ReadElementContentAsString() ?? string.Empty;
break;
}
case "Season":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
bannerSeason = int.Parse(val);
}
break;
}
default:
reader.Skip();
break;
}
}
else
{
reader.Read();
}
}
if (!string.IsNullOrEmpty(url) && bannerSeason.HasValue && bannerSeason.Value == seasonNumber)
{
var imageInfo = new RemoteImageInfo
{
RatingType = RatingType.Score,
CommunityRating = rating,
VoteCount = voteCount,
Url = TVUtils.BannerUrl + url,
ProviderName = ProviderName,
Language = language,
Width = width,
Height = height
};
if (!string.IsNullOrEmpty(thumbnailUrl))
{
imageInfo.ThumbnailUrl = TVUtils.BannerUrl + thumbnailUrl;
}
if (string.Equals(bannerType, "season", StringComparison.OrdinalIgnoreCase))
{
if (string.Equals(bannerType2, "season", StringComparison.OrdinalIgnoreCase))
{
imageInfo.Type = ImageType.Primary;
images.Add(imageInfo);
}
else if (string.Equals(bannerType2, "seasonwide", StringComparison.OrdinalIgnoreCase))
{
imageInfo.Type = ImageType.Banner;
images.Add(imageInfo);
}
}
else if (string.Equals(bannerType, "fanart", StringComparison.OrdinalIgnoreCase))
{
imageInfo.Type = ImageType.Backdrop;
images.Add(imageInfo);
}
}
}
public int Order => 0;
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)

@ -1,40 +1,32 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Providers;
using MediaBrowser.Model.Xml;
using Microsoft.Extensions.Logging;
using TvDbSharper;
using TvDbSharper.Dto;
using RatingType = MediaBrowser.Model.Dto.RatingType;
using Series = MediaBrowser.Controller.Entities.TV.Series;
namespace MediaBrowser.Providers.TV.TheTVDB
{
public class TvdbSeriesImageProvider : IRemoteImageProvider, IHasOrder
{
private readonly IServerConfigurationManager _config;
private readonly IHttpClient _httpClient;
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private readonly IFileSystem _fileSystem;
private readonly IXmlReaderSettingsFactory _xmlReaderSettingsFactory;
private readonly ILogger _logger;
private readonly TvDbClientManager _tvDbClientManager;
public TvdbSeriesImageProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem, IXmlReaderSettingsFactory xmlReaderSettingsFactory)
public TvdbSeriesImageProvider(IHttpClient httpClient, ILogger<TvdbSeriesImageProvider> logger, TvDbClientManager tvDbClientManager)
{
_config = config;
_httpClient = httpClient;
_fileSystem = fileSystem;
_xmlReaderSettingsFactory = xmlReaderSettingsFactory;
_logger = logger;
_tvDbClientManager = tvDbClientManager;
}
public string Name => ProviderName;
@ -58,273 +50,92 @@ namespace MediaBrowser.Providers.TV.TheTVDB
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
{
if (TvdbSeriesProvider.IsValidSeries(item.ProviderIds))
if (!TvdbSeriesProvider.IsValidSeries(item.ProviderIds))
{
var language = item.GetPreferredMetadataLanguage();
var seriesDataPath = await TvdbSeriesProvider.Current.EnsureSeriesInfo(item.ProviderIds, item.Name, item.ProductionYear, language, cancellationToken).ConfigureAwait(false);
return Array.Empty<RemoteImageInfo>();
}
if (string.IsNullOrEmpty(seriesDataPath))
var language = item.GetPreferredMetadataLanguage();
var remoteImages = new List<RemoteImageInfo>();
var keyTypes = new[] { KeyType.Poster, KeyType.Series, KeyType.Fanart };
var tvdbId = Convert.ToInt32(item.GetProviderId(MetadataProviders.Tvdb));
foreach (KeyType keyType in keyTypes)
{
var imageQuery = new ImagesQuery
{
return new RemoteImageInfo[] { };
}
var path = Path.Combine(seriesDataPath, "banners.xml");
KeyType = keyType
};
try
{
return GetImages(path, language, cancellationToken);
}
catch (FileNotFoundException)
{
// No tvdb data yet. Don't blow up
var imageResults =
await _tvDbClientManager.GetImagesAsync(tvdbId, imageQuery, language, cancellationToken)
.ConfigureAwait(false);
remoteImages.AddRange(GetImages(imageResults.Data, language));
}
catch (IOException)
catch (TvDbServerException)
{
// No tvdb data yet. Don't blow up
_logger.LogDebug("No images of type {KeyType} exist for series {TvDbId}", keyType,
tvdbId);
}
}
return new RemoteImageInfo[] { };
return remoteImages;
}
private IEnumerable<RemoteImageInfo> GetImages(string xmlPath, string preferredLanguage, CancellationToken cancellationToken)
private IEnumerable<RemoteImageInfo> GetImages(Image[] images, string preferredLanguage)
{
var settings = _xmlReaderSettingsFactory.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
var list = new List<RemoteImageInfo>();
var languages = _tvDbClientManager.GetLanguagesAsync(CancellationToken.None).Result.Data;
using (var fileStream = _fileSystem.GetFileStream(xmlPath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
foreach (Image image in images)
{
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
var imageInfo = new RemoteImageInfo
{
// Use XmlReader for best performance
using (var reader = XmlReader.Create(streamReader, settings))
{
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
{
cancellationToken.ThrowIfCancellationRequested();
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
};
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "Banner":
{
if (reader.IsEmptyElement)
{
reader.Read();
continue;
}
using (var subtree = reader.ReadSubtree())
{
AddImage(subtree, list);
}
break;
}
default:
reader.Skip();
break;
}
}
else
{
reader.Read();
}
}
}
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))
if (string.Equals(preferredLanguage, i.Language, StringComparison.OrdinalIgnoreCase))
{
return 2;
return 3;
}
}
if (string.IsNullOrEmpty(i.Language))
{
return isLanguageEn ? 3 : 2;
}
return 0;
})
.ThenByDescending(i => i.CommunityRating ?? 0)
.ThenByDescending(i => i.VoteCount ?? 0);
}
private void AddImage(XmlReader reader, List<RemoteImageInfo> images)
{
reader.MoveToContent();
string bannerType = null;
string url = null;
int? bannerSeason = null;
int? width = null;
int? height = null;
string language = null;
double? rating = null;
int? voteCount = null;
string thumbnailUrl = null;
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
if (!isLanguageEn)
{
case "Rating":
{
var val = reader.ReadElementContentAsString() ?? string.Empty;
if (double.TryParse(val, NumberStyles.Any, _usCulture, out var rval))
{
rating = rval;
}
break;
}
case "RatingCount":
{
var val = reader.ReadElementContentAsString() ?? string.Empty;
if (int.TryParse(val, NumberStyles.Integer, _usCulture, out var rval))
{
voteCount = rval;
}
break;
}
case "Language":
{
language = reader.ReadElementContentAsString() ?? string.Empty;
break;
}
case "ThumbnailPath":
{
thumbnailUrl = reader.ReadElementContentAsString() ?? string.Empty;
break;
}
case "BannerType":
{
bannerType = reader.ReadElementContentAsString() ?? string.Empty;
break;
}
case "BannerPath":
{
url = reader.ReadElementContentAsString() ?? string.Empty;
break;
}
case "BannerType2":
{
var bannerType2 = reader.ReadElementContentAsString() ?? string.Empty;
// Sometimes the resolution is stuffed in here
var resolutionParts = bannerType2.Split('x');
if (resolutionParts.Length == 2)
{
if (int.TryParse(resolutionParts[0], NumberStyles.Integer, _usCulture, out var rval))
{
width = rval;
}
if (int.TryParse(resolutionParts[1], NumberStyles.Integer, _usCulture, out rval))
{
height = rval;
}
}
break;
}
case "Season":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
bannerSeason = int.Parse(val);
}
break;
}
default:
reader.Skip();
break;
if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
{
return 2;
}
}
}
else
{
reader.Read();
}
}
if (!string.IsNullOrEmpty(url) && !bannerSeason.HasValue)
{
var imageInfo = new RemoteImageInfo
{
RatingType = RatingType.Score,
CommunityRating = rating,
VoteCount = voteCount,
Url = TVUtils.BannerUrl + url,
ProviderName = Name,
Language = language,
Width = width,
Height = height
};
if (!string.IsNullOrEmpty(thumbnailUrl))
{
imageInfo.ThumbnailUrl = TVUtils.BannerUrl + thumbnailUrl;
}
if (string.Equals(bannerType, "poster", StringComparison.OrdinalIgnoreCase))
{
imageInfo.Type = ImageType.Primary;
images.Add(imageInfo);
}
else if (string.Equals(bannerType, "series", StringComparison.OrdinalIgnoreCase))
{
imageInfo.Type = ImageType.Banner;
images.Add(imageInfo);
}
else if (string.Equals(bannerType, "fanart", StringComparison.OrdinalIgnoreCase))
{
imageInfo.Type = ImageType.Backdrop;
images.Add(imageInfo);
}
}
if (string.IsNullOrEmpty(i.Language))
{
return isLanguageEn ? 3 : 2;
}
return 0;
})
.ThenByDescending(i => i.CommunityRating ?? 0)
.ThenByDescending(i => i.VoteCount ?? 0);
}
public int Order => 0;

File diff suppressed because it is too large Load Diff

@ -0,0 +1,36 @@
using System;
using System.ComponentModel;
using MediaBrowser.Model.Entities;
namespace MediaBrowser.Providers.TV.TheTVDB
{
public static class TvdbUtils
{
public const string TvdbApiKey = "OG4V3YJ3FAP7FP2K";
public const string TvdbBaseUrl = "https://www.thetvdb.com/";
public const 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();
}
}
}

@ -25,7 +25,7 @@ namespace MediaBrowser.Providers.TV
public string Key => MetadataProviders.Tvdb.ToString();
public string UrlFormatString => TvdbPrescanTask.TvdbBaseUrl + "?tab=series&id={0}";
public string UrlFormatString => TvdbUtils.TvdbBaseUrl + "?tab=series&id={0}";
public bool Supports(IHasProviderIds item)
{
@ -53,7 +53,7 @@ namespace MediaBrowser.Providers.TV
public string Key => MetadataProviders.Tvdb.ToString();
public string UrlFormatString => TvdbPrescanTask.TvdbBaseUrl + "?tab=episode&id={0}";
public string UrlFormatString => TvdbUtils.TvdbBaseUrl + "?tab=episode&id={0}";
public bool Supports(IHasProviderIds item)
{

Loading…
Cancel
Save