XmlTvListingsProvider.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using MediaBrowser.Controller.LiveTv;
  2. using MediaBrowser.Model.Dto;
  3. using MediaBrowser.Model.LiveTv;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using Emby.XmlTv.Classes;
  11. using MediaBrowser.Controller.Configuration;
  12. namespace MediaBrowser.Server.Implementations.LiveTv.Listings
  13. {
  14. public class XmlTvListingsProvider : IListingsProvider
  15. {
  16. private readonly IServerConfigurationManager _config;
  17. public XmlTvListingsProvider(IServerConfigurationManager config)
  18. {
  19. _config = config;
  20. }
  21. public string Name
  22. {
  23. get { return "XmlTV"; }
  24. }
  25. public string Type
  26. {
  27. get { return "xmltv"; }
  28. }
  29. private string GetLanguage()
  30. {
  31. return _config.Configuration.PreferredMetadataLanguage;
  32. }
  33. // TODO: Should this method be async?
  34. public Task<IEnumerable<ProgramInfo>> GetProgramsAsync(ListingsProviderInfo info, string channelNumber, string channelName, DateTime startDateUtc, DateTime endDateUtc, CancellationToken cancellationToken)
  35. {
  36. var reader = new XmlTvReader(info.Path, GetLanguage(), null);
  37. var results = reader.GetProgrammes(channelNumber, startDateUtc, endDateUtc, cancellationToken);
  38. return Task.FromResult(results.Select(p => new ProgramInfo()
  39. {
  40. ChannelId = p.ChannelId,
  41. EndDate = p.EndDate,
  42. EpisodeNumber = p.Episode == null ? null : p.Episode.Episode,
  43. EpisodeTitle = p.Episode == null ? null : p.Episode.Title,
  44. Genres = p.Categories,
  45. Id = String.Format("{0}_{1:O}", p.ChannelId, p.StartDate), // Construct an id from the channel and start date,
  46. StartDate = p.StartDate,
  47. Name = p.Title,
  48. Overview = p.Description,
  49. ShortOverview = p.Description,
  50. ProductionYear = !p.CopyrightDate.HasValue ? (int?)null : p.CopyrightDate.Value.Year,
  51. SeasonNumber = p.Episode == null ? null : p.Episode.Series,
  52. IsSeries = p.IsSeries,
  53. IsRepeat = p.IsRepeat,
  54. // IsPremiere = !p.PreviouslyShown.HasValue,
  55. IsKids = p.Categories.Any(c => info.KidsCategories.Contains(c, StringComparer.InvariantCultureIgnoreCase)),
  56. IsMovie = p.Categories.Any(c => info.MovieCategories.Contains(c, StringComparer.InvariantCultureIgnoreCase)),
  57. IsNews = p.Categories.Any(c => info.NewsCategories.Contains(c, StringComparer.InvariantCultureIgnoreCase)),
  58. IsSports = p.Categories.Any(c => info.SportsCategories.Contains(c, StringComparer.InvariantCultureIgnoreCase)),
  59. ImageUrl = p.Icon != null && !String.IsNullOrEmpty(p.Icon.Source) ? p.Icon.Source : null,
  60. HasImage = p.Icon != null && !String.IsNullOrEmpty(p.Icon.Source),
  61. OfficialRating = p.Rating != null && !String.IsNullOrEmpty(p.Rating.Value) ? p.Rating.Value : null,
  62. CommunityRating = p.StarRating.HasValue ? p.StarRating.Value : (float?)null
  63. }));
  64. }
  65. public Task AddMetadata(ListingsProviderInfo info, List<ChannelInfo> channels, CancellationToken cancellationToken)
  66. {
  67. // Add the channel image url
  68. var reader = new XmlTvReader(info.Path, GetLanguage(), null);
  69. var results = reader.GetChannels().ToList();
  70. if (channels != null && channels.Count > 0)
  71. {
  72. channels.ForEach(c =>
  73. {
  74. var match = results.FirstOrDefault(r => r.Id == c.Id);
  75. if (match != null && match.Icon != null && !String.IsNullOrEmpty(match.Icon.Source))
  76. {
  77. c.ImageUrl = match.Icon.Source;
  78. }
  79. });
  80. }
  81. return Task.FromResult(true);
  82. }
  83. public Task Validate(ListingsProviderInfo info, bool validateLogin, bool validateListings)
  84. {
  85. // Assume all urls are valid. check files for existence
  86. if (!info.Path.StartsWith("http", StringComparison.OrdinalIgnoreCase) && !File.Exists(info.Path))
  87. {
  88. throw new FileNotFoundException("Could not find the XmlTv file specified:", info.Path);
  89. }
  90. return Task.FromResult(true);
  91. }
  92. public Task<List<NameIdPair>> GetLineups(ListingsProviderInfo info, string country, string location)
  93. {
  94. // In theory this should never be called because there is always only one lineup
  95. var reader = new XmlTvReader(info.Path, GetLanguage(), null);
  96. var results = reader.GetChannels();
  97. // Should this method be async?
  98. return Task.FromResult(results.Select(c => new NameIdPair() { Id = c.Id, Name = c.DisplayName }).ToList());
  99. }
  100. }
  101. }