diff --git a/README.md b/README.md index 3905b84..b7d202c 100644 --- a/README.md +++ b/README.md @@ -13,27 +13,36 @@ Requires the Medium username appended at the end of the url, e.g. locally: -```js -fetch('http://localhost:4000/oceanprotocol') - .then(res => res.json()) - .then(posts => { - const lastPosts = posts.slice(0, 3) - }) +``` +http://localhost:4000/MEDIUM_USERNAME ``` When published as a web task, append the taskname followed by the Medium username at the end: -```js -fetch('https://wt-bfc3ae9804422f8a4ea114dc7c403296-0.run.webtask.io/medium/oceanprotocol') - .then(res => res.json()) - .then(posts => { - const lastPosts = posts.slice(0, 3) - }) +``` +https://TASK_URL/TASK_NAME/MEDIUM_USERNAME +``` + +--- + +**`webtask-youtube.js`**: Generic task to fetch and reconstruct items from any YouTube account. For now, only fetches a playlist. + +Construct your request url like so, e.g. locally: + +``` +http://localhost:4000/YOUTUBE_PLAYLIST_ID/YOUTUBE_API_KEY +``` + +Add the task name when published on webtask.io: + +``` +https://TASK_URL/TASK_NAME/YOUTUBE_PLAYLIST_ID/YOUTUBE_API_KEY ``` ## Development ```bash +npm install wt-cli -g npm start ``` @@ -51,6 +60,11 @@ wt create webtask-medium.js --name medium wt ls ``` +## Authors + +- Matthias Kretschmann ([@kremalicious](https://github.com/kremalicious)) - [BigchainDB](https://www.bigchaindb.com) & [Ocean Protocol](https://oceanprotocol.com) +- initial Medium web task by [@pedrouid](https://github.com/pedrouid) + ## License ``` diff --git a/webtask-youtube.js b/webtask-youtube.js new file mode 100644 index 0000000..78d0571 --- /dev/null +++ b/webtask-youtube.js @@ -0,0 +1,61 @@ +'use strict' // eslint-disable-line + +const express = require('express') +const request = require('request') +const webtask = require('webtask-tools') + +const app = express() + +app.get('/', (req, res) => { + res.send('Please specify the playlist ID and API key.') +}) + +app.get('/:playlist/:apikey', (req, res) => { + const options = { + url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&playlistId=${req.params.playlist}&key=${req.params.apikey}`, + headers: { 'referer': req.headers.host } + } + + request(options, (error, response, body) => { + const json = JSON.parse(body) + const videos = json.items // eslint-disable-line prefer-destructuring + const parsedPosts = [] + + let holder = {} + + if (error) { + return + } + + for (let i = 0; i < videos.length; i++) { + holder.id = videos[i].snippet.resourceId.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].snippet.resourceId.videoId}` + parsedPosts.push(holder) + holder = {} + } + + res.send(parsedPosts) + }) +}) + +app.get('/:playlist/:apikey/raw', (req, res) => { + const options = { + url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&playlistId=${req.params.playlist}&key=${req.params.apikey}`, + headers: { 'referer': req.headers.host } + } + + request(options, (error, response, body) => { + const json = JSON.parse(body) + + if (error) { + return + } + + res.send(json.items) + }) +}) + +module.exports = webtask.fromExpress(app)