Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No longer works #20

Open
Beastskyler opened this issue Dec 16, 2020 · 2 comments
Open

No longer works #20

Beastskyler opened this issue Dec 16, 2020 · 2 comments

Comments

@Beastskyler
Copy link

Beastskyler commented Dec 16, 2020

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
Copy link
Author

Beastskyler commented Dec 24, 2020

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
Copy link
Author

Beastskyler commented Dec 28, 2020

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();

})();
`


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant