Skip to content

Commit

Permalink
Fix: M3U Parser Cannot parse the EXTINF which has a space after it.
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyroid committed Jun 1, 2024
1 parent 4c65db7 commit be59842
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions data/src/main/java/com/m3u/data/parser/m3u/M3UParserImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.m3u.core.architecture.dispatcher.M3uDispatchers.IO
import com.m3u.core.architecture.logger.Logger
import com.m3u.core.architecture.logger.Profiles
import com.m3u.core.architecture.logger.install
import com.m3u.core.architecture.logger.post
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
Expand All @@ -23,8 +24,8 @@ internal class M3UParserImpl @Inject constructor(
private const val M3U_INFO_MARK = "#EXTINF:"
private const val KODI_MARK = "#KODIPROP:"

private val infoRegex = """$M3U_INFO_MARK(-?\d+)(.*),(.+)""".toRegex()
private val kodiPropRegex = """$KODI_MARK(.+)=(.+)""".toRegex()
private val infoRegex = """(-?\d+)(.*),(.+)""".toRegex()
private val kodiPropRegex = """(.+)=(.+)""".toRegex()
private val metadataRegex = """([\w-_.]+)=\s*(?:"([^"]*)"|(\S+))""".toRegex()

private const val M3U_TVG_LOGO_MARK = "tvg-logo"
Expand All @@ -42,7 +43,7 @@ internal class M3UParserImpl @Inject constructor(
.lineSequence()
.filter { it.isNotEmpty() }
.map { it.trimEnd() }
.dropWhile { it == M3U_HEADER_MARK }
.dropWhile { it.startsWith(M3U_HEADER_MARK) }
.iterator()

var currentLine: String
Expand All @@ -52,11 +53,15 @@ internal class M3UParserImpl @Inject constructor(
while (lines.hasNext()) {
currentLine = lines.next()
while (currentLine.startsWith("#")) {
logger.post { currentLine }
if (currentLine.startsWith(M3U_INFO_MARK)) {
infoMatch = infoRegex.matchEntire(currentLine)
infoMatch = infoRegex
.matchEntire(currentLine.drop(M3U_INFO_MARK.length).trim())
}
if (currentLine.startsWith(KODI_MARK)) {
kodiPropRegex.matchEntire(currentLine)?.also { kodiMatches += it }
kodiPropRegex
.matchEntire(currentLine.drop(KODI_MARK.length).trim())
?.also { kodiMatches += it }
}
if (lines.hasNext()) {
currentLine = lines.next()
Expand Down

0 comments on commit be59842

Please sign in to comment.