jellyfin/Emby.Server.Implementations/LiveTv/ProgramImageProvider.cs

104 lines
3.0 KiB
C#
Raw Normal View History

2015-10-25 17:13:30 +00:00
using MediaBrowser.Controller.Entities;
2013-12-19 21:51:32 +00:00
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
2013-12-19 21:51:32 +00:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2016-11-03 22:56:28 +00:00
namespace Emby.Server.Implementations.LiveTv
2013-12-19 21:51:32 +00:00
{
2015-04-12 01:38:38 +00:00
public class ProgramImageProvider : IDynamicImageProvider, IHasItemChangeMonitor, IHasOrder
2013-12-19 21:51:32 +00:00
{
private readonly ILiveTvManager _liveTvManager;
2015-10-25 17:13:30 +00:00
public ProgramImageProvider(ILiveTvManager liveTvManager)
2013-12-19 21:51:32 +00:00
{
_liveTvManager = liveTvManager;
}
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
2013-12-19 21:51:32 +00:00
{
return new[] { ImageType.Primary };
2013-12-19 21:51:32 +00:00
}
2016-11-13 21:04:21 +00:00
private string GetItemExternalId(BaseItem item)
{
var externalId = item.ExternalId;
if (string.IsNullOrWhiteSpace(externalId))
{
externalId = item.GetProviderId("ProviderExternalId");
}
return externalId;
}
public async Task<DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
2013-12-19 21:51:32 +00:00
{
var liveTvItem = (LiveTvProgram)item;
2013-12-19 21:51:32 +00:00
var imageResponse = new DynamicImageResponse();
2013-12-19 21:51:32 +00:00
2015-10-25 17:13:30 +00:00
var service = _liveTvManager.Services.FirstOrDefault(i => string.Equals(i.Name, liveTvItem.ServiceName, StringComparison.OrdinalIgnoreCase));
2013-12-19 21:51:32 +00:00
2015-10-25 17:13:30 +00:00
if (service != null)
{
try
2013-12-19 21:51:32 +00:00
{
2015-10-25 17:13:30 +00:00
var channel = _liveTvManager.GetInternalChannel(liveTvItem.ChannelId);
2015-06-01 14:49:23 +00:00
2017-02-09 23:59:27 +00:00
if (channel != null)
2013-12-22 17:16:24 +00:00
{
2017-02-09 23:59:27 +00:00
var response = await service.GetProgramImageAsync(GetItemExternalId(liveTvItem), GetItemExternalId(channel), cancellationToken).ConfigureAwait(false);
if (response != null)
{
imageResponse.HasImage = true;
imageResponse.Stream = response.Stream;
imageResponse.Format = response.Format;
}
2013-12-22 17:16:24 +00:00
}
2013-12-19 21:51:32 +00:00
}
2015-10-25 17:13:30 +00:00
catch (NotImplementedException)
{
}
2013-12-19 21:51:32 +00:00
}
return imageResponse;
}
2013-12-19 21:51:32 +00:00
public string Name
{
get { return "Live TV Service Provider"; }
}
2013-12-22 17:16:24 +00:00
public bool Supports(IHasImages item)
{
return item is LiveTvProgram;
2013-12-19 21:51:32 +00:00
}
public int Order
2013-12-19 21:51:32 +00:00
{
2015-04-12 01:38:38 +00:00
get
{
// Let the better providers run first
return 100;
}
2013-12-19 21:51:32 +00:00
}
2016-04-08 18:32:38 +00:00
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
2013-12-19 21:51:32 +00:00
{
2014-03-06 05:17:13 +00:00
var liveTvItem = item as LiveTvProgram;
if (liveTvItem != null)
{
2015-10-04 18:10:50 +00:00
return !liveTvItem.HasImage(ImageType.Primary);
2014-03-06 05:17:13 +00:00
}
return false;
2013-12-19 21:51:32 +00:00
}
}
}