Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export default async function ytDownload(request) {
- const { searchParams } = new URL(request.url);
- const url = searchParams.get('url');
- if (!url || !url.includes('youtube.com') && !url.includes('youtu.be')) {
- return new Response(JSON.stringify({ error: 'Missing or invalid YouTube URL' }), {
- status: 400,
- headers: { 'Content-Type': 'application/json' },
- });
- }
- const extractVideoId = (url) => {
- const match = url.match(/(?:v=|\/)([0-9A-Za-z_-]{11})/);
- return match ? match[1] : null;
- };
- const videoId = extractVideoId(url);
- let mp3Url = null;
- let duration = null;
- let title = null;
- let thumbnail = null;
- let mp4 = null;
- try {
- // Step 1: yt1s.click MP3
- const form = new URLSearchParams();
- form.append('q', url);
- form.append('type', 'mp3');
- const res = await fetch('https://yt1s.click/search', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'Origin': 'https://yt1s.click',
- 'Referer': 'https://yt1s.click/',
- 'User-Agent': 'Mozilla/5.0'
- },
- body: form
- });
- const text = await res.text();
- const match = text.match(/href="(\/download[^"]+)"/);
- if (match) {
- mp3Url = `https://yt1s.click${match[1]}`;
- } else {
- // Step 2: flvto fallback
- const payload = {
- fileType: 'MP3',
- id: videoId
- };
- const flvtoRes = await fetch('https://ht.flvto.online/converter', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'Origin': 'https://ht.flvto.online',
- 'Referer': `https://ht.flvto.online/widget?url=https://www.youtube.com/watch?v=${videoId}`,
- 'User-Agent': 'Mozilla/5.0',
- },
- body: JSON.stringify(payload)
- });
- const flvtoData = await flvtoRes.json();
- if (flvtoData?.link) {
- mp3Url = flvtoData.link;
- duration = flvtoData.duration || null;
- }
- }
- } catch (e) {
- mp3Url = null;
- }
- try {
- // Step 3: Clipto for MP4 and metadata
- const csrfres = await fetch('https://www.clipto.com/api/csrf', {
- headers: {
- 'user-agent': 'Mozilla/5.0',
- 'referer': 'https://www.clipto.com/id/media-downloader/youtube-downloader'
- }
- });
- const csrfData = await csrfres.json();
- const csrftoken = csrfData.token;
- const kuki = `XSRF-TOKEN=${csrftoken}`;
- const cliptoRes = await fetch('https://www.clipto.com/api/youtube', {
- method: 'POST',
- headers: {
- 'x-xsrf-token': csrftoken,
- 'cookie': kuki,
- 'origin': 'https://www.clipto.com',
- 'referer': 'https://www.clipto.com/id/media-downloader/youtube-downloader',
- 'content-type': 'application/json',
- 'user-agent': 'Mozilla/5.0'
- },
- body: JSON.stringify({ url })
- });
- const data = await cliptoRes.json();
- title = data.title;
- thumbnail = data.thumbnail;
- if (!duration && data.duration) {
- duration = data.duration;
- }
- const mp4WithAudio = data.medias.find(v =>
- v.ext === 'mp4' && (v.is_audio === true || v.audioQuality)
- );
- if (mp4WithAudio) {
- mp4 = mp4WithAudio.url;
- }
- } catch (err) {
- // fallback failed
- }
- const result = {
- title,
- thumbnail,
- mp4,
- mp3: mp3Url,
- duration: duration ? Math.round(duration) + 's' : null
- };
- return new Response(JSON.stringify(result, null, 2), {
- headers: { 'Content-Type': 'application/json' }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment