M3uParser.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 line;
  46. string extInf = "";
  47. while ((line = reader.ReadLine()) != null)
  48. {
  49. line = line.Trim();
  50. if (string.IsNullOrWhiteSpace(line))
  51. {
  52. continue;
  53. }
  54. if (line.StartsWith("#EXTM3U", StringComparison.OrdinalIgnoreCase))
  55. {
  56. continue;
  57. }
  58. if (line.StartsWith("#EXTINF:", StringComparison.OrdinalIgnoreCase))
  59. {
  60. extInf = line.Substring(8).Trim();
  61. _logger.Info("Found m3u channel: {0}", extInf);
  62. }
  63. else if (!string.IsNullOrWhiteSpace(extInf))
  64. {
  65. var channel = GetChannelnfo(extInf);
  66. channel.Id = line;
  67. channels.Add(channel);
  68. extInf = "";
  69. }
  70. }
  71. return channels;
  72. }
  73. public M3UChannel GetChannelnfo(string extInf)
  74. {
  75. var titleIndex = extInf.LastIndexOf(',');
  76. var channel = new M3UChannel();
  77. channel.Number = extInf.Trim().Split(' ')[0] ?? "0";
  78. channel.Name = extInf.Substring(titleIndex + 1);
  79. if(channel.Number == "-1") { channel.Number = "0"; }
  80. //Check for channel number with the format from SatIp
  81. int number;
  82. var numberIndex = channel.Name.IndexOf('.');
  83. if (numberIndex > 0)
  84. {
  85. if (int.TryParse(channel.Name.Substring(0, numberIndex), out number))
  86. {
  87. channel.Number = number.ToString();
  88. channel.Name = channel.Name.Substring(numberIndex + 1);
  89. }
  90. }
  91. channel.ImageUrl = FindProperty("tvg-logo", extInf, null);
  92. channel.Number = FindProperty("tvg-id", extInf, channel.Number);
  93. channel.Number = FindProperty("channel-id", extInf, channel.Number);
  94. channel.Name = FindProperty("tvg-name", extInf, channel.Name);
  95. channel.Name = FindProperty("tvg-id", extInf, channel.Name);
  96. return channel;
  97. }
  98. public string FindProperty(string property, string properties, string defaultResult = "")
  99. {
  100. var reg = new Regex(@"([a-z0-9\-_]+)=\""([^""]+)\""", RegexOptions.IgnoreCase);
  101. var matches = reg.Matches(properties);
  102. foreach (Match match in matches)
  103. {
  104. if (match.Groups[1].Value == property)
  105. {
  106. return match.Groups[2].Value;
  107. }
  108. }
  109. return defaultResult;
  110. }
  111. }
  112. public class M3UChannel : ChannelInfo
  113. {
  114. public string Path { get; set; }
  115. }
  116. }