CodeDotJS / youtube-playlist

:snowflake: Extract links, ids, and names from a youtube playlist
MIT License
89 stars 19 forks source link

No longer works #20

Open Beastskyler opened 3 years ago

Beastskyler commented 3 years ago

I have finally started to get back into coding a found out this no longer works.

const commando = require('discord.js-commando');
const ytlist = require('youtube-playlist');
var fs = require('fs');
var path = require('path');
const { exec } = require("child_process");

class PlaylistChannelCommand extends commando.Command {
    constructor(client) {
        super(client, {
            name: 'playlist',
            group: 'music',
            memberName: 'playlist',
            description: 'Adds playlist to the random music bot',
            args: [
                {
                    key: 'link',
                    prompt: 'What playlist do you want to add?',
                    type: 'string'
                }
            ]
        });
    }

    async run(message, { link }) {

        function restart() {
            exec("pm2 restart music", (error, stdout, stderr) => {
                if (error) {
                    console.log(`error: ${error.message}`);
                }
                if (stderr) {
                    console.log(`stderr: ${stderr}`);
                }
                console.log(`stdout: ${stdout}`);
            });
        }

        var pathToFile = path.resolve(__dirname, "../../queue.txt");

        ytlist(link, 'url').then(res => {
            let pls = res.data.playlist.toString().split(',').join('\n');
            fs.appendFile(pathToFile, "\n" + pls, 'utf8',
                function (err) {
                    if (err) throw err;
                    console.log("Data is appended to file successfully.");
                    restart();
                    message.channel.send("Success, " + res.data.playlist.length + ' songs added!');
                });
        });
    }
}

module.exports = PlaylistChannelCommand;

Running this command adds no urls. I am a little more comfortable coding and would like to try and help, however, I don't know how much help I could be.

Beastskyler commented 3 years ago

I have started doing it with puppeteer, right now it only does links and still only does 100

const link = "https://www.youtube.com/playlist?list=PLTo6svdhIL1dvXNr1OujUEdRm885KDYoB";
const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch({ headless: true });
    const page = await browser.newPage();
    await page.goto(link);
    await page.setViewport({
        width: 1200,
        height: 800
    });

    await page.waitForSelector("a.yt-simple-endpoint.style-scope.ytd-playlist-video-renderer");

    const amountOfItems = await page.evaluate(() => {
        return document.querySelector("#stats > yt-formatted-string:nth-child(1) > span:nth-child(1)").innerText;
    });

    const linkList = await page.evaluate(() => {
        var arr = [];
        // if (amountOfItems <= 100) {
            var links = document.querySelectorAll("#content > a.yt-simple-endpoint.style-scope.ytd-playlist-video-renderer");
            links.forEach(element => {
                var currentLink = element.getAttribute("href");
                currentLink = currentLink.substring(0,currentLink.indexOf("&"));
                arr.push("https://www.youtube.com" + currentLink);
            });
            return arr;
        // }
    });
    console.log(linkList);
    await browser.close();
})();
Beastskyler commented 3 years ago

Spent so much time, made me realize how much I don't know. Tbh I don't know how I did it, but I got rid of the limit. Someone please fix and make this code better.


` const link = "https://www.youtube.com/playlist?list=PLuxyDVDEQgJXUN-89ChfO3mDtWgPxMaf9"; const puppeteer = require('puppeteer');

(async () => { function delay(time) { return new Promise(function (resolve) { setTimeout(resolve, time) }); }

const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto(link);
await page.setViewport({
    width: 1200,
    height: 800
});
const amountOfItems = await page.evaluate(() => {
    var num = document.querySelector("#stats > yt-formatted-string:nth-child(1) > span:nth-child(1)").innerText;
    if(num.includes(",")){
        num = parseInt(num.replace(/,/g, ''), 10);
    }
    return num;
});

if (amountOfItems > 100) {
    var amountOfScrolls = amountOfItems / 100;
    for(i = 0; i < amountOfScrolls; i++){
        await page.evaluate( () => {
            window.scrollBy(0, window.innerHeight * 16);
        });
        await delay(1500);
    }

}
await page.waitForSelector("a.yt-simple-endpoint.style-scope.ytd-playlist-video-renderer");

const linkList = await page.evaluate(() => {
    var arr = [];
    var links = document.querySelectorAll("#content > a.yt-simple-endpoint.style-scope.ytd-playlist-video-renderer");
    links.forEach(element => {
        var currentLink = element.getAttribute("href");
        currentLink = currentLink.substring(0, currentLink.indexOf("&"));
        arr.push("https://www.youtube.com" + currentLink);
    });
    return arr;
});
console.log(linkList);
console.log(linkList.length);
await browser.close();

})(); `