webtasks/webtask-youtube.js

119 lines
3.4 KiB
JavaScript
Raw Permalink 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()
2018-10-31 13:07:48 +01:00
const makeRequest = (options, cb) => {
request(options, (error, response, body) => {
const json = JSON.parse(body)
const videos = json.items
if (error) {
2018-10-31 14:32:07 +01:00
return cb(error)
}
if (json.error) {
return cb(json.error)
2018-10-31 13:07:48 +01:00
}
return cb(videos)
})
}
2018-03-21 12:43:05 +01:00
app.get('/', (req, res) => {
2019-08-21 11:46:02 +02:00
res.send(
'Please use /channel or /playlist endpoints, appended with the channel or playlist ID as parameter.'
)
2018-03-21 12:43:05 +01:00
})
2018-10-31 13:07:48 +01:00
app.get('/channel/:channelId', (req, res) => {
2018-03-21 12:43:05 +01:00
const options = {
2019-08-21 11:46:02 +02:00
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
}`,
2019-08-19 13:03:48 +02:00
headers: { referer: req.headers.host }
2018-03-21 12:43:05 +01:00
}
2018-10-31 13:07:48 +01:00
const parsedPosts = []
let holder = {}
2018-03-21 12:43:05 +01:00
2019-08-21 11:46:02 +02:00
makeRequest(options, videos => {
2018-10-31 13:07:48 +01:00
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
2019-08-21 11:46:02 +02:00
holder.videoUrl = `https://www.youtube.com/watch?v=${
videos[i].id.videoId
}`
2018-10-31 13:07:48 +01:00
parsedPosts.push(holder)
holder = {}
2018-03-21 12:43:05 +01:00
}
2018-10-31 13:07:48 +01:00
res.send(parsedPosts)
})
})
2018-10-31 14:02:37 +01:00
app.get('/channel/:channelId/raw', (req, res) => {
const options = {
2019-08-21 11:46:02 +02:00
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
}`,
2019-08-19 13:03:48 +02:00
headers: { referer: req.headers.host }
2018-10-31 14:02:37 +01:00
}
2019-08-21 11:46:02 +02:00
makeRequest(options, videos => {
2018-10-31 14:02:37 +01:00
res.send(videos)
})
})
2018-10-31 13:07:48 +01:00
app.get('/playlist/:playlistId', (req, res) => {
const options = {
2019-08-21 11:46:02 +02:00
url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=10&playlistId=${
req.params.playlistId
}&key=${req.webtaskContext.secrets.YOUTUBE_API_KEY}`,
2019-08-19 13:03:48 +02:00
headers: { referer: req.headers.host }
2018-10-31 13:07:48 +01:00
}
const parsedPosts = []
let holder = {}
2019-08-21 11:46:02 +02:00
makeRequest(options, videos => {
2018-03-21 12:43:05 +01:00
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
2019-08-21 11:46:02 +02:00
holder.videoUrl = `https://www.youtube.com/watch?v=${
videos[i].snippet.resourceId.videoId
}`
2018-03-21 12:43:05 +01:00
parsedPosts.push(holder)
holder = {}
}
res.send(parsedPosts)
})
})
2018-10-31 14:32:07 +01:00
app.get('/playlist/:playlistId/raw', (req, res) => {
2018-03-21 12:43:05 +01:00
const options = {
2019-08-21 11:46:02 +02:00
url: `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=10&playlistId=${
req.params.playlistId
}&key=${req.webtaskContext.secrets.YOUTUBE_API_KEY}`,
2019-08-19 13:03:48 +02:00
headers: { referer: req.headers.host }
2018-03-21 12:43:05 +01:00
}
2019-08-21 11:46:02 +02:00
makeRequest(options, videos => {
2018-10-31 14:02:37 +01:00
res.send(videos)
2018-03-21 12:43:05 +01:00
})
})
module.exports = webtask.fromExpress(app)