Skip to content
This repository has been archived by the owner on Jun 15, 2024. It is now read-only.

Update seasonEpisodeRegex to support different kinds of season&episode matching #24

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Sources/M3UKit/PlaylistParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -324,7 +332,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=\"(.?|.+?)\""#
Expand Down
29 changes: 24 additions & 5 deletions Tests/M3UKitTests/PlaylistParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -189,11 +190,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() {
Expand Down
1 change: 1 addition & 0 deletions Tests/M3UKitTests/Resources/valid.m3u
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
Loading