SDLive multi-instance

This commit is contained in:
2025-08-22 00:48:16 -04:00
parent 665727562d
commit 142dff3a84
3 changed files with 29 additions and 43 deletions
@@ -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
}
@@ -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
}
+13 -43
View File
@@ -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) { export async function GET(request: NextRequest) {
const response = await fetch('https://sdlive-1-internal.d-ho.me/playlist.m3u8') 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) { if (!response.ok) {
return new Response('Failed to fetch playlist', { status: 500 }) return new Response('Failed to fetch playlist', { status: 500 })
} }
const playlist = await response.text() const playlist = await response.text()
const parsedPlaylist = addSdliveIdsToPlaylist(playlist)
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")
return new Response(parsedPlaylist, { return new Response(parsedPlaylist, {
@@ -56,4 +25,5 @@ export async function GET(request: Request) {
'Content-Type': 'application/vnd.apple.mpegurl', 'Content-Type': 'application/vnd.apple.mpegurl',
} }
}) })
} }