From 650aae46dc046313181af5c88a819fb12285d9c8 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Wed, 31 Oct 2018 13:07:48 +0100 Subject: [PATCH 1/4] add videos querying by channel ID --- README.md | 30 ++++++++++++++++++++---- package.json | 2 +- webtask-youtube.js | 58 +++++++++++++++++++++++++++++++++++----------- 3 files changed, 71 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 983de78..cba76ce 100644 --- a/README.md +++ b/README.md @@ -43,15 +43,37 @@ https://TASK_URL/TASK_NAME/:medium_username ### YouTube -**`webtask-youtube.js`**: Generic task to fetch and reconstruct items from any YouTube account. For now, only fetches a playlist. YouTube API key is provided via [secret environment variable](https://webtask.io/docs/issue_parameters) `YOUTUBE_API_KEY` setup in web editor of webtask.io. +**`webtask-youtube.js`**: Generic task to fetch and reconstruct items from any YouTube account. YouTube API key is provided via [secret environment variable](https://webtask.io/docs/issue_parameters) `YOUTUBE_API_KEY` setup in web editor of webtask.io. -Construct your request url like so, e.g. locally: +* `/playlist/:playlistId`: returns all videos in the given playlist. +* `/channel/:channelId`: returns latest 10 videos in the given channel, sorted by date. + +Construct your `GET` request urls like so: ```bash -http://localhost:8080/:youtube_playlist_id +http://localhost:8080/playlist/:youtube_playlist_id +http://localhost:8080/channel/:youtube_channel_id # when published on webtask.io -https://TASK_URL/TASK_NAME/:youtube_playlist_id +https://TASK_URL/TASK_NAME/playlist/:youtube_playlist_id +https://TASK_URL/TASK_NAME/channel/:youtube_channel_id +``` + +**Response** + +For all endpoints, response is reconstructed in the same format: + +```json +[{ + "id": "8EMowpNlWPE", + "title": "Fang Gong, Researcher - Bringing Privacy to Ocean Protocol", + "description": "At Ocean Protocol, we unlock data for AI and use TCRs to maintain a registry of high-quality data sets through voting. Privacy is essential to protecting the ...", + "imageUrl": "https://i.ytimg.com/vi/8EMowpNlWPE/mqdefault.jpg", + "videoUrl": "https://www.youtube.com/watch?v=8EMowpNlWPE" +}, +{ + ... +}] ``` ### Bounties diff --git a/package.json b/package.json index 01546ea..fbb9c5b 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.1.0", "private": true, "scripts": { - "start": "wt serve webtask-bounties.js", + "start": "wt serve webtask-youtube.js", "test": "eslint ./*.js" }, "dependencies": { diff --git a/webtask-youtube.js b/webtask-youtube.js index 10e2474..fcb708e 100644 --- a/webtask-youtube.js +++ b/webtask-youtube.js @@ -6,27 +6,57 @@ const webtask = require('webtask-tools') const app = express() -app.get('/', (req, res) => { - res.send('Please specify the playlist ID as parameter.') -}) - -app.get('/:playlist', (req, res) => { - const options = { - url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&playlistId=${req.params.playlist}&key=${req.webtaskContext.secrets.YOUTUBE_API_KEY}`, - headers: { 'referer': req.headers.host } - } - +const makeRequest = (options, cb) => { request(options, (error, response, body) => { const json = JSON.parse(body) const videos = json.items - const parsedPosts = [] - - let holder = {} if (error) { return } + return cb(videos) + }) +} + +app.get('/', (req, res) => { + res.send('Please specify the playlist ID as parameter.') +}) + +app.get('/channel/:channelId', (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 } + } + + const parsedPosts = [] + let holder = {} + + makeRequest(options, (videos) => { + for (let i = 0; i < videos.length; i++) { + holder.id = videos[i].id.videoId + holder.title = videos[i].snippet.title + holder.description = videos[i].snippet.description + holder.imageUrl = videos[i].snippet.thumbnails.medium.url + holder.videoUrl = `https://www.youtube.com/watch?v=${videos[i].id.videoId}` + parsedPosts.push(holder) + holder = {} + } + + res.send(parsedPosts) + }) +}) + +app.get('/playlist/:playlistId', (req, res) => { + const options = { + url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&playlistId=${req.params.playlistID}&key=${req.webtaskContext.secrets.YOUTUBE_API_KEY}`, + headers: { 'referer': req.headers.host } + } + + const parsedPosts = [] + let holder = {} + + makeRequest(options, (videos) => { for (let i = 0; i < videos.length; i++) { holder.id = videos[i].snippet.resourceId.videoId holder.title = videos[i].snippet.title @@ -41,7 +71,7 @@ app.get('/:playlist', (req, res) => { }) }) -app.get('/:playlist/raw', (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}`, headers: { 'referer': req.headers.host } From 2fb4660f5e0f387615a586b9ff3f6eb8dda2f395 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Wed, 31 Oct 2018 13:54:13 +0100 Subject: [PATCH 2/4] return 10 results for both endpoints --- webtask-youtube.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webtask-youtube.js b/webtask-youtube.js index fcb708e..0b9ff87 100644 --- a/webtask-youtube.js +++ b/webtask-youtube.js @@ -49,7 +49,7 @@ app.get('/channel/:channelId', (req, res) => { app.get('/playlist/:playlistId', (req, res) => { const options = { - url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&playlistId=${req.params.playlistID}&key=${req.webtaskContext.secrets.YOUTUBE_API_KEY}`, + url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=10&playlistId=${req.params.playlistID}&key=${req.webtaskContext.secrets.YOUTUBE_API_KEY}`, headers: { 'referer': req.headers.host } } From 05bea0e95db2a9f40cfdecf9febac55a2b2954d9 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Wed, 31 Oct 2018 14:02:37 +0100 Subject: [PATCH 3/4] 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) }) }) From e863ebc4b0289cc50602cf22b89d340e68573820 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Wed, 31 Oct 2018 14:32:07 +0100 Subject: [PATCH 4/4] fixes --- webtask-youtube.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/webtask-youtube.js b/webtask-youtube.js index 79b8f5b..26f0677 100644 --- a/webtask-youtube.js +++ b/webtask-youtube.js @@ -12,7 +12,11 @@ const makeRequest = (options, cb) => { const videos = json.items if (error) { - return + return cb(error) + } + + if (json.error) { + return cb(json.error) } return cb(videos) @@ -60,7 +64,7 @@ app.get('/channel/:channelId/raw', (req, res) => { 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}`, + url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=10&playlistId=${req.params.playlistId}&key=${req.webtaskContext.secrets.YOUTUBE_API_KEY}`, headers: { 'referer': req.headers.host } } @@ -82,9 +86,9 @@ app.get('/playlist/:playlistId', (req, res) => { }) }) -app.get('/playlist/:playlist/raw', (req, res) => { +app.get('/playlist/:playlistId/raw', (req, res) => { const options = { - url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=10&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.playlistId}&key=${req.webtaskContext.secrets.YOUTUBE_API_KEY}`, headers: { 'referer': req.headers.host } }