-
Notifications
You must be signed in to change notification settings - Fork 2
/
stream.js
160 lines (121 loc) · 2.83 KB
/
stream.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
149
150
151
152
153
154
155
156
157
158
159
160
var logger = require('../../logger.js');
var util = require('util');
var google = require('googleapis');
var youtube = google.youtube('v3');
var OAuth2 = google.auth.OAuth2;
var Stream = require('../core').Stream;
var Responder = require('./responder.js');
var Rotator = require('../core').Rotator;
var Fetch = require('./fetcher.js');
/**
* Manages the connection to Youtube
* @constructor
* @extends Stream
*/
var YoutubeStream = function(service, credentials, accounts, keywords) {
Stream.call(this, service, credentials, accounts, keywords);
// Set credentials
this.oauth = new OAuth2(
credentials.client_id,
credentials.client_secret,
credentials.redirect_uri
);
// Initialize rotator
this.rotator = new Rotator(5000);
// Initialize responder
this.responder = new Responder();
}
/** Inherit */
util.inherits(YoutubeStream, Stream);
/**
* Start the stream
*/
YoutubeStream.prototype.start = function() {
if (!this.rotator.active()) {
logger.warn(this._service, 'Stream:', 'starting...');
this.rotator.start();
}
}
/**
* Stop the stream
*/
YoutubeStream.prototype.stop = function() {
logger.warn(this._service, 'Stream:', 'stopping...');
this.rotator.stop();
}
/**
* Restart stream
*/
YoutubeStream.prototype.restart = function() {
logger.warn(this._service, 'Stream:', 'Restart initiated');
this.stop();
process.nextTick(function() {
this.start();
}.bind(this)
);
}
/**
* Keywords
*/
YoutubeStream.prototype.keywords = function() {
return this._keywords.map(function(n) {
return n.phrase;
});
}
/**
* Add Keyword
*/
YoutubeStream.prototype.addKeyword = function(keyword) {
this._keywords.push(keyword);
}
/**
* Remove Keyword
*/
YoutubeStream.prototype.removeKeyword = function(keyword) {
this._keywords.remove(function(n) {
return n.phrase === keyword.phrase;
});
}
/**
* Accounts
*/
YoutubeStream.prototype.accounts = function() {
return this._accounts.map(function(n) {
return n.service_id;
});
}
/**
* Add Account
*/
YoutubeStream.prototype.addAccount = function(account) {
var self = this;
this._accounts.push(account);
this.rotator.add({
context: self,
key: JSON.stringify(account),
params: {
account: account
},
func: function() {
Fetch(this.oauth, account, function(videos) {
videos.forEach(function(video) {
this.host.emit('post', video);
});
});
account.last_checked = new Date();
}
});
if (!this.rotator.active()) this.rotator.start();
}
/**
* Remove Account
*/
YoutubeStream.prototype.removeAccount = function(account) {
var self = this;
this._accounts.remove(function(n) {
return n.service_id === account.service_id;
});
this.rotator.remove(JSON.stringify(account));
}
/** EXPORT */
module.exports = YoutubeStream;