From 142dff3a84365c740405b36e140006d8684c20f9 Mon Sep 17 00:00:00 2001 From: Deven Perez Date: Fri, 22 Aug 2025 00:48:16 -0400 Subject: [PATCH] SDLive multi-instance --- src/app/iptv/sdlive-1-playlist.m3u8/route.ts | 8 +++ src/app/iptv/sdlive-2-playlist.m3u8/route.ts | 8 +++ src/app/iptv/sdlive-playlist.m3u8/route.ts | 56 +++++--------------- 3 files changed, 29 insertions(+), 43 deletions(-) create mode 100644 src/app/iptv/sdlive-1-playlist.m3u8/route.ts create mode 100644 src/app/iptv/sdlive-2-playlist.m3u8/route.ts diff --git a/src/app/iptv/sdlive-1-playlist.m3u8/route.ts b/src/app/iptv/sdlive-1-playlist.m3u8/route.ts new file mode 100644 index 0000000..9325572 --- /dev/null +++ b/src/app/iptv/sdlive-1-playlist.m3u8/route.ts @@ -0,0 +1,8 @@ +import { NextRequest } from "next/server" + +export async function GET(request: NextRequest) { + const url = new URL(request.url) + const baseUrl = `${url.protocol}//${url.host}` + const response = await fetch(`${baseUrl}/iptv/sdlive-playlist.m3u8?id=1`) + return response +} \ No newline at end of file diff --git a/src/app/iptv/sdlive-2-playlist.m3u8/route.ts b/src/app/iptv/sdlive-2-playlist.m3u8/route.ts new file mode 100644 index 0000000..3e296e7 --- /dev/null +++ b/src/app/iptv/sdlive-2-playlist.m3u8/route.ts @@ -0,0 +1,8 @@ +import { NextRequest } from "next/server" + +export async function GET(request: NextRequest) { + const url = new URL(request.url) + const baseUrl = `${url.protocol}//${url.host}` + const response = await fetch(`${baseUrl}/iptv/sdlive-playlist.m3u8?id=2`) + return response +} \ No newline at end of file diff --git a/src/app/iptv/sdlive-playlist.m3u8/route.ts b/src/app/iptv/sdlive-playlist.m3u8/route.ts index d6ab251..38d1ece 100644 --- a/src/app/iptv/sdlive-playlist.m3u8/route.ts +++ b/src/app/iptv/sdlive-playlist.m3u8/route.ts @@ -1,53 +1,22 @@ -import { debugLog } from "@/utils/debugLog" +import { addSdliveIdsToPlaylist } from "@/utils/iptv/addSdliveIdsToPlaylist" +import { NextRequest } from "next/server" -export async function GET(request: Request) { - const response = await fetch('https://sdlive-1-internal.d-ho.me/playlist.m3u8') +export async function GET(request: NextRequest) { + const searchParams = request.nextUrl.searchParams + const id = searchParams.get('id') + + if (!id || !["1", "2"].includes(id)) { + return new Response("Must be supplied with an id 1 or 2", { status: 400 }) + } + + const response = await fetch(`https://sdlive-${id}-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 }) - } - - // 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") + const parsedPlaylist = addSdliveIdsToPlaylist(playlist) return new Response(parsedPlaylist, { @@ -56,4 +25,5 @@ export async function GET(request: Request) { 'Content-Type': 'application/vnd.apple.mpegurl', } }) + } \ No newline at end of file