forked from Genez-io/express-typescript-getting-started
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-api.ts
45 lines (36 loc) · 1.04 KB
/
server-api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
require('dotenv').config()
import express from 'express';
import axios from 'axios';
const YOUTUBE_PLAYLIST_ID = 'PLXEUJjGpEX7zuX1CKGVfspeG0dhsXhTrL';
const YOUTUBE_API_KEY = process.env['YOUTUBE_API_KEY'];
interface PlaylistItem {
kind: string;
etag: string;
id: string;
contentDetails: ContentDetails;
}
interface ContentDetails {
videoId: string;
videoPublishedAt: string;
}
const api = express.Router();
api.get('/list-latest-videos', async (req, res) => {
const playlistItemsResponse = await axios.get(
'https://www.googleapis.com/youtube/v3/playlistItems',
{
params: {
playlistId: YOUTUBE_PLAYLIST_ID,
key: YOUTUBE_API_KEY,
part: 'contentDetails',
},
}
);
const videoUrls = playlistItemsResponse.data.items
.filter((item: PlaylistItem) =>
Boolean(item.contentDetails.videoPublishedAt)
)
.map((item: PlaylistItem) => item.contentDetails.videoId)
.map((videoId: string) => `https://www.youtube.com/embed/${videoId}`);
res.json(videoUrls);
});
export { api };