1
0
mirror of https://github.com/oceanprotocol/webtasks synced 2024-11-27 21:00:07 +01:00

new youtube task

This commit is contained in:
Matthias Kretschmann 2018-03-21 12:43:05 +01:00
parent da48fc4cab
commit b03d2cbe0e
Signed by: m
GPG Key ID: 606EEEF3C479A91F
2 changed files with 87 additions and 12 deletions

View File

@ -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
```

61
webtask-youtube.js Normal file
View File

@ -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)