M3uParser.cs 5.2 KB

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