Skip to content

Instantly share code, notes, and snippets.

@KalenWessel
Last active February 4, 2019 07:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KalenWessel/a12344a397bf7129cfd9 to your computer and use it in GitHub Desktop.
Save KalenWessel/a12344a397bf7129cfd9 to your computer and use it in GitHub Desktop.
// Bunch of javascript from the YouTubes API sample code which allows me to grab all my daily
// subsciption videos and insert them into a single playlist for easy listening.
// Currently I would have to open each song into a tab and listen to them one by one. Now I can
// just hit 'Play All' on the playlist and listen to all the new posts.
// Define some variables used to remember state.
var playlistId = 'PLyDoEZ08Zam3n_bPBi2ylc_NZV--spj5_';
var channelId;
var playlistArray = [];
// After the API loads, call a function to enable the playlist creation form.
function handleAPILoaded() {
enableForm();
}
// Enable the form for creating a playlist.
function enableForm() {
$('#playlist-button').attr('disabled', false);
}
function updatePlaylist() {
parsePlaylist();
parseSubscription();
}
// Iterates through the videos on "My Subscription" page and compares them against the playlist.
// If the video exists in both lists it returns true, otherwise it returns false.
function checkForDuplicates(playlistVideos, video) {
if (playlistVideos.indexOf(video) > -1) {
return true;
} else {
return false;
}
}
// Parse all the video IDs from the daily s so that we can check for duplicates
function parsePlaylist() {
$.getJSON('https://gdata.youtube.com/feeds/api/playlists/PLyDoEZ08Zam3n_bPBi2ylc_NZV--spj5_/?v=2&alt=json&format=5', function(data) {
var feed = data.feed;
var entries = feed.entry || [];
var urlIds = [];
for (var i = 0; i < entries.length; i++) {
var entry = entries[i];
urlIds.push(entry['media$group']['yt$videoid']['$t']);
}
playlistArray = urlIds;
});
}
// Sleep function to rate limit how quickly we add vidoes to the playlist.
// Calls the checkforDuplicate function and if the video doesn't exist in the playlist
// it called the addToPlaylist function with the video idea as the argument.
function callback(id) {
var ID = id;
return function() {
if (checkForDuplicates(playlistArray, id)) {
return true;
} else {
addToPlaylist(id);
}
}
}
// Parse the subscription feed so that we only grab the video IDs.
function parseSubscription() {
$.getJSON('https://gdata.youtube.com/feeds/api/users/sofakingeuro/newsubscriptionvideos?v=2&alt=json&format=5', function(data) {
var feed = data.feed;
var entries = feed.entry || [];
var urlIds = [];
for (var i = 0; i < entries.length; i++) {
var entry = entries[i];
var title = entry.title.$t;
var id = entry.id.$t;
var sanitizedId = id.replace('tag:youtube.com,2008:video:', '');
urlIds.push(sanitizedId);
}
for (var x = 0; x < urlIds.length; x++) {
setTimeout( callback(urlIds[x]), 1000 * x);
}
});
}
// Add a video ID specified in the form to the playlist.
function addVideoToPlaylist(id) {
addToPlaylist(id.val());
}
// Add a video to a playlist. The "startPos" and "endPos" values let you
// start and stop the video at specific times when the video is played as
// part of the playlist. However, these values are not set in this example.
function addToPlaylist(id, startPos, endPos) {
var details = {
videoId: id,
kind: 'youtube#video'
}
if (startPos != undefined) {
details['startAt'] = startPos;
}
if (endPos != undefined) {
details['endAt'] = endPos;
}
var request = gapi.client.youtube.playlistItems.insert({
part: 'snippet',
resource: {
snippet: {
playlistId: playlistId,
resourceId: details
}
}
});
request.execute(function(response) {
$('#status').html('<pre>' + JSON.stringify(response.result) + '</pre>');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment