Merge pull request #21 from oceanprotocol/feature/youtube-latest

add videos querying by channel
This commit is contained in:
Matthias Kretschmann 2018-10-31 14:45:30 +01:00 committed by GitHub
commit 09ff97ff78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 96 additions and 29 deletions

View File

@ -43,15 +43,43 @@ 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"
},
{
...
}]
```
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

View File

@ -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": {

View File

@ -6,27 +6,72 @@ 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(error)
}
if (json.error) {
return cb(json.error)
}
return cb(videos)
})
}
app.get('/', (req, res) => {
res.send('Please use /channel or /playlist endpoints, appended with the channel or 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('/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}`,
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,20 +86,14 @@ app.get('/:playlist', (req, res) => {
})
})
app.get('/:playlist/raw', (req, res) => {
app.get('/playlist/:playlistId/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.playlistId}&key=${req.webtaskContext.secrets.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)
})
})