1
0
mirror of https://github.com/oceanprotocol/webtasks synced 2024-11-24 02:55:13 +01:00
webtasks/webtask-youtube.js

62 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-03-21 12:43:05 +01:00
'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) => {
2018-06-28 19:22:37 +02:00
res.send('Please specify the playlist ID as parameter.')
2018-03-21 12:43:05 +01:00
})
2018-04-05 11:36:56 +02:00
app.get('/:playlist', (req, res) => {
2018-03-21 12:43:05 +01:00
const options = {
2018-04-05 11:36:56 +02:00
url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&playlistId=${req.params.playlist}&key=${req.webtaskContext.secrets.YOUTUBE_API_KEY}`,
2018-03-21 12:43:05 +01:00
headers: { 'referer': req.headers.host }
}
request(options, (error, response, body) => {
const json = JSON.parse(body)
2018-06-27 20:52:40 +02:00
const videos = json.items
2018-03-21 12:43:05 +01:00
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)
})
})
2018-04-05 11:36:56 +02:00
app.get('/:playlist/raw', (req, res) => {
2018-03-21 12:43:05 +01:00
const options = {
2018-04-05 11:36:56 +02:00
url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&playlistId=${req.params.playlist}&key=${process.env.YOUTUBE_API_KEY}`,
2018-03-21 12:43:05 +01:00
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)