jellyfin/MediaBrowser.Providers/TV/TvdbPersonImageProvider.cs

228 lines
7.7 KiB
C#
Raw Normal View History

2013-07-30 19:21:16 +00:00
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.Logging;
using System;
using System.IO;
using System.Linq;
2013-09-15 17:34:48 +00:00
using System.Text;
2013-07-30 19:21:16 +00:00
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
namespace MediaBrowser.Providers.TV
{
public class TvdbPersonImageProvider : BaseMetadataProvider
{
private readonly ILibraryManager _library;
private readonly IProviderManager _providerManager;
public TvdbPersonImageProvider(ILogManager logManager, IServerConfigurationManager configurationManager, ILibraryManager library, IProviderManager providerManager)
: base(logManager, configurationManager)
{
_library = library;
_providerManager = providerManager;
}
2013-09-16 17:52:14 +00:00
protected override bool RefreshOnVersionChange
{
get
{
return true;
}
}
protected override string ProviderVersion
{
get
{
return "2";
}
}
public override bool RequiresInternet
{
get
{
return true;
}
}
2013-07-30 19:21:16 +00:00
public override bool Supports(BaseItem item)
{
return item is Person;
}
/// <summary>
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
/// </summary>
/// <param name="item">The item.</param>
/// <param name="force">if set to <c>true</c> [force].</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{System.Boolean}.</returns>
public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(item.PrimaryImagePath))
{
var seriesWithPerson = _library.RootFolder
.RecursiveChildren
.OfType<Series>()
.Where(i => !string.IsNullOrEmpty(i.GetProviderId(MetadataProviders.Tvdb)) && i.People.Any(p => string.Equals(p.Name, item.Name, StringComparison.OrdinalIgnoreCase)))
.ToList();
foreach (var series in seriesWithPerson)
{
try
{
await DownloadImageFromSeries(item, series, cancellationToken).ConfigureAwait(false);
}
catch (FileNotFoundException)
{
// No biggie
continue;
}
// break once we have an image
if (!string.IsNullOrEmpty(item.PrimaryImagePath))
{
break;
}
}
}
SetLastRefreshed(item, DateTime.UtcNow);
return true;
}
/// <summary>
/// Downloads the image from series.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="series">The series.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
private async Task DownloadImageFromSeries(BaseItem item, Series series, CancellationToken cancellationToken)
{
var tvdbPath = RemoteSeriesProvider.GetSeriesDataPath(ConfigurationManager.ApplicationPaths, series.GetProviderId(MetadataProviders.Tvdb));
var actorXmlPath = Path.Combine(tvdbPath, "actors.xml");
2013-09-15 17:34:48 +00:00
var url = FetchImageUrl(item, actorXmlPath, cancellationToken);
2013-07-30 19:21:16 +00:00
2013-09-15 17:34:48 +00:00
if (!string.IsNullOrEmpty(url))
2013-07-30 19:21:16 +00:00
{
2013-09-15 17:34:48 +00:00
url = TVUtils.BannerUrl + url;
2013-07-30 19:21:16 +00:00
2013-09-15 17:34:48 +00:00
await _providerManager.SaveImage(item, url, RemoteSeriesProvider.Current.TvDbResourcePool,
ImageType.Primary, null, cancellationToken).ConfigureAwait(false);
}
}
private string FetchImageUrl(BaseItem item, string actorsXmlPath, CancellationToken cancellationToken)
{
var settings = new XmlReaderSettings
2013-07-30 19:21:16 +00:00
{
2013-09-15 17:34:48 +00:00
CheckCharacters = false,
IgnoreProcessingInstructions = true,
IgnoreComments = true,
ValidationType = ValidationType.None
};
2013-07-30 19:21:16 +00:00
2013-09-15 17:34:48 +00:00
using (var streamReader = new StreamReader(actorsXmlPath, Encoding.UTF8))
{
// Use XmlReader for best performance
using (var reader = XmlReader.Create(streamReader, settings))
2013-07-30 19:21:16 +00:00
{
2013-09-15 17:34:48 +00:00
reader.MoveToContent();
2013-07-30 19:21:16 +00:00
2013-09-15 17:34:48 +00:00
// Loop through each element
while (reader.Read())
2013-07-30 19:21:16 +00:00
{
2013-09-15 17:34:48 +00:00
cancellationToken.ThrowIfCancellationRequested();
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "Actor":
{
using (var subtree = reader.ReadSubtree())
{
var url = FetchImageUrlFromActorNode(item, subtree);
if (!string.IsNullOrEmpty(url))
{
return url;
}
}
break;
}
default:
reader.Skip();
break;
}
}
2013-07-30 19:21:16 +00:00
}
2013-09-15 17:34:48 +00:00
}
}
2013-07-30 19:21:16 +00:00
2013-09-15 17:34:48 +00:00
return null;
}
/// <summary>
/// Fetches the data from actor node.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="reader">The reader.</param>
private string FetchImageUrlFromActorNode(BaseItem item, XmlReader reader)
{
reader.MoveToContent();
string name = null;
string image = null;
while (reader.Read())
{
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;
}
2013-07-30 19:21:16 +00:00
}
}
2013-09-15 17:34:48 +00:00
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(image) &&
string.Equals(name, item.Name, StringComparison.OrdinalIgnoreCase))
{
return image;
}
return null;
2013-07-30 19:21:16 +00:00
}
2013-09-15 17:34:48 +00:00
2013-07-30 19:21:16 +00:00
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.Third; }
}
}
}