-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (136 loc) · 3.77 KB
/
index.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Thomas Mathews
* 2016 May 30
*/
var async = require('async')
function isOnList (whitelist, opts, done) {
opts.api.whitelists.get({
id: whitelist.id,
onBehalfOfContentOwner: whitelist.owner
}, function (err, obj) {
if (err && err.code != 404) return done(err)
let onList = true
if (obj.data && obj.data.error && obj.data.error.code == 404) {
onList = false
}
done(null, onList)
})
}
function addToList (whitelist, opts, done) {
opts.api.whitelists.insert({
resource: {
kind: "youtubePartner#whitelist",
id: whitelist.id
},
onBehalfOfContentOwner: whitelist.owner
}, function (err, obj) {
done(err, 'added')
})
}
function removeFromList (whitelist, opts, done) {
opts.api.whitelists.delete({
id: whitelist.id,
onBehalfOfContentOwner: whitelist.owner
}, function (err, obj) {
done(err, 'removed')
})
}
/**
* getApi
*
* @desc Basic method to quickly get an API object used for this module.
*/
function getApi (opts, done) {
var scopes = opts.scopes || [
"https://www.googleapis.com/auth/youtubepartner",
]
var keys = opts.keys
var { google } = require('googleapis')
var jwt = new google.auth.JWT(keys.client_email, null, keys.private_key, scopes, null)
jwt.authorize(function (err, tokens) {
if (err) return done(err)
google.options({auth: jwt})
done(null, google.youtubePartner('v1'))
})
}
/**
* @desc Returns the full whitelist of channels from YouTube
*
* @param {string} owner
* @param {object} opts
* @param {object} opts.api Result of getApi call
* @param {string} opts.pageToken For getting the next page of results
* @param {callback} done
* @return {undefined}
*/
function getWhitelist (owner, opts, done) {
if (!opts.api) return done(Error('Api not supplied.'))
opts.api.whitelists.list({
onBehalfOfContentOwner: owner,
pageToken: opts.pageToken
}, (err, result) => {
if (err) {
return done(err)
}
done(null, result.data)
})
}
/**
* handleMany
*
* @desc Uses handleOne to handle many.
*
* @param {array} whitelists - A list of whitelist.
* @param {object} opts - Same options as handleOne.
* @param {callback} done - Supplies an array of object with errors and their whitelist struct.
* @return {undefined}
*/
function handleMany (whitelists, opts, done) {
async.mapSeries(whitelists, function (whitelist, next) {
handleOne(whitelist, opts, function (err, status) {
setTimeout(function delay () {
next(null, {
error: err,
whitelist: whitelist,
status: status
})
}, opts.delay || 0)
})
}, function (err, results) {
done(results)
})
}
/**
* handleOne
*
* @desc Adds or removes channel from owner's whitelist.
*
* @param {object} whitelist - Structure of whitelist.
* @param {string} whitelist.id - The YouTube channel Id.
* @param {string} whitelist.owner - Owner to diff from.
* @param {string} whitelist.on - Wheither to add or remove.
* @param {object} opts - Structure of options.
* @param {object} opts.api - API object to operate with.
* @param {callback} done - The callback that returns an error if unsuccesful.
* @return {undefined}
*/
function handleOne (whitelist, opts, done) {
if (!opts.api) return done(Error('Api not supplied.'))
if (!whitelist.owner) return done(Error('Owner not supplied.'))
isOnList(whitelist, opts, function (err, inlist) {
if (err) return done(err, 'nochange')
if (whitelist.on && !inlist) {
return addToList(whitelist, opts, done)
}
if (!whitelist.on && inlist) {
return removeFromList(whitelist, opts, done)
}
done(null, 'nochange')
})
}
module.exports = {
handleMany: handleMany,
handleOne: handleOne,
getApi: getApi,
getWhitelist: getWhitelist
}