From 983c232cf59cd1bf7a87abab72a087d75eb02322 Mon Sep 17 00:00:00 2001 From: Yavuz Ozturk Date: Wed, 7 Feb 2024 12:33:00 +0100 Subject: [PATCH 1/2] Update seasonEpisodeRegex to support different kinds of season&episode matching --- Sources/M3UKit/PlaylistParser.swift | 2 +- Tests/M3UKitTests/PlaylistParserTests.swift | 28 +++++++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/Sources/M3UKit/PlaylistParser.swift b/Sources/M3UKit/PlaylistParser.swift index a6756dc..069eb49 100644 --- a/Sources/M3UKit/PlaylistParser.swift +++ b/Sources/M3UKit/PlaylistParser.swift @@ -324,7 +324,7 @@ public final class PlaylistParser { internal let mediaKindSeriesRegex: RegularExpression = #"\/series\/"# internal let mediaKindLiveRegex: RegularExpression = #"\/live\/"# - internal let seasonEpisodeRegex: RegularExpression = #" S(\d+) E(\d+)"# + internal let seasonEpisodeRegex: RegularExpression = #" (?i)s(\d+) ?(?i)e(\d+)"# internal let attributesIdRegex: RegularExpression = #"tvg-id=\"(.?|.+?)\""# internal let attributesNameRegex: RegularExpression = #"tvg-name=\"(.?|.+?)\""# diff --git a/Tests/M3UKitTests/PlaylistParserTests.swift b/Tests/M3UKitTests/PlaylistParserTests.swift index 0ca9679..b7fd06e 100644 --- a/Tests/M3UKitTests/PlaylistParserTests.swift +++ b/Tests/M3UKitTests/PlaylistParserTests.swift @@ -189,11 +189,29 @@ final class PlaylistParserTests: XCTestCase { func testSeasonEpisodeParsing() { let parser = PlaylistParser() - let input = "Kyou Kara Ore Wa!! LIVE ACTION S01 E09" - let output = parser.parseSeasonEpisode(input) - XCTAssertEqual(output.name, "Kyou Kara Ore Wa!! LIVE ACTION S01 E09") - XCTAssertEqual(output.se?.s, 1) - XCTAssertEqual(output.se?.e, 9) + let input1 = "Kyou Kara Ore Wa!! LIVE ACTION S01 E09" + let output1 = parser.parseSeasonEpisode(input1) + XCTAssertEqual(output1.name, "Kyou Kara Ore Wa!! LIVE ACTION S01 E09") + XCTAssertEqual(output1.se?.s, 1) + XCTAssertEqual(output1.se?.e, 9) + + let input2 = "Kyou Kara Ore Wa!! LIVE ACTION S01E09" + let output2 = parser.parseSeasonEpisode(input2) + XCTAssertEqual(output2.name, "Kyou Kara Ore Wa!! LIVE ACTION S01E09") + XCTAssertEqual(output2.se?.s, 1) + XCTAssertEqual(output2.se?.e, 9) + + let input3 = "Kyou Kara Ore Wa!! LIVE ACTION s01e09" + let output3 = parser.parseSeasonEpisode(input3) + XCTAssertEqual(output3.name, "Kyou Kara Ore Wa!! LIVE ACTION s01e09") + XCTAssertEqual(output3.se?.s, 1) + XCTAssertEqual(output3.se?.e, 9) + + let input4 = "Kyou Kara Ore Wa!! LIVE ACTION s01 e09" + let output4 = parser.parseSeasonEpisode(input4) + XCTAssertEqual(output4.name, "Kyou Kara Ore Wa!! LIVE ACTION s01 e09") + XCTAssertEqual(output4.se?.s, 1) + XCTAssertEqual(output4.se?.e, 9) } func testSeasonEpisodeParsingWithNameUpdate() { From cd2a6d6887f0d74de94eba8bd3a1658f8603f126 Mon Sep 17 00:00:00 2001 From: Yavuz Ozturk Date: Mon, 12 Feb 2024 22:33:40 +0100 Subject: [PATCH 2/2] Skip session line --- Sources/M3UKit/PlaylistParser.swift | 8 ++++++++ Tests/M3UKitTests/PlaylistParserTests.swift | 1 + Tests/M3UKitTests/Resources/valid.m3u | 1 + 3 files changed, 10 insertions(+) diff --git a/Sources/M3UKit/PlaylistParser.swift b/Sources/M3UKit/PlaylistParser.swift index 069eb49..e1a3df1 100644 --- a/Sources/M3UKit/PlaylistParser.swift +++ b/Sources/M3UKit/PlaylistParser.swift @@ -77,6 +77,8 @@ public final class PlaylistParser { if self.isInfoLine(line) { lastMetadataLine = line + } else if self.isSessionLine(line) { + lineNumber += 1 } else if let url = URL(string: line) { lastURL = url } @@ -127,6 +129,8 @@ public final class PlaylistParser { if self.isInfoLine(line) { lastMetadataLine = line + } else if self.isSessionLine(line) { + lineNumber += 1 } else if let url = URL(string: line) { lastURL = url } @@ -233,6 +237,10 @@ public final class PlaylistParser { return input.starts(with: "#EXTINF:") } + internal func isSessionLine(_ input: String) -> Bool { + return input.starts(with: "#EXT-X-SESSION-DATA:") + } + internal func extractDuration(line: Int, rawString: String) throws -> Int { guard let match = durationRegex.firstMatch(in: rawString), diff --git a/Tests/M3UKitTests/PlaylistParserTests.swift b/Tests/M3UKitTests/PlaylistParserTests.swift index b7fd06e..5841b9f 100644 --- a/Tests/M3UKitTests/PlaylistParserTests.swift +++ b/Tests/M3UKitTests/PlaylistParserTests.swift @@ -36,6 +36,7 @@ final class PlaylistParserTests: XCTestCase { XCTAssertEqual(playlist.medias[0].attributes.name, "TV SHOW") XCTAssertEqual(playlist.medias[0].attributes.seasonNumber, 1) XCTAssertEqual(playlist.medias[0].attributes.episodeNumber, 1) + XCTAssertEqual(playlist.medias[0].url.absoluteString, "https://cdnuk001.broadcastcdn.net/KUK-BBCNEWSHD/index.m3u8") let invalidURL = Bundle.module.url(forResource: "invalid", withExtension: "m3u")! XCTAssertThrowsError(try parser.parse(invalidURL)) diff --git a/Tests/M3UKitTests/Resources/valid.m3u b/Tests/M3UKitTests/Resources/valid.m3u index 5f66539..0b39ac0 100644 --- a/Tests/M3UKitTests/Resources/valid.m3u +++ b/Tests/M3UKitTests/Resources/valid.m3u @@ -1,4 +1,5 @@ #EXTM3U x-tvg-url="https://iptv-org.github.io/epg/guides/al/ipko.com.epg.xml,https://iptv-org.github.io/epg/guides/be/telenettv.be.epg.xml,https://iptv-org.github.io/epg/guides/bf/canalplus-afrique.com.epg.xml,https://iptv-org.github.io/epg/guides/ch/tv.blue.ch.epg.xml,https://iptv-org.github.io/epg/guides/dk/allente.se.epg.xml,https://iptv-org.github.io/epg/guides/gr/cosmote.gr.epg.xml,https://iptv-org.github.io/epg/guides/it/guidatv.sky.it.epg.xml,https://iptv-org.github.io/epg/guides/my/astro.com.my.epg.xml,https://iptv-org.github.io/epg/guides/nl/delta.nl.epg.xml,https://iptv-org.github.io/epg/guides/nl/ziggogo.tv.epg.xml,https://iptv-org.github.io/epg/guides/se/allente.se.epg.xml,https://iptv-org.github.io/epg/guides/tr/digiturk.com.tr.epg.xml,https://iptv-org.github.io/epg/guides/uk/bt.com.epg.xml" +#EXT-X-SESSION-DATA:DATA-ID="blablabla" #EXTINF:-1 tvg-id="" tvg-name="TV SHOW S01 E01" tvg-logo="" group-title="TV",TV SHOW S01 E01 https://cdnuk001.broadcastcdn.net/KUK-BBCNEWSHD/index.m3u8 #EXTINF:-1 tvg-id="BBCNews.uk" tvg-country="INT" tvg-language="English" tvg-logo="https://raw.githubusercontent.com/Tapiosinn/tv-logos/master/countries/united-kingdom/bbc-news-uk.png" group-title="News",BBC News HD (720p) [Geo-blocked]