Merge pull request #1491 from jose-pr/M3UParser

M3u Parser Improvements
This commit is contained in:
Luke 2016-02-22 23:14:37 -05:00
commit af90c48057

View File

@ -48,12 +48,9 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
private List<M3UChannel> GetChannels(StreamReader reader, string urlHash, string channelIdPrefix) private List<M3UChannel> GetChannels(StreamReader reader, string urlHash, string channelIdPrefix)
{ {
var channels = new List<M3UChannel>(); var channels = new List<M3UChannel>();
string channnelName = null;
string channelNumber = null;
string line; string line;
string imageUrl = null; string extInf = "";
while ((line = reader.ReadLine()) != null) while ((line = reader.ReadLine()) != null)
{ {
line = line.Trim(); line = line.Trim();
if (string.IsNullOrWhiteSpace(line)) if (string.IsNullOrWhiteSpace(line))
@ -68,30 +65,49 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
if (line.StartsWith("#EXTINF:", StringComparison.OrdinalIgnoreCase)) if (line.StartsWith("#EXTINF:", StringComparison.OrdinalIgnoreCase))
{ {
line = line.Substring(8); extInf = line.Substring(8).Trim();
_logger.Info("Found m3u channel: {0}", line); _logger.Info("Found m3u channel: {0}", extInf);
var parts = line.Split(new[] { ',' }, 2);
channelNumber = parts[0].Trim().Split(' ')[0] ?? "0";
channnelName = FindProperty("tvg-name", line, parts[1]);
imageUrl = FindProperty("tvg-logo", line, null);
} }
else if (!string.IsNullOrWhiteSpace(channelNumber)) else if (!string.IsNullOrWhiteSpace(extInf))
{ {
channels.Add(new M3UChannel var channel = GetChannelnfo(extInf);
{ channel.Id = channelIdPrefix + urlHash + line.GetMD5().ToString("N");
Name = channnelName, channel.Path = line;
Number = channelNumber, channels.Add(channel);
Id = channelIdPrefix + urlHash + line.GetMD5().ToString("N"), extInf = "";
ImageUrl = imageUrl
});
imageUrl = null;
channelNumber = null;
channnelName = null;
} }
} }
return channels; return channels;
} }
public M3UChannel GetChannelnfo(string extInf)
{
var titleIndex = extInf.LastIndexOf(',');
var channel = new M3UChannel();
channel.Number = extInf.Trim().Split(' ')[0] ?? "0";
channel.Name = extInf.Substring(titleIndex + 1);
if(channel.Number == "-1") { channel.Number = "0"; }
//Check for channel number with the format from SatIp
int number;
var numberIndex = channel.Name.IndexOf('.');
if (numberIndex > 0)
{
if (int.TryParse(channel.Name.Substring(0, numberIndex), out number))
{
channel.Number = number.ToString();
channel.Name = channel.Name.Substring(numberIndex + 1);
}
}
channel.ImageUrl = FindProperty("tvg-logo", extInf, null);
channel.Number = FindProperty("tvg-id", extInf, channel.Number);
channel.Number = FindProperty("channel-id", extInf, channel.Number);
channel.Name = FindProperty("tvg-name", extInf, channel.Name);
channel.Name = FindProperty("tvg-id", extInf, channel.Name);
return channel;
}
public string FindProperty(string property, string properties, string defaultResult = "") public string FindProperty(string property, string properties, string defaultResult = "")
{ {
var reg = new Regex(@"([a-z0-9\-_]+)=\""([^""]+)\""", RegexOptions.IgnoreCase); var reg = new Regex(@"([a-z0-9\-_]+)=\""([^""]+)\""", RegexOptions.IgnoreCase);