XmlTvListingsProvider.cs 6.1 KB

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