This repository has been archived by the owner on Sep 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webtask-youtube.js
118 lines (98 loc) · 3.41 KB
/
webtask-youtube.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict' // eslint-disable-line
const express = require('express')
const request = require('request')
const webtask = require('webtask-tools')
const app = express()
const makeRequest = (options, cb) => {
request(options, (error, response, body) => {
const json = JSON.parse(body)
const videos = json.items
if (error) {
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
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)
})
})
app.get('/playlist/:playlistId/raw', (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 }
}
makeRequest(options, videos => {
res.send(videos)
})
})
module.exports = webtask.fromExpress(app)