-
Notifications
You must be signed in to change notification settings - Fork 1
/
ooyala.js
190 lines (171 loc) · 6.75 KB
/
ooyala.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use strict';
/* ------------------------------------------------------------
*
* A request formatter for Ooyala API
* based on arguments of the .apiRequest() method
* an Ooyala request is formatted and the response returned
*
* The api must be initialiased with a config
* ooyala.init({
*
* api_key: 'Rjsgjsdyuzxjhjss53453.xzgkS',
* secret_key: 'uysiIYYIkjnkkSb80e-3DfKhjdfs7iB',
* baseUrl: 'http://api.ooyala.com',
* include: ['labels']
*
* })
*
* NB: keys above are fake examples. Use your own keys
*
* -------------------------------------------------------------
*/
var request = require('request'),
crypto = require('crypto'),
_ = require('underscore'),
moment = require('moment-timezone');
module.exports = {
init: function(config) {
_.extend(this, config);
return this;
},
defaults: {
query: {
created_at_start: '1 day'
},
count: 50,
path: '/v2/assets',
method: 'GET'
},
// retruns apiRequest Promise
apiRequest: function(args) {
var self = this,
method, path, query, count;
if (args.method === undefined) {
method = self.defaults.method;
} else {
method = args.method;
}
if (args.path === undefined) {
path = self.defaults.path;
} else {
path = args.path;
}
if (args.query === undefined) {
query = self.defaults.query;
} else {
query = args.query;
}
if (args.count === undefined) {
count = self.defaults.count;
} else {
count = args.count;
}
if (args.player_id) {
path = '/v2/players/' + args.player_id;
}
if (args.asset_id) {
path = '/v2/assets/' + args.asset_id;
}
return new Promise(function(resolve, reject) {
var complex = false,
where_clauses = [],
expiration = Math.round(+new Date() / 1000 + 3600);
// if anything but term is being queried its complex
_.each(query, function(value, attr) {
if (attr !== 'term') {
complex = true;
return false;
}
});
_.each(query, function(value, attr) {
switch (attr) {
case 'term':
// can't usefully mix AND and OR in Ooyala query
if (complex) {
where_clauses.push('name INCLUDES \'' + value + '\'');
} else {
where_clauses.push('description INCLUDES \'' + value + '\' OR name INCLUDES \'' + value + '\'' + ' OR labels INCLUDES \'' + value + '\'');
}
break;
case 'label':
_.each(value.split(','), function(part) {
where_clauses.push('labels INCLUDES \'' + part.trim() + '\'');
});
break;
case 'created_at_start':
if (typeof value === 'string') {
where_clauses.push('created_at > \'' + moment().subtract(parseInt(value.split(' ')[0], 10), value.split(' ')[1]).format() + '\'');
} else {
where_clauses.push('created_at > \'' + moment(value).format() + '\'');
}
break;
case 'created_at_end':
if (typeof value === 'string') {
where_clauses.push('created_at <= \'' + moment().subtract(parseInt(value.split(' ')[0], 10), value.split(' ')[1]).format() + '\'');
} else {
where_clauses.push('created_at <= \'' + moment(value).format() + '\'');
}
break;
case 'updated_at_start':
if (typeof value === 'string') {
where_clauses.push('updated_at > \'' + moment().subtract(parseInt(value.split(' ')[0], 10), value.split(' ')[1]).format() + '\'');
} else {
where_clauses.push('updated_at > \'' + moment(value).format() + '\'');
}
break;
case 'updated_at_end':
if (typeof value === 'string') {
where_clauses.push('updated_at <= \'' + moment().subtract(parseInt(value.split(' ')[0], 10), value.split(' ')[1]).format() + '\'');
} else {
where_clauses.push('updated_at <= \'' + moment(value).format() + '\'');
}
break;
}
});
// path = '/v2/assets/5qZmgxeDqA11FdzQpKYuQK94ttSi1WiF';
// path = '/v2/players/ZTIxYmJjZDM2NWYzZDViZGRiOWJjYzc5';
// ../api?asset_id=5qZmgxeDqA11FdzQpKYuQK94ttSi1WiF
// ../api?player_id=ZTIxYmJjZDM2NWYzZDViZGRiOWJjYzc5
// create signature
self.stringToTokenize = self.secret_key +
method + path +
'api_key=' + self.api_key +
'count=' + count +
'expires=' + expiration +
'include=' + self.include.join(',') +
'where=' + where_clauses.join(' AND ');
self.signature = crypto.createHash('sha256')
.update(self.stringToTokenize)
.digest('base64')
.replace(/\=+$/, '').substr(0, 43);
console.log('Ooyala API call', (args.player_id || args.asset_id) ? path : '', {
// api_key: self.api_key,
count: count,
expires: expiration,
include: self.include.join(','),
where: where_clauses.join(' AND '),
// signature: signature
});
request({
baseUrl: self.baseUrl,
url: path,
qs: {
api_key: self.api_key,
count: count,
expires: expiration,
include: self.include.join(','),
where: where_clauses.join(' AND '),
signature: self.signature
},
json: true
}, function(err, r, body) {
if (err) {
console.log(222, err, r);
reject(err);
}
// pass back returned json object
resolve(body);
});
});
}
};