add videos querying by channel ID

This commit is contained in:
Matthias Kretschmann 2018-10-31 13:07:48 +01:00
parent bac7e7c694
commit 650aae46dc
Signed by: m
GPG Key ID: 606EEEF3C479A91F
3 changed files with 71 additions and 19 deletions

View File

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

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,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 }