From 665727562db922763a9b6feef5586103bd912b36 Mon Sep 17 00:00:00 2001 From: Deven Perez Date: Fri, 22 Aug 2025 00:16:38 -0400 Subject: [PATCH] Add tvg-id to sdlive-playlist --- src/app/iptv/sdlive-playlist.m3u8/route.ts | 65 ++++++++++++++++++---- 1 file changed, 53 insertions(+), 12 deletions(-) diff --git a/src/app/iptv/sdlive-playlist.m3u8/route.ts b/src/app/iptv/sdlive-playlist.m3u8/route.ts index a677e05..d6ab251 100644 --- a/src/app/iptv/sdlive-playlist.m3u8/route.ts +++ b/src/app/iptv/sdlive-playlist.m3u8/route.ts @@ -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', + } + }) } \ No newline at end of file