Skip to content

Commit

Permalink
add m3u attributes to model
Browse files Browse the repository at this point in the history
  • Loading branch information
jefersonsv committed Mar 13, 2019
1 parent f4ae2e5 commit adbc3c4
Show file tree
Hide file tree
Showing 14 changed files with 1,466 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/m3uParser.tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static void Main(string[] args)
{
//var customFile = Path.Combine(@"C:\Data\Desktop", "test.m3u");
//var customM3u = M3U.ParseFromFile(customFile);
var simpleVodM3u = M3U.ParseFromFile(simpleVod);
var simpleVodM3u = M3U.Parse(simpleVod);
var headerParameterM3u = M3U.Parse(headerParameter);
}

Expand Down
3 changes: 3 additions & 0 deletions src/m3uParser/M3U.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ namespace m3uParser
/// https://developer.apple.com/library/content/technotes/tn2288/_index.html
/// https://developer.apple.com/documentation/http_live_streaming/example_playlists_for_http_live_streaming/event_playlist_construction
/// https://tools.ietf.org/html/draft-pantos-http-live-streaming-23#page-12
/// https://wiki.tvip.ru/en/m3u
/// https://pastebin.com/KNYEZNun
/// https://github.com/AlexanderSofronov/iptv.example
/// </summary>
public static class M3U
{
Expand Down
62 changes: 55 additions & 7 deletions src/m3uParser/Model/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,70 @@ public class Attributes
internal Attributes(IEnumerable<KeyValuePair<string, string>> attributes)
{
this.AttributeList = attributes;
this.GroupTitle = this.AttributeList.FirstOrDefault(w => w.Key.ToLower().Replace("-", string.Empty) == "grouptitle").Value;
this.GuideTimeShiftingTV = this.AttributeList.FirstOrDefault(w => w.Key.ToLower().Replace("-", string.Empty) == "tvgshift").Value;
this.GuideIdentifierTV = this.AttributeList.FirstOrDefault(w => w.Key.ToLower().Replace("-", string.Empty) == "tvgname").Value;
this.Logo = this.AttributeList.FirstOrDefault(w => w.Key.ToLower().Replace("-", string.Empty) == "tvglogo").Value;
this.AudioTrack = this.AttributeList.FirstOrDefault(w => w.Key.ToLower().Replace("-", string.Empty) == "audiotrack").Value;
this.AspectRatio = this.AttributeList.FirstOrDefault(w => w.Key.ToLower().Replace("-", string.Empty) == "aspectratio").Value;
this.Id = this.AttributeList.FirstOrDefault(w => w.Key.ToLower().Replace("-", string.Empty) == "tvgid").Value;

// Obsoletes
this.GuideTimeShiftingTV = GetOrNull("tvgshift");
this.GuideIdentifierTV = GetOrNull("tvgname");
this.Logo = GetOrNull("tvglogo");
this.Id = GetOrNull("tvgid");

this.GroupTitle = GetOrNull(nameof(this.GroupTitle));
this.TvgShift = GetOrNull(nameof(this.TvgShift));
this.TvgName = GetOrNull(nameof(this.TvgName));
this.TvgLogo = GetOrNull(nameof(this.TvgLogo));
this.AudioTrack = GetOrNull(nameof(this.AudioTrack));
this.AspectRatio = GetOrNull(nameof(this.AspectRatio));
this.TvgId = GetOrNull(nameof(this.TvgId));
this.UrlTvg = GetOrNull(nameof(this.UrlTvg));
this.M3UAutoLoad = ConvertIntOrNull(GetOrNull(nameof(this.M3UAutoLoad)));
this.Cache = ConvertIntOrNull(GetOrNull(nameof(this.Cache)));
this.Deinterlace = ConvertIntOrNull(GetOrNull(nameof(this.Deinterlace)));
this.Refresh = ConvertIntOrNull(GetOrNull(nameof(this.Refresh)));
this.ChannelNumber = ConvertIntOrNull(GetOrNull(nameof(this.ChannelNumber)));
}

string GetOrNull(string name)
{
return this.AttributeList?
.FirstOrDefault(w => w.Key?.ToLower()?.Replace("-", string.Empty) == name.ToLower())
.Value;
}

int? ConvertIntOrNull(string value)
{
int num;
if (int.TryParse(value, out num))
return num;

return null;
}

public IEnumerable<KeyValuePair<string, string>> AttributeList { get; private set; }
public string GroupTitle { get; private set; }

[Obsolete("Change to: TvgShift property")]
public string GuideTimeShiftingTV { get; private set; }
public string TvgShift { get; private set; }

[Obsolete("Change to: TvgName property")]
public string GuideIdentifierTV { get; private set; }
public string TvgName { get; private set; }

[Obsolete("Change to: TvgLogo property")]
public string Logo { get; private set; }
public string TvgLogo { get; private set; }

public string AudioTrack { get; private set; }
public string AspectRatio { get; private set; }
[Obsolete("Change to: TvgId property")]
public string Id { get; private set; }

public string TvgId { get; private set; }
public string UrlTvg { get; private set; }
public int? M3UAutoLoad { get; private set; }
public int? Cache { get; private set; }
public int? Deinterlace { get; private set; }
public int? Refresh { get; private set; }
public int? ChannelNumber { get; private set; }
}
}
19 changes: 16 additions & 3 deletions src/m3uParser/Model/Extm3u.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using Sprache;

