Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix, today, try #13

Open
maurioski opened this issue Dec 26, 2024 · 3 comments
Open

Fix, today, try #13

maurioski opened this issue Dec 26, 2024 · 3 comments

Comments

@maurioski
Copy link

maurioski commented Dec 26, 2024

have somne fixes, and need to enable yt token

  • [const API_KEY = "YT TOKEN HERE";
    async function getLiveStreamDetails(channelOrVideoId) {
    let apiUrl;

    if (channelOrVideoId.length === 24) {
    apiUrl = https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=${channelOrVideoId}&type=video&eventType=live&key=${API_KEY};
    } else {
    apiUrl = https://www.googleapis.com/youtube/v3/videos?part=snippet&id=${channelOrVideoId}&key=${API_KEY};
    }
    const response = await fetch(apiUrl);
    if (!response.ok) {
    throw new Error(Error en la solicitud a la API de YouTube: ${response.statusText});
    }
    const data = await response.json();

    if (channelOrVideoId.length === 24) {
    // Validación para ID de canal
    if (data.items.length === 0) {
    throw new Error("No hay transmisiones en vivo disponibles para este canal.");
    }
    return data.items[0].id.videoId;
    } else {
    // Validación para ID de video
    if (data.items.length === 0) {
    throw new Error("El ID del video proporcionado no es válido o no está en vivo.");
    }
    return channelOrVideoId;
    }
    }

async function getManifestUrl(videoId, type) {
if (type === "dash") {
return https://www.youtube.com/watch?v=${videoId};
} else if (type === "hls") {
return https://www.youtube.com/watch?v=${videoId};
} else {
throw new Error("Tipo de manifiesto no válido. Usa 'dash' o 'hls'.");
}
}
async function handleRequest(request) {
const url = new URL(request.url);
const pathParts = url.pathname.split("/");
const resourceType = pathParts[3]; // master.mpd o master.m3u8
const channelOrVideoId = pathParts[2];

try {
const videoId = await getLiveStreamDetails(channelOrVideoId);

const manifestUrl = await getManifestUrl(videoId, resourceType === "master.mpd" ? "dash" : "hls");

return Response.redirect(manifestUrl, 302);

} catch (error) {
return new Response(error.message, { status: 500 });
}
}
addEventListener("fetch", (event) => {
event.respondWith(
handleRequest(event.request).catch(
(err) => new Response(err.stack || "Error interno del servidor", { status: 500 })
)
);
}); ]

@thegittyuserr
Copy link

Doesn't work. Could you please demonstrate its function?

@maurioski
Copy link
Author

Doesn't work. Could you please demonstrate its function?

u get the token from yt api first?

@thegittyuserr
Copy link

Yes, configured a key for the Youtube V3 API and inserted it into your script. Problem is that your code has many formatting/syntax errors, probably you had a problem copy pasting this into this thread. Could you message me the js file that you are currently using (without your API key of course) so I can be sure that your idea works?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants