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