jellyfin-server/Emby.Server.Implementations/Library/LocalTrailerPostScanTask.cs

106 lines
3.6 KiB
C#
Raw Normal View History

2015-01-25 06:34:50 +00:00
using MediaBrowser.Controller.Channels;
2014-12-11 06:20:28 +00:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2017-05-21 07:25:49 +00:00
using MediaBrowser.Controller.Dto;
2016-05-10 19:20:17 +00:00
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
2014-12-11 06:20:28 +00:00
namespace Emby.Server.Implementations.Library
2014-12-11 06:20:28 +00:00
{
public class LocalTrailerPostScanTask : ILibraryPostScanTask
{
private readonly ILibraryManager _libraryManager;
private readonly IChannelManager _channelManager;
public LocalTrailerPostScanTask(ILibraryManager libraryManager, IChannelManager channelManager)
{
_libraryManager = libraryManager;
_channelManager = channelManager;
}
public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
{
2016-05-10 19:20:17 +00:00
var items = _libraryManager.GetItemList(new InternalItemsQuery
{
IncludeItemTypes = new[] { typeof(BoxSet).Name, typeof(Game).Name, typeof(Movie).Name, typeof(Series).Name },
2017-05-21 07:25:49 +00:00
Recursive = true,
DtoOptions = new DtoOptions(true)
2016-05-10 19:20:17 +00:00
}).OfType<IHasTrailers>().ToList();
2014-12-11 06:20:28 +00:00
2016-04-30 03:59:46 +00:00
var trailerTypes = Enum.GetNames(typeof(TrailerType))
.Select(i => (TrailerType)Enum.Parse(typeof(TrailerType), i, true))
.Except(new[] { TrailerType.LocalTrailer })
.ToArray();
2016-03-20 06:46:51 +00:00
var trailers = _libraryManager.GetItemList(new InternalItemsQuery
2014-12-11 06:20:28 +00:00
{
2016-03-19 15:38:05 +00:00
IncludeItemTypes = new[] { typeof(Trailer).Name },
2016-05-10 19:20:17 +00:00
TrailerTypes = trailerTypes,
2017-05-21 07:25:49 +00:00
Recursive = true,
DtoOptions = new DtoOptions(false)
2016-04-30 03:59:46 +00:00
2016-03-20 06:46:51 +00:00
}).ToArray();
2014-12-11 06:20:28 +00:00
var numComplete = 0;
foreach (var item in items)
{
cancellationToken.ThrowIfCancellationRequested();
2016-03-19 15:38:05 +00:00
await AssignTrailers(item, trailers).ConfigureAwait(false);
2014-12-11 06:20:28 +00:00
numComplete++;
double percent = numComplete;
percent /= items.Count;
progress.Report(percent * 100);
}
progress.Report(100);
}
private async Task AssignTrailers(IHasTrailers item, BaseItem[] channelTrailers)
{
if (item is Game)
{
return;
}
var imdbId = item.GetProviderId(MetadataProviders.Imdb);
var tmdbId = item.GetProviderId(MetadataProviders.Tmdb);
var trailers = channelTrailers.Where(i =>
{
if (!string.IsNullOrWhiteSpace(imdbId) &&
string.Equals(imdbId, i.GetProviderId(MetadataProviders.Imdb), StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (!string.IsNullOrWhiteSpace(tmdbId) &&
string.Equals(tmdbId, i.GetProviderId(MetadataProviders.Tmdb), StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
});
var trailerIds = trailers.Select(i => i.Id)
.ToList();
if (!trailerIds.SequenceEqual(item.RemoteTrailerIds))
{
item.RemoteTrailerIds = trailerIds;
var baseItem = (BaseItem)item;
await baseItem.UpdateToRepository(ItemUpdateType.MetadataImport, CancellationToken.None)
.ConfigureAwait(false);
}
}
}
}