Forked from vikaspotluri123/Youtube Download Full Channel
Created
July 8, 2023 22:04
-
-
Save vvsvin/9f7e85e6c04b3982f083d4a8eb759fd0 to your computer and use it in GitHub Desktop.
Download all of the videos in a youtube channel as mp3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const API_KEY = "API_KEY_HERE" | |
const API_URL = "https://www.googleapis.com/youtube/v3/search" | |
const API_PATH = "/youtube/v3/search" | |
const https = require("https"); // I'm not sure why nodejs https API uses const but I'll trust them :) | |
const exec = require("child_process").exec; | |
var API_PARAMS = | |
{ | |
"key" : API_KEY, | |
"channelId" : "CHANNEL ID", | |
//"channelName" : "Just so you know :)", | |
"part" : "id", | |
"maxResults" : "50", | |
"pageToken" : "" | |
}, | |
options = | |
{ | |
"hostname" : "www.googleapis.com", | |
"port" : 443, | |
"path" : "/youtube/v3/search", | |
"method" : "GET" | |
}, | |
downloaderIndex = 0, | |
videoIDs = [], | |
apiResponse = ""; | |
function generateGETString() | |
{ | |
return "?" + JSON.stringify(API_PARAMS) | |
.replace(/{/g,"") | |
.replace(/\,/g,"&") | |
.replace(/:/g,"=") | |
.replace(/\"/g,"") | |
.replace(/}/,""); | |
} | |
function generateAudioOnlyCommand(videoID) | |
{ | |
return "youtube-dl --extract-audio --audio-quality 0 --audio-format mp3 https://youtube.com/watch?v=" + videoID; | |
} | |
function requestAPI() | |
{ | |
options.path = API_PATH + generateGETString(); | |
console.log(options.path); | |
var req = https.request(options, (res) => | |
{ | |
//console.log('statusCode: ', res.statusCode); | |
//console.log('headers: ', res.headers); | |
res.on('data', (d) => | |
{ | |
apiResponse += d; | |
//process.stdout.write(d); | |
}); | |
res.on('end',() => | |
{ | |
apiResponse = JSON.parse(apiResponse); | |
//console.log(apiResponse.pageInfo,apiResponse.items.length); | |
for(var i = 0 ; i < apiResponse.items.length ; i ++) | |
{ | |
if(apiResponse.items[i]["id"]["kind"] !== "youtube#video") | |
console.log("skipping",apiResponse.items[i]); | |
else videoIDs.push(apiResponse.items[i]["id"]["videoId"]); | |
console.log(apiResponse.items[i]["id"]["kind"]); | |
} | |
if(!(typeof apiResponse["nextPageToken"] === "undefined")) | |
{ | |
API_PARAMS.pageToken = apiResponse["nextPageToken"]; | |
apiResponse = ""; | |
requestAPI(); | |
} | |
else | |
{ | |
console.log("finished",videoIDs.length); | |
syncDownload(); | |
} | |
}); | |
}); | |
req.end(); | |
req.on('error', (e) => {console.error(e);}); | |
} | |
function syncDownload() | |
{ | |
//console.log(videoIDs); | |
//console.log(generateAudioOnlyCommand(videoIDs[downloaderIndex])); return; | |
console.log("Running " + downloaderIndex); | |
exec(generateAudioOnlyCommand(videoIDs[downloaderIndex]),{/*maxBuffer:10000000 //commenting out becuase I don't think I'll need it :P*/},function(e,stdout,stderr) | |
{ | |
console.log("Downloaded " + videoIDs[downloaderIndex]); | |
console.log(e); | |
console.log(stderr); | |
console.log(stdout); | |
console.log(); | |
console.log(); | |
console.log(); | |
console.log(); | |
//console.log(videoIDs.length); | |
//console.log(downloaderIndex); | |
if(++downloaderIndex > videoIDs.length - 1) | |
console.log("Downloader Index:" + downloaderIndex + " DONE!"); | |
else | |
syncDownload(); | |
}); | |
} | |
try | |
{ | |
process.chdir('DOWNLOAD_DIRECTORY'); | |
//console.log('New directory: ' + process.cwd()); | |
} | |
catch(err) | |
{ | |
console.log('chdir: ' + err); | |
} | |
requestAPI(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment