webtasks/webtask-youtube.js

92 lines
2.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()
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) {
return
}
return cb(videos)
})
}
2018-03-21 12:43:05 +01:00
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-10-31 13:07:48 +01:00
app.get('/channel/:channelId', (req, res) => {
2018-03-21 12:43:05 +01:00
const options = {
2018-10-31 13:07:48 +01: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}`,
2018-03-21 12:43:05 +01:00
headers: { 'referer': req.headers.host }
}
2018-10-31 13:07:48 +01:00
const parsedPosts = []
let holder = {}
2018-03-21 12:43:05 +01:00
2018-10-31 13:07:48 +01:00
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 = {}
2018-03-21 12:43:05 +01:00
}
2018-10-31 13:07:48 +01:00
res.send(parsedPosts)
})
})
app.get('/playlist/:playlistId', (req, res) => {
const options = {
2018-10-31 13:54:13 +01: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}`,
2018-10-31 13:07:48 +01:00
headers: { 'referer': req.headers.host }
}
const parsedPosts = []
let holder = {}
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
holder.videoUrl = `https://www.youtube.com/watch?v=${videos[i].snippet.resourceId.videoId}`
parsedPosts.push(holder)
holder = {}
}
res.send(parsedPosts)
})
})
2018-10-31 13:07:48 +01:00
app.get('/playlist/: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)