Skip to content

Instantly share code, notes, and snippets.

@donnaken15

donnaken15/000.js

Last active Jul 14, 2020
Embed
What would you like to do?
YouTube playlist video lister (50+ videos)
/* https://www.googleapis.com/youtube/v3/
playlistItems?playlistId=PLTWDLMN4U1FNBJll-GvoNce8PQTuXjFYh
&part=snippet&maxResults=50&pageToken=...
&key=YOUR_API_KEY */
var listcur = 0, pgtok, data, videos = new Array(), vidcnt;
getvids = function() {
for (var i = 0; i < data.items.length; i++)
{
videos[listcur] = data.items[i].snippet.resourceId.videoId;
listcur++;
}
}
var __ = new XMLHttpRequest();
//__.open("GET","https://www.googleapis.com/youtube/v3/playlistItems?playlistId=PLTWDLMN4U1FNBJll-GvoNce8PQTuXjFYh&part=snippet&key=KEY&maxResults=50",true);
__.onreadystatechange = function() {
if (this.readyState == XMLHttpRequest.DONE && this.status === 200)
{
data = JSON.parse(__.responseText);
console.log("got next "+data.pageInfo.resultsPerPage.toString()+" videos");
getvids();
pgtok = data.nextPageToken;
}
}
var _ = new XMLHttpRequest();
_.open("GET","https://www.googleapis.com/youtube/v3/playlistItems?playlistId=PLTWDLMN4U1FNBJll-GvoNce8PQTuXjFYh&part=snippet&key=KEY&maxResults=50",false);
_.onreadystatechange = function() {
if (this.readyState == XMLHttpRequest.DONE && this.status === 200)
{
data = JSON.parse(_.responseText);
console.log("got first 50 videos");
listcur = 0; vidcnt = data.pageInfo.totalResults;
getvids(); pgtok = data.nextPageToken;
for (var i = data.pageInfo.resultsPerPage; i < vidcnt; i += data.pageInfo.resultsPerPage) {
__.open("GET","https://www.googleapis.com/youtube/v3/playlistItems?playlistId=PLTWDLMN4U1FNBJll-GvoNce8PQTuXjFYh&part=snippet&key=KEY&maxResults=50&pageToken="+pgtok,false);
__.send();
}
console.log("complete");
return videos;
}
}
_.send();
shuffle = function() {
var a = [];
// i couldve made my own unique rng generator but am too lazy to right now
while(a.length < vidcnt){
var r = Math.floor(Math.random() * vidcnt) + 1;
if(a.indexOf(r) === -1) a.push(r);
}
for (i = 0; i < vidcnt; i++)
{
a[i] = videos[a[i]]
}
return a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.