yt-dlx
Version:
Effortless Audio-Video Downloader And Streamer!
35 lines • 1.69 kB
JavaScript
// Suggestion: Add JSDoc comments to the function, explaining its purpose, parameters, and return value. Also, consider adding more robust error handling, such as retrying the authentication process or providing more specific error messages to the user. Avoid using global variables and instead pass the `TubeType` instance as a parameter to the functions that need it.
import * as fs from "fs";
import * as path from "path";
import colors from "colors";
import { Innertube, UniversalCache } from "youtubei.js";
export let Tube = null;
export default async function TubeLogin(cookiesFilePathOrString) {
let cookiesData;
if (fs.existsSync(cookiesFilePathOrString)) {
try {
cookiesData = fs.readFileSync(cookiesFilePathOrString, "utf8");
}
catch (error) {
console.error(colors.red("@error:"), "Failed to read cookies file.");
process.exit(1);
}
}
else
cookiesData = cookiesFilePathOrString;
try {
Tube = await Innertube.create({
user_agent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
cache: new UniversalCache(true, path.join(process.cwd(), "YouTubeDLX")),
cookie: cookiesData,
});
console.log(colors.green("@info:"), "Connected to YouTube...");
return Tube;
}
catch (err) {
console.error(colors.red("@error:"), "Failed to authenticate. The cookies appear to be corrupt or invalid.");
console.error(colors.red("@error:"), "Try using valid YouTube cookies.");
process.exit(1);
}
}
//# sourceMappingURL=TubeLogin.js.map