Ok so i've been struggling with this one for some time and can't seem to figure out how to do it. I wanted to do a test with a bunch of youtube Urls i generated using a random string, basically i goes "https://www.youtube.com/watch?v=XXX" XXX meaning the randomly generated sequence.
What i wanted to try was to differentiate the Urls between the "this video isn't available" from the ones that do work.
But i can't seem to find anywhere on the internet how to run a code that will go through the Urls and tell you which one contains the certain text from the others.
It seems to be a task that requires javascript but i'm not sure how i can use it to meet it.
For now i've been doing it hand to hand double clicking using Notepad but it's so tiring for my wrists lol.
CodePudding user response:
Could you do something like this? And then once you invoke the function just catch the error if thrown and output the ids were applicable?
import fs from "fs";
import ytdl from "ytdl-core";
/**
* @description Download a youtube video to a file for a given video id.
* @param {string} videoId The youtube video id.
* @param {string} outputPath The path to the output file.
* @returns {Promise<void>}
* @example downloadVideo("videoId", "outputPath");
*/
export async function downloadVideo(
videoId: string,
outputPath: string
): Promise<void> {
const video = await ytdl(videoId);
const writeStream = fs.createWriteStream(outputPath);
video.pipe(writeStream);
}
/**
* @description Generate a list of random video ids
* @param {number} count The number of video ids to generate.
* @returns {string[]}
* @example generateVideoIds(10);
*/
function generateVideoIds(count: number): string[] {
const videoIds: string[] = [];
for (let i = 0; i < count; i ) {
const videoId = Math.random().toString(36).substring(2, 15);
videoIds.push(videoId);
}
return videoIds;
}
CodePudding user response:
Wouldn't this be better to use the YouTube API?