Skip to content

Instantly share code, notes, and snippets.

@dnicolson
Created July 29, 2019 17:34
Show Gist options
  • Save dnicolson/71501fe9d06d575f18dbfeb8878e6986 to your computer and use it in GitHub Desktop.
Save dnicolson/71501fe9d06d575f18dbfeb8878e6986 to your computer and use it in GitHub Desktop.
Aggregate videos from multiple channels in the last hours
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