forked from analog-nico/quota
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathyoutube.js
60 lines (49 loc) · 1.65 KB
/
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
'use strict';
var _ = require('lodash');
var moment = require('moment');
var Manager = require('../manager.js');
/**
* Quota Preset for YouTube Analytics API
*
* Quota rules based on: https://developers.google.com/youtube/analytics/v1/quota
* As well as the information provided in the Google Developer Console
*
* YouTube Analytics API docs: https://developers.google.com/youtube/analytics/
*
* YouTube Data API and YouTube Reporting API not yet supported.
*
* In a cluster environment a local Server can be used if all requests on
* behalf a particular user are only made by a single node.js instance.
*
* @param options
* @returns {Manager}
*/
module.exports = function (options) {
// Note: Daily quotas refresh at midnight PST.
function getStartOfNextWindow() {
return moment.utc().startOf('day').add(1, 'day').add(8, 'hours').valueOf();
}
_.defaults(options, {
requestsPerSecondPerUser: 12
});
var manager = new Manager({
backoff: 'exponential' // Reference implementation: https://github.com/google/google-http-java-client/blob/346a896b201a784533fab6f5d0214dc62db21a70/google-http-client/src/main/java/com/google/api/client/util/ExponentialBackOff.java
});
manager.addRule({
limit: 100000,
throttling: {
type: 'window-fixed',
getStartOfNextWindow: getStartOfNextWindow
},
resource: 'requests'
});
manager.addRule({
limit: options.requestsPerSecondPerUser,
window: 1000,
throttling: 'window-sliding',
queueing: 'fifo',
scope: 'userId',
resource: 'requests'
});
return manager;
};