Last active
July 14, 2020 03:27
-
-
Save donnaken15/637fe61c6b938b1e16033d54c5e1f1f7 to your computer and use it in GitHub Desktop.
YouTube playlist video lister (50+ videos)
This file contains 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
/* 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