2014-01-28 11:37:01 -07:00
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-11-05 09:08:44 -07: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 11:37:01 -07:00
|
|
|
|
using MediaBrowser.Providers.TV;
|
2013-11-05 09:08:44 -07: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;
|
2017-05-21 00:25:49 -07:00
|
|
|
|
using MediaBrowser.Controller.Dto;
|
2016-10-27 11:30:20 -07:00
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
|
using MediaBrowser.Model.Xml;
|
2013-11-05 09:08:44 -07:00
|
|
|
|
|
2014-01-28 11:37:01 -07:00
|
|
|
|
namespace MediaBrowser.Providers.People
|
2013-11-05 09:08:44 -07:00
|
|
|
|
{
|
2014-01-31 12:55:21 -07:00
|
|
|
|
public class TvdbPersonImageProvider : IRemoteImageProvider, IHasOrder
|
2013-11-05 09:08:44 -07:00
|
|
|
|
{
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2015-06-20 20:35:22 -07:00
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2014-01-28 11:37:01 -07:00
|
|
|
|
private readonly IHttpClient _httpClient;
|
2016-10-27 11:30:20 -07:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
private readonly IXmlReaderSettingsFactory _xmlSettings;
|
2013-11-05 09:08:44 -07:00
|
|
|
|
|
2016-10-27 11:30:20 -07:00
|
|
|
|
public TvdbPersonImageProvider(IServerConfigurationManager config, ILibraryManager libraryManager, IHttpClient httpClient, IFileSystem fileSystem, IXmlReaderSettingsFactory xmlSettings)
|
2013-11-05 09:08:44 -07:00
|
|
|
|
{
|
|
|
|
|
_config = config;
|
2015-06-20 20:35:22 -07:00
|
|
|
|
_libraryManager = libraryManager;
|
2014-01-28 11:37:01 -07:00
|
|
|
|
_httpClient = httpClient;
|
2016-10-27 11:30:20 -07:00
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
|
_xmlSettings = xmlSettings;
|
2013-11-05 09:08:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return ProviderName; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ProviderName
|
|
|
|
|
{
|
|
|
|
|
get { return "TheTVDB"; }
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
public bool Supports(BaseItem item)
|
2013-11-05 09:08:44 -07:00
|
|
|
|
{
|
|
|
|
|
return item is Person;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
2014-01-28 11:37:01 -07:00
|
|
|
|
{
|
|
|
|
|
return new List<ImageType>
|
|
|
|
|
{
|
|
|
|
|
ImageType.Primary
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
public Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
|
2013-11-05 09:08:44 -07:00
|
|
|
|
{
|
2016-03-19 23:46:51 -07:00
|
|
|
|
var seriesWithPerson = _libraryManager.GetItemList(new InternalItemsQuery
|
2015-07-06 19:25:23 -07:00
|
|
|
|
{
|
|
|
|
|
IncludeItemTypes = new[] { typeof(Series).Name },
|
2018-09-12 10:26:21 -07:00
|
|
|
|
PersonIds = new[] { item.Id },
|
2017-05-21 00:25:49 -07:00
|
|
|
|
DtoOptions = new DtoOptions(false)
|
|
|
|
|
{
|
|
|
|
|
EnableImages = false
|
|
|
|
|
}
|
2015-07-06 19:25:23 -07:00
|
|
|
|
|
2016-03-19 23:46:51 -07:00
|
|
|
|
}).Cast<Series>()
|
2015-11-24 11:45:52 -07:00
|
|
|
|
.Where(i => TvdbSeriesProvider.IsValidSeries(i.ProviderIds))
|
2013-11-05 09:08:44 -07: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)
|
|
|
|
|
{
|
2015-11-24 11:45:52 -07:00
|
|
|
|
var tvdbPath = TvdbSeriesProvider.GetSeriesDataPath(_config.ApplicationPaths, series.ProviderIds);
|
2013-11-05 09:08:44 -07:00
|
|
|
|
|
|
|
|
|
var actorXmlPath = Path.Combine(tvdbPath, "actors.xml");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return GetImageInfo(actorXmlPath, personName, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-10-27 11:30:20 -07:00
|
|
|
|
catch (IOException)
|
2014-12-22 20:58:14 -07:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2013-11-05 09:08:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RemoteImageInfo GetImageInfo(string xmlFile, string personName, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2016-10-27 11:30:20 -07:00
|
|
|
|
var settings = _xmlSettings.Create(false);
|
2013-11-05 09:08:44 -07:00
|
|
|
|
|
2016-10-27 11:30:20 -07:00
|
|
|
|
settings.CheckCharacters = false;
|
|
|
|
|
settings.IgnoreProcessingInstructions = true;
|
|
|
|
|
settings.IgnoreComments = true;
|
|
|
|
|
|
|
|
|
|
using (var fileStream = _fileSystem.GetFileStream(xmlFile, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
|
2013-11-05 09:08:44 -07:00
|
|
|
|
{
|
2016-10-27 11:30:20 -07:00
|
|
|
|
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
|
2013-11-05 09:08:44 -07:00
|
|
|
|
{
|
2016-10-27 11:30:20 -07:00
|
|
|
|
// Use XmlReader for best performance
|
|
|
|
|
using (var reader = XmlReader.Create(streamReader, settings))
|
2013-11-05 09:08:44 -07:00
|
|
|
|
{
|
2016-10-27 11:30:20 -07:00
|
|
|
|
reader.MoveToContent();
|
2016-12-02 13:10:35 -07:00
|
|
|
|
reader.Read();
|
2013-11-05 09:08:44 -07:00
|
|
|
|
|
2016-10-27 11:30:20 -07:00
|
|
|
|
// Loop through each element
|
2016-12-03 14:46:06 -07:00
|
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
2013-11-05 09:08:44 -07:00
|
|
|
|
{
|
2016-10-27 11:30:20 -07:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2013-11-05 09:08:44 -07:00
|
|
|
|
{
|
2016-10-27 11:30:20 -07:00
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "Actor":
|
2013-11-05 09:08:44 -07:00
|
|
|
|
{
|
2016-12-03 16:57:34 -07:00
|
|
|
|
if (reader.IsEmptyElement)
|
|
|
|
|
{
|
|
|
|
|
reader.Read();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-10-27 11:30:20 -07:00
|
|
|
|
using (var subtree = reader.ReadSubtree())
|
2013-11-05 09:08:44 -07:00
|
|
|
|
{
|
2016-10-27 11:30:20 -07:00
|
|
|
|
var info = FetchImageInfoFromActorNode(personName, subtree);
|
|
|
|
|
|
|
|
|
|
if (info != null)
|
|
|
|
|
{
|
|
|
|
|
return info;
|
|
|
|
|
}
|
2013-11-05 09:08:44 -07:00
|
|
|
|
}
|
2016-10-27 11:30:20 -07:00
|
|
|
|
break;
|
2013-11-05 09:08:44 -07:00
|
|
|
|
}
|
2016-10-27 11:30:20 -07:00
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
2013-11-05 09:08:44 -07:00
|
|
|
|
break;
|
2016-10-27 11:30:20 -07:00
|
|
|
|
}
|
2013-11-05 09:08:44 -07:00
|
|
|
|
}
|
2016-12-02 13:10:35 -07:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
reader.Read();
|
|
|
|
|
}
|
2013-11-05 09:08:44 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2016-12-02 13:10:35 -07:00
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
reader.Read();
|
|
|
|
|
|
|
|
|
|
// Loop through each element
|
2016-12-03 14:46:06 -07:00
|
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
2013-11-05 09:08:44 -07:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-02 13:10:35 -07:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
reader.Read();
|
|
|
|
|
}
|
2013-11-05 09:08:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 11:37:01 -07:00
|
|
|
|
public int Order
|
2013-11-05 09:08:44 -07:00
|
|
|
|
{
|
2014-01-28 11:37:01 -07:00
|
|
|
|
get { return 1; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
CancellationToken = cancellationToken,
|
2016-10-31 11:39:41 -07:00
|
|
|
|
Url = url
|
2014-01-28 11:37:01 -07:00
|
|
|
|
});
|
2013-11-05 09:08:44 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|