Skip to content

Commit

Permalink
Get a playlist content & duration
Browse files Browse the repository at this point in the history
Soon, we'll be able to display data !
  • Loading branch information
Elanis committed Aug 21, 2021
1 parent 1752094 commit 0557f82
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
95 changes: 95 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const fetch = require('node-fetch');

const fs = require('fs');

const PLAYLIST_ID = '';
const API_KEY = '';

async function getVideoData(videoId) {
const res = await fetch(
`https://www.googleapis.com/youtube/v3/videos?id=${videoId}&key=${API_KEY}`
);

return await res.json();
}

async function getVideosData(videosId) {
const res = await fetch(
`https://youtube.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&id=${videosId.join(',')}&key=${API_KEY}`
);

return await res.json();
}

const iso8601DurationRegex = /(-)?P(?:([.,\d]+)Y)?(?:([.,\d]+)M)?(?:([.,\d]+)W)?(?:([.,\d]+)D)?T(?:([.,\d]+)H)?(?:([.,\d]+)M)?(?:([.,\d]+)S)?/;
function parseISO8601Duration(iso8601Duration) {
const matches = iso8601Duration.match(iso8601DurationRegex);

return {
sign: matches[1] === undefined ? '+' : '-',
years: matches[2] === undefined ? 0 : parseInt(matches[2]),
months: matches[3] === undefined ? 0 : parseInt(matches[3]),
weeks: matches[4] === undefined ? 0 : parseInt(matches[4]),
days: matches[5] === undefined ? 0 : parseInt(matches[5]),
hours: matches[6] === undefined ? 0 : parseInt(matches[6]),
minutes: matches[7] === undefined ? 0 : parseInt(matches[7]),
seconds: matches[8] === undefined ? 0 : parseInt(matches[8])
};
}

async function getPlayListData(playlistId) {
const finalResult = [];
let obj = {};
do {
let res;

if(obj.nextPageToken) {
res = await fetch(
`https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=${playlistId}&key=${API_KEY}&pageToken=${obj.nextPageToken}`
);
} else {
res = await fetch(
`https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=${playlistId}&key=${API_KEY}`
);
}

obj = await res.json();
const videos = await getVideosData(obj.items.map((elt) => elt.snippet.resourceId.videoId));
finalResult.push(...obj.items.map((elt) => {
elt.videoData = videos.items.filter((video) =>
video.id === elt.snippet.resourceId.videoId
)[0];

return elt;
}));
} while(obj.nextPageToken);

return finalResult;
}

(async() => {
const playlistData = await getPlayListData(PLAYLIST_ID, 0);

console.log('Video count: ', playlistData.length);
const totalDuration = playlistData.reduce((acc, item) => {
const duration = parseISO8601Duration(item.videoData.contentDetails.duration);

acc.hours += duration.days * 24;
acc.hours += duration.hours;
acc.minutes += duration.minutes;
acc.seconds += duration.seconds;

while(acc.seconds > 60) {
acc.seconds -= 60;
acc.minutes++;
}

while(acc.minutes > 60) {
acc.minutes -= 60;
acc.hours++;
}

return acc;
}, { hours: 0, minutes: 0, seconds: 0 });
console.log('Total duration: ', totalDuration.hours.toString().padStart(2, '0') + ':' + totalDuration.minutes.toString().padStart(2, '0') + ':' + totalDuration.seconds.toString().padStart(2, '0'));
})();
31 changes: 31 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "lumen-module-youtube",
"version": "1.0.0",
"description": "Youtube Module",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"node-fetch": "^2.6.1"
}
}

0 comments on commit 0557f82

Please sign in to comment.