mirror of
https://github.com/oceanprotocol/webtasks
synced 2024-11-27 21:00:07 +01:00
new youtube task
This commit is contained in:
parent
da48fc4cab
commit
b03d2cbe0e
38
README.md
38
README.md
@ -13,27 +13,36 @@
|
|||||||
|
|
||||||
Requires the Medium username appended at the end of the url, e.g. locally:
|
Requires the Medium username appended at the end of the url, e.g. locally:
|
||||||
|
|
||||||
```js
|
```
|
||||||
fetch('http://localhost:4000/oceanprotocol')
|
http://localhost:4000/MEDIUM_USERNAME
|
||||||
.then(res => res.json())
|
|
||||||
.then(posts => {
|
|
||||||
const lastPosts = posts.slice(0, 3)
|
|
||||||
})
|
|
||||||
```
|
```
|
||||||
|
|
||||||
When published as a web task, append the taskname followed by the Medium username at the end:
|
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')
|
https://TASK_URL/TASK_NAME/MEDIUM_USERNAME
|
||||||
.then(res => res.json())
|
```
|
||||||
.then(posts => {
|
|
||||||
const lastPosts = posts.slice(0, 3)
|
---
|
||||||
})
|
|
||||||
|
**`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
|
## Development
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
npm install wt-cli -g
|
||||||
npm start
|
npm start
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -51,6 +60,11 @@ wt create webtask-medium.js --name medium
|
|||||||
wt ls
|
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
|
## License
|
||||||
|
|
||||||
```
|
```
|
||||||
|
61
webtask-youtube.js
Normal file
61
webtask-youtube.js
Normal 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)
|
Loading…
Reference in New Issue
Block a user