update m3u name parsing

This commit is contained in:
Luke Pulverenti 2016-11-27 15:52:24 -05:00
parent 26ef23d628
commit 7f51f27014

View File

@ -57,6 +57,7 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts
return Task.FromResult(_fileSystem.OpenRead(url)); return Task.FromResult(_fileSystem.OpenRead(url));
} }
const string ExtInfPrefix = "#EXTINF:";
private List<M3UChannel> GetChannels(StreamReader reader, string urlHash, string channelIdPrefix, string tunerHostId) private List<M3UChannel> GetChannels(StreamReader reader, string urlHash, string channelIdPrefix, string tunerHostId)
{ {
var channels = new List<M3UChannel>(); var channels = new List<M3UChannel>();
@ -75,9 +76,9 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts
continue; continue;
} }
if (line.StartsWith("#EXTINF:", StringComparison.OrdinalIgnoreCase)) if (line.StartsWith(ExtInfPrefix, StringComparison.OrdinalIgnoreCase))
{ {
extInf = line.Substring(8).Trim(); extInf = line.Substring(ExtInfPrefix.Length).Trim();
_logger.Info("Found m3u channel: {0}", extInf); _logger.Info("Found m3u channel: {0}", extInf);
} }
else if (!string.IsNullOrWhiteSpace(extInf) && !line.StartsWith("#", StringComparison.OrdinalIgnoreCase)) else if (!string.IsNullOrWhiteSpace(extInf) && !line.StartsWith("#", StringComparison.OrdinalIgnoreCase))
@ -91,61 +92,131 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts
} }
return channels; return channels;
} }
private M3UChannel GetChannelnfo(string extInf, string tunerHostId, string mediaUrl) private M3UChannel GetChannelnfo(string extInf, string tunerHostId, string mediaUrl)
{ {
var titleIndex = extInf.LastIndexOf(',');
var channel = new M3UChannel(); var channel = new M3UChannel();
channel.TunerHostId = tunerHostId; channel.TunerHostId = tunerHostId;
channel.Number = extInf.Trim().Split(' ')[0] ?? "0"; extInf = extInf.Trim();
channel.Name = extInf.Substring(titleIndex + 1);
//Check for channel number with the format from SatIp //Check for channel number with the format from SatIp
int number; //int number;
var numberIndex = channel.Name.IndexOf('.'); //var numberIndex = channel.Name.IndexOf('.');
if (numberIndex > 0) //if (numberIndex > 0)
{ //{
if (int.TryParse(channel.Name.Substring(0, numberIndex), out number)) // if (int.TryParse(channel.Name.Substring(0, numberIndex), out number))
{ // {
channel.Number = number.ToString(); // channel.Number = number.ToString();
channel.Name = channel.Name.Substring(numberIndex + 1); // channel.Name = channel.Name.Substring(numberIndex + 1);
} // }
} //}
if (string.Equals(channel.Number, "-1", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrWhiteSpace(mediaUrl))
{
channel.Number = Path.GetFileNameWithoutExtension(mediaUrl.Split('/').Last());
}
if (string.Equals(channel.Number, "-1", StringComparison.OrdinalIgnoreCase))
{
channel.Number = "0";
}
channel.ImageUrl = FindProperty("tvg-logo", extInf); channel.ImageUrl = FindProperty("tvg-logo", extInf);
channel.Name = GetChannelName(extInf);
channel.Number = GetChannelNumber(extInf, mediaUrl);
return channel;
}
private string GetChannelNumber(string extInf, string mediaUrl)
{
var nameParts = extInf.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var nameInExtInf = nameParts.Length > 1 ? nameParts.Last().Trim() : null;
var numberString = nameParts[0];
//Check for channel number with the format from SatIp
int number;
if (!string.IsNullOrWhiteSpace(nameInExtInf))
{
var numberIndex = nameInExtInf.IndexOf('.');
if (numberIndex > 0)
{
if (int.TryParse(nameInExtInf.Substring(0, numberIndex), out number))
{
numberString = number.ToString();
}
}
}
if (string.IsNullOrWhiteSpace(numberString) ||
string.Equals(numberString, "-1", StringComparison.OrdinalIgnoreCase) ||
string.Equals(numberString, "0", StringComparison.OrdinalIgnoreCase))
{
numberString = FindProperty("tvg-id", extInf);
}
if (string.IsNullOrWhiteSpace(numberString) ||
string.Equals(numberString, "-1", StringComparison.OrdinalIgnoreCase) ||
string.Equals(numberString, "0", StringComparison.OrdinalIgnoreCase))
{
numberString = FindProperty("channel-id", extInf);
}
if (string.IsNullOrWhiteSpace(numberString) ||
string.Equals(numberString, "-1", StringComparison.OrdinalIgnoreCase) ||
string.Equals(numberString, "0", StringComparison.OrdinalIgnoreCase))
{
numberString = null;
}
if (string.IsNullOrWhiteSpace(numberString))
{
if (string.IsNullOrWhiteSpace(mediaUrl))
{
numberString = null;
}
else
{
numberString = Path.GetFileNameWithoutExtension(mediaUrl.Split('/').Last());
}
}
return numberString;
}
private string GetChannelName(string extInf)
{
var nameParts = extInf.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var nameInExtInf = nameParts.Length > 1 ? nameParts.Last().Trim() : null;
//Check for channel number with the format from SatIp
int number;
if (!string.IsNullOrWhiteSpace(nameInExtInf))
{
var numberIndex = nameInExtInf.IndexOf('.');
if (numberIndex > 0)
{
if (int.TryParse(nameInExtInf.Substring(0, numberIndex), out number))
{
//channel.Number = number.ToString();
nameInExtInf = nameInExtInf.Substring(numberIndex + 1);
}
}
}
var name = FindProperty("tvg-name", extInf); var name = FindProperty("tvg-name", extInf);
if (string.IsNullOrWhiteSpace(name))
{
name = nameInExtInf;
}
if (string.IsNullOrWhiteSpace(name)) if (string.IsNullOrWhiteSpace(name))
{ {
name = FindProperty("tvg-id", extInf); name = FindProperty("tvg-id", extInf);
} }
channel.Name = name; if (string.IsNullOrWhiteSpace(name))
var numberString = FindProperty("tvg-id", extInf);
if (string.IsNullOrWhiteSpace(numberString))
{ {
numberString = FindProperty("channel-id", extInf); name = null;
} }
if (!string.IsNullOrWhiteSpace(numberString)) return name;
{
channel.Number = numberString;
} }
return channel;
}
private string FindProperty(string property, string properties) private string FindProperty(string property, string properties)
{ {
var reg = new Regex(@"([a-z0-9\-_]+)=\""([^""]+)\""", RegexOptions.IgnoreCase); var reg = new Regex(@"([a-z0-9\-_]+)=\""([^""]+)\""", RegexOptions.IgnoreCase);