2019-01-06 13:50:43 -07:00
|
|
|
using System;
|
2016-02-18 23:20:18 -07:00
|
|
|
using System.Collections.Generic;
|
2016-12-07 13:03:00 -07:00
|
|
|
using System.Globalization;
|
2016-02-18 23:20:18 -07:00
|
|
|
using System.IO;
|
2016-08-30 11:17:37 -07:00
|
|
|
using System.Linq;
|
2016-02-21 10:22:13 -07:00
|
|
|
using System.Text.RegularExpressions;
|
2016-02-18 23:20:18 -07:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
using MediaBrowser.Common.Net;
|
2016-10-18 11:35:27 -07:00
|
|
|
using MediaBrowser.Controller;
|
2016-02-18 23:20:18 -07:00
|
|
|
using MediaBrowser.Controller.LiveTv;
|
2017-01-14 12:57:08 -07:00
|
|
|
using MediaBrowser.Model.Extensions;
|
2019-01-13 12:22:00 -07:00
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2016-02-18 23:20:18 -07:00
|
|
|
|
2016-11-03 16:35:19 -07:00
|
|
|
namespace Emby.Server.Implementations.LiveTv.TunerHosts
|
2016-02-18 23:20:18 -07:00
|
|
|
{
|
|
|
|
public class M3uParser
|
|
|
|
{
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
private readonly IHttpClient _httpClient;
|
2016-10-18 11:35:27 -07:00
|
|
|
private readonly IServerApplicationHost _appHost;
|
2016-02-18 23:20:18 -07:00
|
|
|
|
2016-10-18 11:35:27 -07:00
|
|
|
public M3uParser(ILogger logger, IFileSystem fileSystem, IHttpClient httpClient, IServerApplicationHost appHost)
|
2016-02-18 23:20:18 -07:00
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
_httpClient = httpClient;
|
2016-10-18 11:35:27 -07:00
|
|
|
_appHost = appHost;
|
2016-02-18 23:20:18 -07:00
|
|
|
}
|
|
|
|
|
2017-08-20 12:10:00 -07:00
|
|
|
public async Task<List<ChannelInfo>> Parse(string url, string channelIdPrefix, string tunerHostId, CancellationToken cancellationToken)
|
2016-02-18 23:20:18 -07:00
|
|
|
{
|
|
|
|
// Read the file and display it line by line.
|
|
|
|
using (var reader = new StreamReader(await GetListingsStream(url, cancellationToken).ConfigureAwait(false)))
|
|
|
|
{
|
2017-07-30 11:02:25 -07:00
|
|
|
return GetChannels(reader, channelIdPrefix, tunerHostId);
|
2016-02-18 23:20:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-20 12:10:00 -07:00
|
|
|
public List<ChannelInfo> ParseString(string text, string channelIdPrefix, string tunerHostId)
|
2017-01-13 20:46:02 -07:00
|
|
|
{
|
|
|
|
// Read the file and display it line by line.
|
|
|
|
using (var reader = new StringReader(text))
|
|
|
|
{
|
2017-07-30 11:02:25 -07:00
|
|
|
return GetChannels(reader, channelIdPrefix, tunerHostId);
|
2017-01-13 20:46:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-18 23:20:18 -07:00
|
|
|
public Task<Stream> GetListingsStream(string url, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2016-10-18 11:35:27 -07:00
|
|
|
return _httpClient.Get(new HttpRequestOptions
|
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
// Some data providers will require a user agent
|
2019-01-19 17:12:44 -07:00
|
|
|
UserAgent = _appHost.ApplicationSemanticVersion
|
2016-10-18 11:35:27 -07:00
|
|
|
});
|
2016-02-18 23:20:18 -07:00
|
|
|
}
|
|
|
|
return Task.FromResult(_fileSystem.OpenRead(url));
|
|
|
|
}
|
|
|
|
|
2016-11-27 13:52:24 -07:00
|
|
|
const string ExtInfPrefix = "#EXTINF:";
|
2017-08-20 12:10:00 -07:00
|
|
|
private List<ChannelInfo> GetChannels(TextReader reader, string channelIdPrefix, string tunerHostId)
|
2016-02-18 23:20:18 -07:00
|
|
|
{
|
2017-08-20 12:10:00 -07:00
|
|
|
var channels = new List<ChannelInfo>();
|
2016-02-18 23:20:18 -07:00
|
|
|
string line;
|
2016-02-22 17:48:30 -07:00
|
|
|
string extInf = "";
|
2017-02-01 13:56:41 -07:00
|
|
|
|
2016-11-27 13:52:24 -07:00
|
|
|
while ((line = reader.ReadLine()) != null)
|
2016-02-18 23:20:18 -07:00
|
|
|
{
|
|
|
|
line = line.Trim();
|
|
|
|
if (string.IsNullOrWhiteSpace(line))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line.StartsWith("#EXTM3U", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-27 13:52:24 -07:00
|
|
|
if (line.StartsWith(ExtInfPrefix, StringComparison.OrdinalIgnoreCase))
|
2016-02-18 23:20:18 -07:00
|
|
|
{
|
2016-11-27 13:52:24 -07:00
|
|
|
extInf = line.Substring(ExtInfPrefix.Length).Trim();
|
2018-12-13 06:18:25 -07:00
|
|
|
_logger.LogInformation("Found m3u channel: {0}", extInf);
|
2016-02-18 23:20:18 -07:00
|
|
|
}
|
2016-03-18 18:40:13 -07:00
|
|
|
else if (!string.IsNullOrWhiteSpace(extInf) && !line.StartsWith("#", StringComparison.OrdinalIgnoreCase))
|
2016-02-24 12:06:26 -07:00
|
|
|
{
|
2016-08-30 11:17:37 -07:00
|
|
|
var channel = GetChannelnfo(extInf, tunerHostId, line);
|
2017-07-30 11:02:25 -07:00
|
|
|
if (string.IsNullOrWhiteSpace(channel.Id))
|
2017-01-23 14:51:23 -07:00
|
|
|
{
|
2017-07-30 11:02:25 -07:00
|
|
|
channel.Id = channelIdPrefix + line.GetMD5().ToString("N");
|
2017-01-23 14:51:23 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-30 11:02:25 -07:00
|
|
|
channel.Id = channelIdPrefix + channel.Id.GetMD5().ToString("N");
|
2017-01-23 14:51:23 -07:00
|
|
|
}
|
|
|
|
|
2016-02-22 18:09:08 -07:00
|
|
|
channel.Path = line;
|
2016-02-22 17:48:30 -07:00
|
|
|
channels.Add(channel);
|
|
|
|
extInf = "";
|
2016-02-18 23:20:18 -07:00
|
|
|
}
|
|
|
|
}
|
2017-02-01 13:56:41 -07:00
|
|
|
|
2016-02-18 23:20:18 -07:00
|
|
|
return channels;
|
|
|
|
}
|
2016-11-27 13:52:24 -07:00
|
|
|
|
2017-08-20 12:10:00 -07:00
|
|
|
private ChannelInfo GetChannelnfo(string extInf, string tunerHostId, string mediaUrl)
|
2016-02-22 17:48:30 -07:00
|
|
|
{
|
2017-08-20 12:10:00 -07:00
|
|
|
var channel = new ChannelInfo();
|
2016-02-24 12:06:26 -07:00
|
|
|
channel.TunerHostId = tunerHostId;
|
2016-02-22 17:48:30 -07:00
|
|
|
|
2016-11-27 13:52:24 -07:00
|
|
|
extInf = extInf.Trim();
|
2016-02-22 17:48:30 -07:00
|
|
|
|
2019-01-17 10:47:41 -07:00
|
|
|
var attributes = ParseExtInf(extInf, out string remaining);
|
2016-12-07 13:03:00 -07:00
|
|
|
extInf = remaining;
|
2016-11-27 13:52:24 -07:00
|
|
|
|
2019-01-17 10:47:41 -07:00
|
|
|
if (attributes.TryGetValue("tvg-logo", out string value))
|
2016-12-07 13:03:00 -07:00
|
|
|
{
|
|
|
|
channel.ImageUrl = value;
|
|
|
|
}
|
2016-11-27 13:52:24 -07:00
|
|
|
|
2016-12-07 13:03:00 -07:00
|
|
|
channel.Name = GetChannelName(extInf, attributes);
|
|
|
|
channel.Number = GetChannelNumber(extInf, attributes, mediaUrl);
|
2016-11-27 13:52:24 -07:00
|
|
|
|
2019-01-17 10:47:41 -07:00
|
|
|
attributes.TryGetValue("tvg-id", out string tvgId);
|
2017-02-23 12:13:26 -07:00
|
|
|
|
2019-01-17 10:47:41 -07:00
|
|
|
attributes.TryGetValue("channel-id", out string channelId);
|
2017-02-23 12:13:26 -07:00
|
|
|
|
|
|
|
channel.TunerChannelId = string.IsNullOrWhiteSpace(tvgId) ? channelId : tvgId;
|
|
|
|
|
|
|
|
var channelIdValues = new List<string>();
|
2017-02-04 16:32:16 -07:00
|
|
|
if (!string.IsNullOrWhiteSpace(channelId))
|
2017-01-23 14:51:23 -07:00
|
|
|
{
|
2017-02-23 12:13:26 -07:00
|
|
|
channelIdValues.Add(channelId);
|
|
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(tvgId))
|
|
|
|
{
|
|
|
|
channelIdValues.Add(tvgId);
|
|
|
|
}
|
|
|
|
if (channelIdValues.Count > 0)
|
|
|
|
{
|
|
|
|
channel.Id = string.Join("_", channelIdValues.ToArray());
|
2017-01-23 14:51:23 -07:00
|
|
|
}
|
|
|
|
|
2016-11-27 13:52:24 -07:00
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:03:00 -07:00
|
|
|
private string GetChannelNumber(string extInf, Dictionary<string, string> attributes, string mediaUrl)
|
2016-11-27 13:52:24 -07:00
|
|
|
{
|
|
|
|
var nameParts = extInf.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
var nameInExtInf = nameParts.Length > 1 ? nameParts.Last().Trim() : null;
|
|
|
|
|
2017-01-13 20:46:02 -07:00
|
|
|
string numberString = null;
|
2016-11-27 13:52:24 -07:00
|
|
|
|
2017-01-13 21:31:43 -07:00
|
|
|
// Check for channel number with the format from SatIp
|
2017-01-13 20:46:02 -07:00
|
|
|
// #EXTINF:0,84. VOX Schweiz
|
2017-01-13 21:31:43 -07:00
|
|
|
// #EXTINF:0,84.0 - VOX Schweiz
|
2016-11-27 13:52:24 -07:00
|
|
|
if (!string.IsNullOrWhiteSpace(nameInExtInf))
|
2016-02-22 17:48:30 -07:00
|
|
|
{
|
2017-01-13 21:31:43 -07:00
|
|
|
var numberIndex = nameInExtInf.IndexOf(' ');
|
2016-11-27 13:52:24 -07:00
|
|
|
if (numberIndex > 0)
|
2016-02-22 17:48:30 -07:00
|
|
|
{
|
2017-01-13 21:31:43 -07:00
|
|
|
var numberPart = nameInExtInf.Substring(0, numberIndex).Trim(new[] { ' ', '.' });
|
|
|
|
|
2019-01-13 13:46:33 -07:00
|
|
|
if (double.TryParse(numberPart, NumberStyles.Any, CultureInfo.InvariantCulture, out var number))
|
2016-11-27 13:52:24 -07:00
|
|
|
{
|
2017-01-13 21:31:43 -07:00
|
|
|
numberString = numberPart;
|
2016-11-27 13:52:24 -07:00
|
|
|
}
|
2016-02-22 17:48:30 -07:00
|
|
|
}
|
|
|
|
}
|
2016-08-30 11:17:37 -07:00
|
|
|
|
2016-12-07 13:03:00 -07:00
|
|
|
if (!string.IsNullOrWhiteSpace(numberString))
|
|
|
|
{
|
|
|
|
numberString = numberString.Trim();
|
|
|
|
}
|
|
|
|
|
2017-02-04 16:32:16 -07:00
|
|
|
if (!IsValidChannelNumber(numberString))
|
2016-08-30 11:17:37 -07:00
|
|
|
{
|
2019-01-17 10:47:41 -07:00
|
|
|
if (attributes.TryGetValue("tvg-id", out string value))
|
2016-12-07 13:03:00 -07:00
|
|
|
{
|
2019-01-13 13:46:33 -07:00
|
|
|
if (double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var doubleValue))
|
2017-01-13 20:46:02 -07:00
|
|
|
{
|
|
|
|
numberString = value;
|
|
|
|
}
|
2016-12-07 13:03:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(numberString))
|
|
|
|
{
|
|
|
|
numberString = numberString.Trim();
|
2016-08-30 11:17:37 -07:00
|
|
|
}
|
|
|
|
|
2017-02-04 16:32:16 -07:00
|
|
|
if (!IsValidChannelNumber(numberString))
|
2016-08-30 11:17:37 -07:00
|
|
|
{
|
2019-01-17 10:47:41 -07:00
|
|
|
if (attributes.TryGetValue("channel-id", out string value))
|
2016-12-07 13:03:00 -07:00
|
|
|
{
|
|
|
|
numberString = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(numberString))
|
|
|
|
{
|
|
|
|
numberString = numberString.Trim();
|
2016-08-30 11:17:37 -07:00
|
|
|
}
|
|
|
|
|
2017-02-04 16:32:16 -07:00
|
|
|
if (!IsValidChannelNumber(numberString))
|
2016-11-27 13:52:24 -07:00
|
|
|
{
|
|
|
|
numberString = null;
|
|
|
|
}
|
2016-10-18 11:23:41 -07:00
|
|
|
|
2016-11-27 13:52:24 -07:00
|
|
|
if (string.IsNullOrWhiteSpace(numberString))
|
2016-10-18 11:23:41 -07:00
|
|
|
{
|
2016-11-27 13:52:24 -07:00
|
|
|
if (string.IsNullOrWhiteSpace(mediaUrl))
|
|
|
|
{
|
|
|
|
numberString = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-05 23:13:49 -07:00
|
|
|
try
|
|
|
|
{
|
|
|
|
numberString = Path.GetFileNameWithoutExtension(mediaUrl.Split('/').Last());
|
2016-12-07 13:03:00 -07:00
|
|
|
|
2017-06-05 23:13:49 -07:00
|
|
|
if (!IsValidChannelNumber(numberString))
|
|
|
|
{
|
|
|
|
numberString = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch
|
2016-12-07 13:03:00 -07:00
|
|
|
{
|
2017-06-05 23:13:49 -07:00
|
|
|
// Seeing occasional argument exception here
|
2016-12-07 13:03:00 -07:00
|
|
|
numberString = null;
|
|
|
|
}
|
2016-11-27 13:52:24 -07:00
|
|
|
}
|
2016-10-18 11:23:41 -07:00
|
|
|
}
|
|
|
|
|
2016-11-27 13:52:24 -07:00
|
|
|
return numberString;
|
|
|
|
}
|
2016-10-18 11:23:41 -07:00
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
private static bool IsValidChannelNumber(string numberString)
|
2017-02-04 16:32:16 -07:00
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(numberString) ||
|
|
|
|
string.Equals(numberString, "-1", StringComparison.OrdinalIgnoreCase) ||
|
|
|
|
string.Equals(numberString, "0", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-13 13:46:33 -07:00
|
|
|
if (!double.TryParse(numberString, NumberStyles.Any, CultureInfo.InvariantCulture, out var value))
|
2017-02-04 16:32:16 -07:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
private static string GetChannelName(string extInf, Dictionary<string, string> attributes)
|
2016-11-27 13:52:24 -07:00
|
|
|
{
|
|
|
|
var nameParts = extInf.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
var nameInExtInf = nameParts.Length > 1 ? nameParts.Last().Trim() : null;
|
|
|
|
|
2017-01-13 21:31:43 -07:00
|
|
|
// Check for channel number with the format from SatIp
|
|
|
|
// #EXTINF:0,84. VOX Schweiz
|
|
|
|
// #EXTINF:0,84.0 - VOX Schweiz
|
2016-11-27 13:52:24 -07:00
|
|
|
if (!string.IsNullOrWhiteSpace(nameInExtInf))
|
2016-10-18 11:23:41 -07:00
|
|
|
{
|
2017-01-13 21:31:43 -07:00
|
|
|
var numberIndex = nameInExtInf.IndexOf(' ');
|
2016-11-27 13:52:24 -07:00
|
|
|
if (numberIndex > 0)
|
|
|
|
{
|
2017-01-13 21:31:43 -07:00
|
|
|
var numberPart = nameInExtInf.Substring(0, numberIndex).Trim(new[] { ' ', '.' });
|
|
|
|
|
2019-01-13 13:46:33 -07:00
|
|
|
if (double.TryParse(numberPart, NumberStyles.Any, CultureInfo.InvariantCulture, out var number))
|
2016-11-27 13:52:24 -07:00
|
|
|
{
|
|
|
|
//channel.Number = number.ToString();
|
2017-01-13 21:31:43 -07:00
|
|
|
nameInExtInf = nameInExtInf.Substring(numberIndex + 1).Trim(new[] { ' ', '-' });
|
2016-11-27 13:52:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 10:47:41 -07:00
|
|
|
attributes.TryGetValue("tvg-name", out string name);
|
2016-12-07 13:03:00 -07:00
|
|
|
|
2016-11-27 13:52:24 -07:00
|
|
|
if (string.IsNullOrWhiteSpace(name))
|
|
|
|
{
|
|
|
|
name = nameInExtInf;
|
2016-10-18 11:23:41 -07:00
|
|
|
}
|
|
|
|
|
2016-11-27 13:52:24 -07:00
|
|
|
if (string.IsNullOrWhiteSpace(name))
|
2016-10-18 11:23:41 -07:00
|
|
|
{
|
2019-01-17 10:47:41 -07:00
|
|
|
attributes.TryGetValue("tvg-id", string name);
|
2016-10-18 11:23:41 -07:00
|
|
|
}
|
|
|
|
|
2016-11-27 13:52:24 -07:00
|
|
|
if (string.IsNullOrWhiteSpace(name))
|
|
|
|
{
|
|
|
|
name = null;
|
|
|
|
}
|
2016-02-22 17:48:30 -07:00
|
|
|
|
2016-11-27 13:52:24 -07:00
|
|
|
return name;
|
2016-02-22 17:48:30 -07:00
|
|
|
}
|
2016-11-27 13:52:24 -07:00
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
private static Dictionary<string, string> ParseExtInf(string line, out string remaining)
|
2016-02-21 10:22:13 -07:00
|
|
|
{
|
2016-12-07 13:03:00 -07:00
|
|
|
var dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
2016-02-21 10:22:13 -07:00
|
|
|
var reg = new Regex(@"([a-z0-9\-_]+)=\""([^""]+)\""", RegexOptions.IgnoreCase);
|
2016-12-07 13:03:00 -07:00
|
|
|
var matches = reg.Matches(line);
|
2017-01-14 12:57:08 -07:00
|
|
|
|
|
|
|
remaining = line;
|
|
|
|
|
2016-02-21 10:22:13 -07:00
|
|
|
foreach (Match match in matches)
|
|
|
|
{
|
2017-01-14 12:57:08 -07:00
|
|
|
var key = match.Groups[1].Value;
|
|
|
|
var value = match.Groups[2].Value;
|
2016-12-07 13:03:00 -07:00
|
|
|
|
2017-01-14 12:57:08 -07:00
|
|
|
dict[match.Groups[1].Value] = match.Groups[2].Value;
|
|
|
|
remaining = remaining.Replace(key + "=\"" + value + "\"", string.Empty, StringComparison.OrdinalIgnoreCase);
|
2016-12-07 13:03:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return dict;
|
2016-02-21 10:22:13 -07:00
|
|
|
}
|
2016-02-18 23:20:18 -07:00
|
|
|
}
|
2018-12-13 06:18:25 -07:00
|
|
|
}
|