Add tvg-id to sdlive-playlist
This commit is contained in:
@@ -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 })
|
||||
}
|
||||
|
||||
const newInfo = `#EXTINF:-1 tvg-id="${sdliveChannelNum}.sdlive" ${afterExtInf}`
|
||||
|
||||
newLines.push(newInfo, streamURL)
|
||||
}
|
||||
|
||||
const parsedPlaylist = newLines.join("\n")
|
||||
|
||||
|
||||
|
||||
return new Response(playlist, {
|
||||
headers: {
|
||||
'Content-Disposition': 'attachment; filename="sdlive-playlist.m3u8"',
|
||||
'Content-Type': 'application/vnd.apple.mpegurl',
|
||||
}
|
||||
})
|
||||
return new Response(parsedPlaylist, {
|
||||
headers: {
|
||||
'Content-Disposition': 'attachment; filename="sdlive-playlist.m3u8"',
|
||||
'Content-Type': 'application/vnd.apple.mpegurl',
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user