Created
July 29, 2019 17:34
-
-
Save dnicolson/71501fe9d06d575f18dbfeb8878e6986 to your computer and use it in GitHub Desktop.
Aggregate videos from multiple channels in the last hours
This file contains hidden or 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
const fetchVideos = (channels, apiKey, lastHours) => { | |
return Promise.all( | |
channels.map(channel => { | |
const url = `https://www.googleapis.com/youtube/v3/search?key=${apiKey}&channelId=${channel}&part=snippet,id&order=date&maxResults=20`; | |
return fetch(url) | |
.then(response => response.json()) | |
.then(json => { | |
return json.items | |
.filter(item => item.id.kind === 'youtube#video') | |
.filter(item => { | |
const now = new Date(); | |
const then = new Date(item.snippet.publishedAt); | |
const timeDiffInHours = parseInt((now - then) / 1000) / 60 / 60; | |
return timeDiffInHours < lastHours; | |
}) | |
.map(item => { | |
return { | |
id: item.id.videoId, | |
title: item.snippet.title, | |
description: item.snippet.description, | |
date: item.snippet.publishedAt, | |
image: item.snippet.thumbnails.high.url, | |
}; | |
}); | |
}); | |
}), | |
).then(videos => { | |
return videos.flat().sort((a, b) => { | |
return new Date(b.date) - new Date(a.date); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment