From 05bea0e95db2a9f40cfdecf9febac55a2b2954d9 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Wed, 31 Oct 2018 14:02:37 +0100 Subject: [PATCH] add raw responses --- README.md | 6 ++++++ webtask-youtube.js | 25 +++++++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index cba76ce..8c05dd9 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,12 @@ For all endpoints, response is reconstructed in the same format: }] ``` +If you want to get the original response returned from YouTube API, append `/raw` at the end of your request, e.g.: + +``` +https://TASK_URL/TASK_NAME/playlist/:youtube_playlist_id/raw +``` + ### Bounties **`webtask-bounties.js`**: Task to fetch open bounties on Gitcoin and Bounties.network in one request. Task creates a unified response from fetching both networks. diff --git a/webtask-youtube.js b/webtask-youtube.js index 0b9ff87..79b8f5b 100644 --- a/webtask-youtube.js +++ b/webtask-youtube.js @@ -20,7 +20,7 @@ const makeRequest = (options, cb) => { } app.get('/', (req, res) => { - res.send('Please specify the playlist ID as parameter.') + res.send('Please use /channel or /playlist endpoints, appended with the channel or playlist ID as parameter.') }) app.get('/channel/:channelId', (req, res) => { @@ -47,6 +47,17 @@ app.get('/channel/:channelId', (req, res) => { }) }) +app.get('/channel/:channelId/raw', (req, res) => { + const options = { + url: `https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=${req.params.channelId}&maxResults=10&order=date&type=video&key=${req.webtaskContext.secrets.YOUTUBE_API_KEY}`, + headers: { 'referer': req.headers.host } + } + + makeRequest(options, (videos) => { + res.send(videos) + }) +}) + app.get('/playlist/:playlistId', (req, res) => { const options = { url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=10&playlistId=${req.params.playlistID}&key=${req.webtaskContext.secrets.YOUTUBE_API_KEY}`, @@ -73,18 +84,12 @@ app.get('/playlist/:playlistId', (req, res) => { app.get('/playlist/:playlist/raw', (req, res) => { const options = { - url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&playlistId=${req.params.playlist}&key=${process.env.YOUTUBE_API_KEY}`, + url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=10&playlistId=${req.params.playlist}&key=${process.env.YOUTUBE_API_KEY}`, headers: { 'referer': req.headers.host } } - request(options, (error, response, body) => { - const json = JSON.parse(body) - - if (error) { - return - } - - res.send(json.items) + makeRequest(options, (videos) => { + res.send(videos) }) })