Skip to content

Commit

Permalink
show video duration beside the name
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydp17 committed Nov 30, 2017
1 parent cfe3739 commit 6d7c2da
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
Expand Down
22 changes: 21 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

0 comments on commit 6d7c2da

Please sign in to comment.