From da96f842282a191b05c60eeeda51ab9e64a8c538 Mon Sep 17 00:00:00 2001 From: capitanwesler Date: Sun, 14 Mar 2021 13:17:45 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A5=20Implementing=20video=20functions?= =?UTF-8?q?=20to=20get=20the=20videos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/UtilsController.ts | 30 ++++++++++++++++++++++++++++++ src/routes.ts | 1 + 2 files changed, 31 insertions(+) diff --git a/src/controllers/UtilsController.ts b/src/controllers/UtilsController.ts index 62eb6cd..1c18d24 100644 --- a/src/controllers/UtilsController.ts +++ b/src/controllers/UtilsController.ts @@ -169,4 +169,34 @@ export default class UtilsController { res.status(500).json({ message: 'Aruppi lost in the shell' }); } } + + async getVideos(req: Request, res: Response, next: NextFunction) { + const { channelId } = req.params; + let data: any; + + try { + data = await requestGot( + `${urls.BASE_YOUTUBE}${channelId}&part=snippet,id&order=date&maxResults=50`, + { scrapy: false, parse: true }, + ); + } catch (err) { + return next(err); + } + + const results: any[] = data.items.map((item: any) => { + return { + title: item.snippet.title, + videoId: item.id.videoId, + thumbDefault: item.snippet.thumbnails.default.url, + thumbMedium: item.snippet.thumbnails.medium.url, + thumbHigh: item.snippet.thumbnails.high.url, + }; + }); + + if (results.length > 0) { + res.status(200).json({ videos: results }); + } else { + res.status(500).json({ message: 'Aruppi lost in the shell' }); + } + } } diff --git a/src/routes.ts b/src/routes.ts index b20822e..ad59790 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -95,5 +95,6 @@ routes.get('/api/v4/search/:title', directoryController.search); routes.get('/api/v4/anitakume', utilsController.getAnitakume); routes.get('/api/v4/news', utilsController.getNews); routes.get('/api/v4/images/:title', utilsController.getImages); +routes.get('/api/v4/videos/:channelId', utilsController.getVideos); export default routes;