M3uParser.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using CommonIO;
  8. using MediaBrowser.Common.Extensions;
  9. using MediaBrowser.Common.Net;
  10. using MediaBrowser.Controller.LiveTv;
  11. using MediaBrowser.Model.Logging;
  12. namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
  13. {
  14. public class M3uParser
  15. {
  16. private readonly ILogger _logger;
  17. private readonly IFileSystem _fileSystem;
  18. private readonly IHttpClient _httpClient;
  19. public M3uParser(ILogger logger, IFileSystem fileSystem, IHttpClient httpClient)
  20. {
  21. _logger = logger;
  22. _fileSystem = fileSystem;
  23. _httpClient = httpClient;
  24. }
  25. public async Task<List<M3UChannel>> Parse(string url, string channelIdPrefix, CancellationToken cancellationToken)
  26. {
  27. var urlHash = url.GetMD5().ToString("N");
  28. // Read the file and display it line by line.
  29. using (var reader = new StreamReader(await GetListingsStream(url, cancellationToken).ConfigureAwait(false)))
  30. {
  31. return GetChannels(reader, urlHash, channelIdPrefix);
  32. }
  33. }
  34. public Task<Stream> GetListingsStream(string url, CancellationToken cancellationToken)
  35. {
  36. if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  37. {
  38. return _httpClient.Get(url, cancellationToken);
  39. }
  40. return Task.FromResult(_fileSystem.OpenRead(url));
  41. }
  42. private List<M3UChannel> GetChannels(StreamReader reader, string urlHash, string channelIdPrefix)
  43. {
  44. var channels = new List<M3UChannel>();
  45. string channnelName = null;
  46. string channelNumber = null;
  47. string line;
  48. string imageUrl = null;
  49. while ((line = reader.ReadLine()) != null)
  50. {
  51. line = line.Trim();
  52. if (string.IsNullOrWhiteSpace(line))
  53. {
  54. continue;
  55. }
  56. if (line.StartsWith("#EXTM3U", StringComparison.OrdinalIgnoreCase))
  57. {
  58. continue;
  59. }
  60. if (line.StartsWith("#EXTINF:", StringComparison.OrdinalIgnoreCase))
  61. {
  62. line = line.Substring(8);
  63. _logger.Info("Found m3u channel: {0}", line);
  64. var parts = line.Split(new[] { ',' }, 2);
  65. channelNumber = parts[0].Trim().Split(' ')[0] ?? "0";
  66. channnelName = FindProperty("tvg-name", line, parts[1]);
  67. imageUrl = FindProperty("tvg-logo", line, null);
  68. }
  69. else if (!string.IsNullOrWhiteSpace(channelNumber))
  70. {
  71. channels.Add(new M3UChannel
  72. {
  73. Name = channnelName,
  74. Number = channelNumber,
  75. Id = channelIdPrefix + urlHash + line.GetMD5().ToString("N"),
  76. ImageUrl = imageUrl
  77. });
  78. imageUrl = null;
  79. channelNumber = null;
  80. channnelName = null;
  81. }
  82. }
  83. return channels;
  84. }
  85. public string FindProperty(string property, string properties, string defaultResult = "")
  86. {
  87. var reg = new Regex(@"([a-z0-9\-_]+)=\""([^""]+)\""", RegexOptions.IgnoreCase);
  88. var matches = reg.Matches(properties);
  89. foreach (Match match in matches)
  90. {
  91. if (match.Groups[1].Value == property)
  92. {
  93. return match.Groups[2].Value;
  94. }
  95. }
  96. return defaultResult;
  97. }
  98. }
  99. public class M3UChannel : ChannelInfo
  100. {
  101. public string Path { get; set; }
  102. }
  103. }