61 lines
1.5 KiB
JavaScript
61 lines
1.5 KiB
JavaScript
const search_url = "https://www.youtube.com/results?pbj=1&search_query=";
|
|
|
|
const headers = {
|
|
"X-YouTube-Client-Name": "1",
|
|
"X-YouTube-Client-Version": "2.20200319.09.00",
|
|
"Accept-Language": "en-US,en;q=0.5",
|
|
};
|
|
|
|
async function* mediaIterator(search) {
|
|
const result = await fetch(search_url + search, {
|
|
headers,
|
|
}).then((c) => c.json());
|
|
|
|
const videos = result?.[1]?.response?.contents?.twoColumnSearchResultsRenderer
|
|
?.primaryContents?.sectionListRenderer?.contents?.[0]?.itemSectionRenderer
|
|
?.contents;
|
|
|
|
if (videos == undefined) {
|
|
return;
|
|
}
|
|
|
|
for (const content of videos) {
|
|
if (content?.videoRenderer != undefined) {
|
|
const video = content.videoRenderer;
|
|
if (video.videoId == undefined) {
|
|
continue;
|
|
}
|
|
|
|
yield {
|
|
title: video.title?.accessibility?.accessibilityData?.label,
|
|
id: video.videoId,
|
|
};
|
|
} else if (content?.playlistRenderer != undefined) {
|
|
const playl = content.playlistRenderer;
|
|
if (playl.playlistId == undefined) {
|
|
continue;
|
|
}
|
|
|
|
yield {
|
|
title: playl.title?.simpleText,
|
|
id: playl.playlistId,
|
|
count: playl.videoCount,
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
if (Deno.args.length == 0) {
|
|
console.error("Expected search keywords");
|
|
Deno.exit(1);
|
|
}
|
|
|
|
for await (const media of mediaIterator(Deno.args.join("+"))) {
|
|
console.log(
|
|
media.title +
|
|
(media.count != undefined ? " <" + media.count + " Videos>" : "") +
|
|
":",
|
|
);
|
|
console.log("ytdl://" + media.id);
|
|
}
|