diff --git a/src/api.js b/src/api.js index f150c27..13a8cbc 100644 --- a/src/api.js +++ b/src/api.js @@ -34,7 +34,7 @@ async function getVideoDetails(videoId) { const { data } = await axios.get(`${BASE_URL}/videos`, { params: { id: videoId, - part: 'snippet,statistics', + part: 'snippet,statistics,contentDetails', key: YOUTUBE_PLAYLIST_SORTER_API_KEY, }, }); diff --git a/src/index.js b/src/index.js index 7c31a47..b1337d3 100755 --- a/src/index.js +++ b/src/index.js @@ -52,9 +52,29 @@ function prettyPrintVideo(video, index) { let likes = abbreviate(video.statistics.likeCount, 1); if (Number.isNaN(views)) views = 'Disabled'; if (Number.isNaN(likes)) likes = 'Disabled'; - console.log(`${index + 1}. ${video.snippet.title} [👀 ${views} / 👍 ${likes}]`); + const duration = formatIsoDuration(video.contentDetails.duration); + console.log(`${index + 1}. ${video.snippet.title} [👀 ${views} / 👍 ${likes}] (⏳ ${duration})`); console.log(`\thttps://www.youtube.com/watch?v=${video.id}`); console.log(); } +function formatIsoDuration(duration) { + const durationSplits = duration + .replace(/[a-zA-Z]/g, ' ') + .split(' ') + .filter(str => !!str); + return durationSplits.reverse().reduce((acc, item, index) => { + if (index === 0) { + return (`${item}s ` + acc).trim(); + } + if (index === 1) { + return (`${item}m ` + acc).trim(); + } + if (index === 2) { + return (`${item}h ` + acc).trim(); + } + return acc; + }, ''); +} + main();