2014-01-28 19:37:01 +01:00
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-11-05 17:08:44 +01:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Providers;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
using MediaBrowser.Providers.TV;
|
2013-11-05 17:08:44 +01:00
|
|
|
|
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;
|
|
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
|
namespace MediaBrowser.Providers.People
|
2013-11-05 17:08:44 +01:00
|
|
|
|
{
|
2014-01-31 20:55:21 +01:00
|
|
|
|
public class TvdbPersonImageProvider : IRemoteImageProvider, IHasOrder
|
2013-11-05 17:08:44 +01:00
|
|
|
|
{
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2015-06-21 05:35:22 +02:00
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
private readonly IHttpClient _httpClient;
|
2013-11-05 17:08:44 +01:00
|
|
|
|
|
2015-06-21 05:35:22 +02:00
|
|
|
|
public TvdbPersonImageProvider(IServerConfigurationManager config, ILibraryManager libraryManager, IHttpClient httpClient)
|
2013-11-05 17:08:44 +01:00
|
|
|
|
{
|
|
|
|
|
_config = config;
|
2015-06-21 05:35:22 +02:00
|
|
|
|
_libraryManager = libraryManager;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
_httpClient = httpClient;
|
2013-11-05 17:08:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return ProviderName; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ProviderName
|
|
|
|
|
{
|
|
|
|
|
get { return "TheTVDB"; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-21 19:37:34 +01:00
|
|
|
|
public bool Supports(IHasImages item)
|
2013-11-05 17:08:44 +01:00
|
|
|
|
{
|
|
|
|
|
return item is Person;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
|
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
|
|
|
|
|
{
|
|
|
|
|
return new List<ImageType>
|
|
|
|
|
{
|
|
|
|
|
ImageType.Primary
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 06:11:54 +01:00
|
|
|
|
public Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, CancellationToken cancellationToken)
|
2013-11-05 17:08:44 +01:00
|
|
|
|
{
|
2015-01-25 07:34:50 +01:00
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var itemName = item.Name;
|
|
|
|
|
|
2015-07-07 04:25:23 +02:00
|
|
|
|
var seriesWithPerson = _libraryManager.GetItems(new InternalItemsQuery
|
|
|
|
|
{
|
|
|
|
|
IncludeItemTypes = new[] { typeof(Series).Name },
|
|
|
|
|
Person = itemName
|
|
|
|
|
|
|
|
|
|
}).Items.Cast<Series>()
|
|
|
|
|
.Where(i => !string.IsNullOrEmpty(i.GetProviderId(MetadataProviders.Tvdb)))
|
2013-11-05 17:08:44 +01:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var infos = seriesWithPerson.Select(i => GetImageFromSeriesData(i, item.Name, cancellationToken))
|
|
|
|
|
.Where(i => i != null)
|
|
|
|
|
.Take(1);
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(infos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RemoteImageInfo GetImageFromSeriesData(Series series, string personName, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var tvdbPath = TvdbSeriesProvider.GetSeriesDataPath(_config.ApplicationPaths, series.GetProviderId(MetadataProviders.Tvdb));
|
|
|
|
|
|
|
|
|
|
var actorXmlPath = Path.Combine(tvdbPath, "actors.xml");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return GetImageInfo(actorXmlPath, personName, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-12-23 04:58:14 +01:00
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2013-11-05 17:08:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RemoteImageInfo GetImageInfo(string xmlFile, string personName, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var settings = new XmlReaderSettings
|
|
|
|
|
{
|
|
|
|
|
CheckCharacters = false,
|
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
|
IgnoreComments = true,
|
|
|
|
|
ValidationType = ValidationType.None
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using (var streamReader = new StreamReader(xmlFile, Encoding.UTF8))
|
|
|
|
|
{
|
|
|
|
|
// Use XmlReader for best performance
|
|
|
|
|
using (var reader = XmlReader.Create(streamReader, settings))
|
|
|
|
|
{
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "Actor":
|
|
|
|
|
{
|
|
|
|
|
using (var subtree = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
var info = FetchImageInfoFromActorNode(personName, subtree);
|
|
|
|
|
|
|
|
|
|
if (info != null)
|
|
|
|
|
{
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(image) &&
|
|
|
|
|
string.Equals(name, personName, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return new RemoteImageInfo
|
|
|
|
|
{
|
|
|
|
|
Url = TVUtils.BannerUrl + image,
|
|
|
|
|
Type = ImageType.Primary,
|
|
|
|
|
ProviderName = Name
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
|
public int Order
|
2013-11-05 17:08:44 +01:00
|
|
|
|
{
|
2014-01-28 19:37:01 +01:00
|
|
|
|
get { return 1; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = TvdbSeriesProvider.Current.TvDbResourcePool
|
|
|
|
|
});
|
2013-11-05 17:08:44 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|