Skip to content

Commit

Permalink
support multi streamurl for m3u
Browse files Browse the repository at this point in the history
  • Loading branch information
madbulls authored and hariimurti committed Sep 17, 2021
1 parent 8077568 commit 68d72e1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 41 deletions.
55 changes: 31 additions & 24 deletions app/src/main/java/net/harimurti/tv/extension/Playlist.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.harimurti.tv.extension

import com.google.gson.Gson
import com.google.gson.JsonParseException
import net.harimurti.tv.extra.AppLog
import net.harimurti.tv.extra.M3uTool
import net.harimurti.tv.model.*

Expand All @@ -21,32 +22,38 @@ fun List<M3U>?.toPlaylist(): Playlist? {
var drm: DrmLicense?

for (item in this) {
//hashset drm (disable same value)
if(!item.licenseKey.isNullOrEmpty()) {
drm = DrmLicense()
drm.name = item.licenseName
drm.url = item.licenseKey
if (hashSet.none { d -> d.name == item.licenseName })
hashSet.add(drm)
}

//hashmap (map same groupname as key)
map = hashMap[item.groupName]
//set map value as channel
ch = Channel()
if (map != null) {
ch.name = item.channelName
ch.streamUrl = item.streamUrl
ch.drmName = item.licenseName
map.add(ch)
}else {
map = ArrayList()
ch.name = item.channelName
ch.streamUrl = item.streamUrl
ch.drmName = item.licenseName
map.add(ch)
for (i in item.streamUrl!!.indices) {
//hashset drm (disable same value)
if (!item.licenseKey.isNullOrEmpty()) {
drm = DrmLicense()
drm.name = item.licenseName
drm.url = item.licenseKey
if (hashSet.none { d -> d.name == item.licenseName })
hashSet.add(drm)
}

//hashmap (map same groupname as key)
map = hashMap[item.groupName]
//set map value as channel
ch = Channel()
if (map != null) {
ch.name =
if (i > 0) item.channelName + " #$i" else item.channelName
ch.streamUrl = item.streamUrl!![i]
ch.drmName = item.licenseName
map.add(ch)
}else {
map = ArrayList()
ch.name =
if (i > 0) item.channelName + " #$i" else item.channelName
ch.streamUrl = item.streamUrl!![i]
ch.drmName = item.licenseName
map.add(ch)
}
hashMap[item.groupName.toString()] = map
AppLog.d("Load channel "+map.size.toString())
}
hashMap[item.groupName.toString()] = map
}

//set map as categories
Expand Down
27 changes: 11 additions & 16 deletions app/src/main/java/net/harimurti/tv/extra/M3uTool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class M3uTool {

var m3u = M3U()
var userAgent: String? = null
var extGrp: String? = null
while (buffer.readLine().also { line = it } != null) {
lineNumber++
when {
Expand All @@ -47,45 +48,39 @@ class M3uTool {
userAgent = regexUserAgent(line)
}
isExtGrp(line) -> {
// reset if GroupName is set
if(!m3u.groupName.isNullOrEmpty())
m3u= M3U()

// set group name
m3u.groupName = regexGrp(line)?.normalize()
// get extgroup name
extGrp = regexGrp(line)?.normalize()
}
isExtInf(line) -> {
// reset if ChannelName is set
if(!m3u.channelName.isNullOrEmpty())
result.add(m3u)
m3u= M3U()

// set channel name
m3u.channelName = regexCh(line)?.normalize()

// set group name
m3u.groupName = regexTitle(line)?.normalize()

if(m3u.channelName.isNullOrEmpty() || m3u.channelName!!.startsWith(M3U.EXTINF))
m3u.channelName = "NO NAME"
if(m3u.groupName.isNullOrBlank())
m3u.groupName = extGrp ?: "UNCATAGORIZED"
}
isKodi(line) -> {
// set drm license
m3u.licenseKey = regexKodi(line)
m3u.licenseName = md5(regexKodi(line).toString())
}
isStream(line) -> {
if(m3u.channelName.isNullOrEmpty() || m3u.channelName!!.startsWith(M3U.EXTINF))
m3u.channelName = "NO NAME"
if(m3u.groupName.isNullOrBlank())
m3u.groupName = "UNCATAGORIZED"

// add channel
val streamUrl = line?.trim()
m3u.streamUrl = if (userAgent == null) streamUrl else "$streamUrl|User-Agent=$userAgent"
result.add(m3u)

// reset channel
m3u= M3U()
m3u.streamUrl!!.add((if (userAgent == null) streamUrl else "$streamUrl|User-Agent=$userAgent")!!)
}
}
}
buffer.close()
} catch (e: IOException) {
throw M3U.ParsingException(lineNumber, "Cannot read file", e)
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/net/harimurti/tv/model/M3U.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class M3U {
@SerializedName("name")
var channelName: String? = null
@SerializedName("stream_url")
var streamUrl: String? = null
var streamUrl: ArrayList<String>? = ArrayList()
@SerializedName("drm_url")
var licenseKey: String? = null
@SerializedName("drm_name")
Expand Down

0 comments on commit 68d72e1

Please sign in to comment.