Add tvg-id to sdlive-playlist

This commit is contained in:
2025-08-22 00:16:38 -04:00
parent 89a829dac0
commit 665727562d
+53 -12
View File
@@ -1,18 +1,59 @@
export async function GET(request: Request) {
const response = await fetch('https://sdlive-1-internal.d-ho.me/playlist.m3u8')
import { debugLog } from "@/utils/debugLog"
if (!response.ok) {
return new Response('Failed to fetch playlist', { status: 500 })
export async function GET(request: Request) {
const response = await fetch('https://sdlive-1-internal.d-ho.me/playlist.m3u8')
if (!response.ok) {
return new Response('Failed to fetch playlist', { status: 500 })
}
const playlist = await response.text()
const lines = playlist.split("\n")
debugLog(lines)
let newLines = [lines[0]]
for (let i = 1; i + 1 < lines.length; i += 2) {
let info = lines[i]
let streamURL = lines[i + 1]
debugLog(`Attempting to parse: info: ${info}. streamURL: ${streamURL}`)
// Basic validation to hope that assumptions are valid
if (!info.startsWith("#EXTINF:-1")) {
console.error(`iptv/sdlive-playlist.m3u8: One of the info lines did not start with #EXTINF:. Instead got: ${info}`)
return new Response("Internal Server Error", { status: 500 })
} else if (info.includes("tvg-id")) {
console.warn("iptv/sdlive-playlist.m3u8: One of the channels already has a tvg-id. Channel skipped.")
newLines.push(info, streamURL)
continue
} else if (!streamURL.match(/\/stream\/[0-9]+\.m3u8$/)) {
console.error(`iptv/sdlive-playlist.m3u8: One of the streamURLs did not match expected ending. Instead got: ${streamURL}`)
return new Response("Internal Server Error", { status: 500 })
}
const playlist = await response.text()
// Add tvg-id. This will need to be better parsed
const afterExtInf = info.slice(11)
const sdliveChannelNum = streamURL.split("/").at(-1)?.slice(0, -5)
if (sdliveChannelNum == null) {
console.error("iptv/sdlive-playlist.m3u8: One of the streamURLs was not a number")
return new Response("Internal Server Error", { status: 500 })
}
return new Response(playlist, {
headers: {
'Content-Disposition': 'attachment; filename="sdlive-playlist.m3u8"',
'Content-Type': 'application/vnd.apple.mpegurl',
}
})
const newInfo = `#EXTINF:-1 tvg-id="${sdliveChannelNum}.sdlive" ${afterExtInf}`
newLines.push(newInfo, streamURL)
}
const parsedPlaylist = newLines.join("\n")
return new Response(parsedPlaylist, {
headers: {
'Content-Disposition': 'attachment; filename="sdlive-playlist.m3u8"',
'Content-Type': 'application/vnd.apple.mpegurl',
}
})
}