M3uParser.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Model.IO;
  10. using MediaBrowser.Common.Extensions;
  11. using MediaBrowser.Common.IO;
  12. using MediaBrowser.Common.Net;
  13. using MediaBrowser.Controller;
  14. using MediaBrowser.Controller.IO;
  15. using MediaBrowser.Controller.LiveTv;
  16. using MediaBrowser.Model.Logging;
  17. using MediaBrowser.Model.Extensions;
  18. namespace Emby.Server.Implementations.LiveTv.TunerHosts
  19. {
  20. public class M3uParser
  21. {
  22. private readonly ILogger _logger;
  23. private readonly IFileSystem _fileSystem;
  24. private readonly IHttpClient _httpClient;
  25. private readonly IServerApplicationHost _appHost;
  26. public M3uParser(ILogger logger, IFileSystem fileSystem, IHttpClient httpClient, IServerApplicationHost appHost)
  27. {
  28. _logger = logger;
  29. _fileSystem = fileSystem;
  30. _httpClient = httpClient;
  31. _appHost = appHost;
  32. }
  33. public async Task<List<M3UChannel>> Parse(string url, string channelIdPrefix, string tunerHostId, bool enableStreamUrlAsIdentifier, CancellationToken cancellationToken)
  34. {
  35. var urlHash = url.GetMD5().ToString("N");
  36. // Read the file and display it line by line.
  37. using (var reader = new StreamReader(await GetListingsStream(url, cancellationToken).ConfigureAwait(false)))
  38. {
  39. return GetChannels(reader, urlHash, channelIdPrefix, tunerHostId, enableStreamUrlAsIdentifier);
  40. }
  41. }
  42. public List<M3UChannel> ParseString(string text, string channelIdPrefix, string tunerHostId)
  43. {
  44. var urlHash = "text".GetMD5().ToString("N");
  45. // Read the file and display it line by line.
  46. using (var reader = new StringReader(text))
  47. {
  48. return GetChannels(reader, urlHash, channelIdPrefix, tunerHostId, false);
  49. }
  50. }
  51. public Task<Stream> GetListingsStream(string url, CancellationToken cancellationToken)
  52. {
  53. if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  54. {
  55. return _httpClient.Get(new HttpRequestOptions
  56. {
  57. Url = url,
  58. CancellationToken = cancellationToken,
  59. // Some data providers will require a user agent
  60. UserAgent = _appHost.FriendlyName + "/" + _appHost.ApplicationVersion
  61. });
  62. }
  63. return Task.FromResult(_fileSystem.OpenRead(url));
  64. }
  65. const string ExtInfPrefix = "#EXTINF:";
  66. private List<M3UChannel> GetChannels(TextReader reader, string urlHash, string channelIdPrefix, string tunerHostId, bool enableStreamUrlAsIdentifier)
  67. {
  68. var channels = new List<M3UChannel>();
  69. string line;
  70. string extInf = "";
  71. while ((line = reader.ReadLine()) != null)
  72. {
  73. line = line.Trim();
  74. if (string.IsNullOrWhiteSpace(line))
  75. {
  76. continue;
  77. }
  78. if (line.StartsWith("#EXTM3U", StringComparison.OrdinalIgnoreCase))
  79. {
  80. continue;
  81. }
  82. if (line.StartsWith(ExtInfPrefix, StringComparison.OrdinalIgnoreCase))
  83. {
  84. extInf = line.Substring(ExtInfPrefix.Length).Trim();
  85. _logger.Info("Found m3u channel: {0}", extInf);
  86. }
  87. else if (!string.IsNullOrWhiteSpace(extInf) && !line.StartsWith("#", StringComparison.OrdinalIgnoreCase))
  88. {
  89. var channel = GetChannelnfo(extInf, tunerHostId, line);
  90. if (string.IsNullOrWhiteSpace(channel.Id) || enableStreamUrlAsIdentifier)
  91. {
  92. channel.Id = channelIdPrefix + urlHash + line.GetMD5().ToString("N");
  93. }
  94. else
  95. {
  96. channel.Id = channelIdPrefix + urlHash + channel.Id.GetMD5().ToString("N");
  97. }
  98. channel.Path = line;
  99. channels.Add(channel);
  100. extInf = "";
  101. }
  102. }
  103. return channels;
  104. }
  105. private M3UChannel GetChannelnfo(string extInf, string tunerHostId, string mediaUrl)
  106. {
  107. var channel = new M3UChannel();
  108. channel.TunerHostId = tunerHostId;
  109. extInf = extInf.Trim();
  110. string remaining;
  111. var attributes = ParseExtInf(extInf, out remaining);
  112. extInf = remaining;
  113. string value;
  114. if (attributes.TryGetValue("tvg-logo", out value))
  115. {
  116. channel.ImageUrl = value;
  117. }
  118. channel.Name = GetChannelName(extInf, attributes);
  119. channel.Number = GetChannelNumber(extInf, attributes, mediaUrl);
  120. string tvgId;
  121. attributes.TryGetValue("tvg-id", out tvgId);
  122. string channelId;
  123. attributes.TryGetValue("channel-id", out channelId);
  124. channel.TunerChannelId = string.IsNullOrWhiteSpace(tvgId) ? channelId : tvgId;
  125. var channelIdValues = new List<string>();
  126. if (!string.IsNullOrWhiteSpace(channelId))
  127. {
  128. channelIdValues.Add(channelId);
  129. }
  130. if (!string.IsNullOrWhiteSpace(tvgId))
  131. {
  132. channelIdValues.Add(tvgId);
  133. }
  134. if (channelIdValues.Count > 0)
  135. {
  136. channel.Id = string.Join("_", channelIdValues.ToArray());
  137. }
  138. return channel;
  139. }
  140. private string GetChannelNumber(string extInf, Dictionary<string, string> attributes, string mediaUrl)
  141. {
  142. var nameParts = extInf.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  143. var nameInExtInf = nameParts.Length > 1 ? nameParts.Last().Trim() : null;
  144. string numberString = null;
  145. // Check for channel number with the format from SatIp
  146. // #EXTINF:0,84. VOX Schweiz
  147. // #EXTINF:0,84.0 - VOX Schweiz
  148. if (!string.IsNullOrWhiteSpace(nameInExtInf))
  149. {
  150. var numberIndex = nameInExtInf.IndexOf(' ');
  151. if (numberIndex > 0)
  152. {
  153. var numberPart = nameInExtInf.Substring(0, numberIndex).Trim(new[] { ' ', '.' });
  154. double number;
  155. if (double.TryParse(numberPart, NumberStyles.Any, CultureInfo.InvariantCulture, out number))
  156. {
  157. numberString = numberPart;
  158. }
  159. }
  160. }
  161. if (!string.IsNullOrWhiteSpace(numberString))
  162. {
  163. numberString = numberString.Trim();
  164. }
  165. if (!IsValidChannelNumber(numberString))
  166. {
  167. string value;
  168. if (attributes.TryGetValue("tvg-id", out value))
  169. {
  170. double doubleValue;
  171. if (double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out doubleValue))
  172. {
  173. numberString = value;
  174. }
  175. }
  176. }
  177. if (!string.IsNullOrWhiteSpace(numberString))
  178. {
  179. numberString = numberString.Trim();
  180. }
  181. if (!IsValidChannelNumber(numberString))
  182. {
  183. string value;
  184. if (attributes.TryGetValue("channel-id", out value))
  185. {
  186. numberString = value;
  187. }
  188. }
  189. if (!string.IsNullOrWhiteSpace(numberString))
  190. {
  191. numberString = numberString.Trim();
  192. }
  193. if (!IsValidChannelNumber(numberString))
  194. {
  195. numberString = null;
  196. }
  197. if (string.IsNullOrWhiteSpace(numberString))
  198. {
  199. if (string.IsNullOrWhiteSpace(mediaUrl))
  200. {
  201. numberString = null;
  202. }
  203. else
  204. {
  205. numberString = Path.GetFileNameWithoutExtension(mediaUrl.Split('/').Last());
  206. if (!IsValidChannelNumber(numberString))
  207. {
  208. numberString = null;
  209. }
  210. }
  211. }
  212. return numberString;
  213. }
  214. private bool IsValidChannelNumber(string numberString)
  215. {
  216. if (string.IsNullOrWhiteSpace(numberString) ||
  217. string.Equals(numberString, "-1", StringComparison.OrdinalIgnoreCase) ||
  218. string.Equals(numberString, "0", StringComparison.OrdinalIgnoreCase))
  219. {
  220. return false;
  221. }
  222. double value;
  223. if (!double.TryParse(numberString, NumberStyles.Any, CultureInfo.InvariantCulture, out value))
  224. {
  225. return false;
  226. }
  227. return true;
  228. }
  229. private string GetChannelName(string extInf, Dictionary<string, string> attributes)
  230. {
  231. var nameParts = extInf.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  232. var nameInExtInf = nameParts.Length > 1 ? nameParts.Last().Trim() : null;
  233. // Check for channel number with the format from SatIp
  234. // #EXTINF:0,84. VOX Schweiz
  235. // #EXTINF:0,84.0 - VOX Schweiz
  236. if (!string.IsNullOrWhiteSpace(nameInExtInf))
  237. {
  238. var numberIndex = nameInExtInf.IndexOf(' ');
  239. if (numberIndex > 0)
  240. {
  241. var numberPart = nameInExtInf.Substring(0, numberIndex).Trim(new[] { ' ', '.' });
  242. double number;
  243. if (double.TryParse(numberPart, NumberStyles.Any, CultureInfo.InvariantCulture, out number))
  244. {
  245. //channel.Number = number.ToString();
  246. nameInExtInf = nameInExtInf.Substring(numberIndex + 1).Trim(new[] { ' ', '-' });
  247. }
  248. }
  249. }
  250. string name;
  251. attributes.TryGetValue("tvg-name", out name);
  252. if (string.IsNullOrWhiteSpace(name))
  253. {
  254. name = nameInExtInf;
  255. }
  256. if (string.IsNullOrWhiteSpace(name))
  257. {
  258. attributes.TryGetValue("tvg-id", out name);
  259. }
  260. if (string.IsNullOrWhiteSpace(name))
  261. {
  262. name = null;
  263. }
  264. return name;
  265. }
  266. private Dictionary<string, string> ParseExtInf(string line, out string remaining)
  267. {
  268. var dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  269. var reg = new Regex(@"([a-z0-9\-_]+)=\""([^""]+)\""", RegexOptions.IgnoreCase);
  270. var matches = reg.Matches(line);
  271. remaining = line;
  272. foreach (Match match in matches)
  273. {
  274. var key = match.Groups[1].Value;
  275. var value = match.Groups[2].Value;
  276. dict[match.Groups[1].Value] = match.Groups[2].Value;
  277. remaining = remaining.Replace(key + "=\"" + value + "\"", string.Empty, StringComparison.OrdinalIgnoreCase);
  278. }
  279. return dict;
  280. }
  281. }
  282. public class M3UChannel : ChannelInfo
  283. {
  284. public string Path { get; set; }
  285. }
  286. }