Spaces:
Paused
Paused
import got from 'got'; | |
import { | |
ConvertResponseSchema, | |
LinkItemSchema, | |
YoutubedlResponseSchema, | |
} from '../types/youtubedl-v1.js'; | |
import { | |
YoutubeDownloaderArgsSchema, | |
YoutubedlSchema, | |
} from '../types/youtube-v1.js'; | |
import { DEFAULT_HEADERS } from '../constant.js'; | |
import { parseFileSize } from './util.js'; | |
export async function youtubedl(url, server) { | |
YoutubeDownloaderArgsSchema.parse(arguments); | |
const form = { | |
k_query: url, | |
k_page: 'home', | |
hl: server || 'en', | |
q_auto: 0 | |
}; | |
const data = await got.post('https://www.y2mate.com/mates/analyzeV2/ajax', { | |
headers: { | |
...DEFAULT_HEADERS, | |
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8', | |
cookie: '_ga=GA1.1.1058493269.1720585210; _ga_PSRPB96YVC=GS1.1.1720585209.1.1.1720585486.0.0.0', | |
origin: 'https://www.y2mate.com' | |
}, | |
form | |
}).json(); | |
const json = YoutubedlResponseSchema.parse(data); | |
const video = {}; | |
const audio = {}; | |
const other = {}; | |
for (const key in json.links) { | |
for (const tag in json.links[key]) { | |
const data = json.links[key][tag]; | |
const quality = data.q; | |
const type = data.f; | |
const fileSizeH = data.size; | |
const fileSize = parseFileSize(fileSizeH); | |
(type === 'mp4' ? video : type === 'mp3' ? audio : other)[quality.toLowerCase()] = { | |
quality, | |
type, | |
fileSizeH, | |
fileSize, | |
download: convert.bind(convert, json.vid, data.k) | |
}; | |
} | |
} | |
const result = { | |
id: json.vid, | |
thumbnail: `https://i.ytimg.com/vi/${json.vid}/0.jpg`, | |
title: json.title, | |
duration: json.t, | |
video, | |
audio, | |
other | |
}; | |
console.log(result); | |
return YoutubedlSchema.parse(result); | |
} | |
export async function convert(vid, k) { | |
const form = { | |
vid, | |
k | |
}; | |
try { | |
const data = await got.post('https://www.y2mate.com/mates/convertV2/index', { | |
headers: { | |
...DEFAULT_HEADERS, | |
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8', | |
cookie: '_ga=GA1.1.1058493269.1720585210; _ga_PSRPB96YVC=GS1.1.1720585209.1.1.1720585486.0.0.0', | |
origin: 'https://www.y2mate.com' | |
}, | |
form | |
}).json(); | |
const json = ConvertResponseSchema.parse(data); | |
if (!json.dlink) { | |
throw new Error("Download link is missing"); | |
} | |
return json.dlink; | |
} catch (error) { | |
console.error("Error in convert function:", error); | |
return ""; | |
} | |
} |