forked from MediaBrowser/Emby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTvdbPersonImageProvider.cs
127 lines (107 loc) · 4.51 KB
/
TvdbPersonImageProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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 MediaBrowser.Providers.Extensions;
using System;
using System.IO;
using System.Linq;
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;
}
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");
var xmlDoc = new XmlDocument();
xmlDoc.Load(actorXmlPath);
var actorNodes = xmlDoc.SelectNodes("//Actor");
if (actorNodes == null)
{
return;
}
foreach (var actorNode in actorNodes.OfType<XmlNode>())
{
var name = actorNode.SafeGetString("Name");
if (string.Equals(item.Name, name, StringComparison.OrdinalIgnoreCase))
{
var image = actorNode.SafeGetString("Image");
if (!string.IsNullOrEmpty(image))
{
var url = TVUtils.BannerUrl + image;
await _providerManager.SaveImage(item, url, RemoteSeriesProvider.Current.TvDbResourcePool,
ImageType.Primary, null, cancellationToken).ConfigureAwait(false);
}
break;
}
}
}
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.Third; }
}
}
}