aaronshappell / picabot

A discord bot for didney worl who has an appetite for non-nutritive substances
MIT License
3 stars 7 forks source link

Add youtube playlist support #11

Open aaronshappell opened 7 years ago

aaronshappell commented 7 years ago

Be able to add songs from a youtube playlist link

aaronshappell commented 7 years ago

Parse the users message to !addsong to see if it contains &list= meaning it is a playlist link. Then use the youtube api to get a list of urls of the songs in the playlist and add them to the queue. https://developers.google.com/youtube/v3/docs/playlistItems/list

dylan-thinnes commented 7 years ago

Some old hacky-code I cobbled together a while ago to download all the video URLs (part after ?v=) for a given playlist. This code SHOULD NOT be used in production, it uses unnecessary global variables and most importantly has nested callbacks.

var https = require('https');
const googleAPIKey = "AIzaSyATdBFjBBgA5r_GELdAzqbyGpi4x8mKkBo";
const playlistId = "PLJ5l1Fqq-I3trtQ5ab9kvukUcCvko_Kn3";
const baseAPIEndpoint = "/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=" + playlistId + "&fields=items(snippet(resourceId%2FvideoId%2Ctitle))%2CnextPageToken&key="+googleAPIKey;
var playlistAPIData = [];
var playlistAPIDataLength = 0;

var printResults = function () {
    for (var ii = 0; ii < playlistAPIData.length; ii++) {
        console.log(playlistAPIData[ii].snippet.resourceId.videoId);
    }
}
var parseAPIData = function (response) {
    var responseDataString = "";
    response.on("data", function (chunk) {
        responseDataString += chunk;
    });
    response.on("end", function () {
        var responseData = JSON.parse(responseDataString);
        Array.prototype.push.apply(playlistAPIData,responseData.items);
        if (responseData.nextPageToken !== undefined) {
            https.get({
                protocol: "https:",
                host: "www.googleapis.com",
                path: baseAPIEndpoint+"&pageToken="+responseData.nextPageToken
            }).on("response",parseAPIData);
        }else{
            //console.log(playlistAPIData);
            //console.log(playlistAPIData.length);
            printResults();
        }
    });
}
https.get({
    protocol: "https:",
    host: "www.googleapis.com",
    path: baseAPIEndpoint
}).on("response",parseAPIData);