Skip to content

Commit

Permalink
feat: added get videos to functions
Browse files Browse the repository at this point in the history
Signed-off-by: veralex <alexey.verevkin@akvelon.com>

feat: added get videos to functions
  • Loading branch information
alexakvelon authored and poiana committed Apr 11, 2023
1 parent 6534d2f commit 6a61a63
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
15 changes: 12 additions & 3 deletions assets/js/community-videos.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,27 @@ import { format } from "date-fns";
}`
);
const data = await response.json();

const videoIds = data.map(
({ resourceId: { videoId } }) => videoId
);
const videosResponse = await fetch(
`/.netlify/functions/youtube-bypass?type=videos&id=${videoIds.join(
","
)}`
);
const videos = await videosResponse.json();

const el = document.getElementById(playlist_id);


const items = data
.map(({ resourceId: { videoId: id }, title, publishedAt, thumbnails }) => {
.map(({ resourceId: { videoId: id }, title, publishedAt, thumbnails }, i) => {
let tpl = template.replace("%img_src%", thumbnails?.standard?.url);
tpl = tpl.replaceAll("%id%", id);
tpl = tpl.replace("%title%", title);
tpl = tpl.replace(
"%published%",
format(new Date(publishedAt), "MMM dd, yyyy")
format(new Date(videos[i].publishedAt), "MMM dd, yyyy")
);
return tpl.trim();
})
Expand Down
28 changes: 20 additions & 8 deletions assets/js/videos.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,27 @@ async function handler() {
return;
}

data.map((videos, i) => {
data.map(async (playlistItems, i) => {
const videoIds = playlistItems.map(
({ resourceId: { videoId } }) => videoId
);
const response = await fetch(
`/.netlify/functions/youtube-bypass?type=videos&id=${videoIds.join(
","
)}`
);
const videos = await response.json();

const el = document.getElementById(ids[i].id);
const items = videos.map(
({
resourceId: { videoId: id },
description,
title,
publishedAt: published,
}) => replace(template, { id, description, title, published })

const items = playlistItems.map(
({ resourceId: { videoId: id }, description, title }, i) =>
replace(template, {
id,
description,
title,
published: videos[i].publishedAt,
})
);
el.innerHTML = items.join();
});
Expand Down
15 changes: 11 additions & 4 deletions functions/youtube-bypass.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ const mapToReponse = (response) => response.items.map(({ snippet }) => snippet);

exports.handler = async ({ queryStringParameters }, context) => {
try {
const { id, take } = queryStringParameters;
const url = `${API_URL}/playlistItems?part=snippet&playlistId=${id}&maxResults=${
take || 1
}&key=${process.env.YOUTUBE_API_KEY}`;
const { id, take, type } = queryStringParameters;
let url;
switch (type) {
case "videos":
url = `${API_URL}/videos?key=${process.env.YOUTUBE_API_KEY}&part=snippet&id=${id}`;
break;
default: // playlist is default
url = `${API_URL}/playlistItems?key=${process.env.YOUTUBE_API_KEY}&part=snippet&playlistId=${id}&maxResults=${take || 1}`;
break;
}

const response = await fetch(url);
const json = await response.json();

Expand Down

0 comments on commit 6a61a63

Please sign in to comment.