namespace m3uParser.Model
Expand All @@ -18,6 +20,10 @@ public class Extm3u

internal Extm3u(string content)
{
CultureInfo ci = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;

var segments = SegmentSpecification.SegmentCollection.Parse(content);

if (segments == null || segments.Count() == 0)
Expand All @@ -32,7 +38,7 @@ internal Extm3u(string content)
else
{
// parse attributes
Attributes = new Attributes(PairsSpecification.Attributes.Parse(segments.First()));
Attributes = new Attributes(LinesSpecification.HeaderLine.Parse(segments.First()));
}

IList<string> warnings = new List<string>();
Expand Down Expand Up @@ -61,14 +67,21 @@ internal Extm3u(string content)
break;

case "EXTINF":
medias.Add(new Media(tag.Value));
try
{
medias.Add(new Media(tag.Value));
}
catch
{
warnings.Add($"Can't parse media #{tag.Key}{(string.IsNullOrEmpty(tag.Value) ? string.Empty : ":")}{tag.Value}");
}
break;

case "EXT-X-ENDLIST":
break;

default:
warnings.Add($"The content #{tag.Key}{(string.IsNullOrEmpty(tag.Value) ? string.Empty : ":")}{tag.Value} cannot be parsed");
warnings.Add($"Can't parse content #{tag.Key}{(string.IsNullOrEmpty(tag.Value) ? string.Empty : ":")}{tag.Value}");

break;
}
Expand Down
2 changes: 2 additions & 0 deletions src/m3uParser/Model/Media.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Threading;
using Sprache;

namespace m3uParser
Expand Down
16 changes: 10 additions & 6 deletions src/m3uParser/ParseSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ from div in Parse.String(":").Text().Optional()
from value in Parse.AnyChar.Many()
//from value in Parse.Chars((Environment.NewLine).Many()
//from end in Parse.Chars(Environment.NewLine).Optional()
select new KeyValuePair<string, string>(key.ToUpper().Trim(), string.Concat(value));
select new KeyValuePair<string, string>(key.ToUpper().Trim(), string.Concat(value).Trim());
}

internal static class LinesSpecification
Expand All @@ -73,13 +73,17 @@ from endOfLine in Parse.LineEnd.Optional()
//from open in Parse.String("#EXTINF:").Text()
from duration in PrimitiveSpecification.DecimalSigned.Once()
from param in PairsSpecification.Attributes.Token()
from space2 in Parse.Char(',').Optional()
from title in Title.Token()
select new Media(duration.First(), param, title, null);
from comma in Parse.Char(',')
from title in Title.Optional()
select new Media(duration.First(), param, title.GetOrDefault(), null);

internal static readonly Parser<Title> Title =
from raw in Parse.CharExcept(Environment.NewLine).Many().Text()
select new Title(raw, raw);
from raw in Parse.CharExcept(Environment.NewLine).Many()
//from raw in Parse.AnyChar.Except(Environment.NewLine)
//from raw in Parse.AnyChar.Many().Text()

//from raw in Parse.AnyChar().Except(Environment.NewLine) //.Many() Parse.CharExcept(Environment.NewLine).Many().Optional() //.Many().Text().Optional()
select new Title(raw.Any() ? string.Concat(raw) : null, raw.Any() ? string.Concat(raw) : null);

// https://stackoverflow.com/questions/21414309/sprache-parse-signed-integer
internal static readonly Parser<Media> Extinf =
Expand Down
Loading

0 comments on commit adbc3c4

Please sign in to comment.