-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
245 lines (206 loc) · 5.47 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//! itc-report.js
//! version : 0.0.1
//! authors : Marek Serafin
//! license : MIT
//! github.com/stoprocent/itc-report
var async = require('async');
request = require('request'),
cheerio = require('cheerio'),
moment = require('moment'),
fs = require('fs'),
path = require('path'),
_ = require('underscore');
/**
* Expose `iTunesConnect`
*/
exports = module.exports = iTunesConnect;
/**
* Expose `ReportQuery`.
*/
exports.ReportQuery = ReportQuery;
/**
* Initialize a new `iTunesConnect` with the given `username`, `password` and `options`.
*
* @param {String} flags
* @param {String} description
* @api public
*/
function iTunesConnect(username, password, options) {
this._cookies = [];
this.options = {
loginURL : "https://itunesconnect.apple.com",
apiURL : "https://reportingitc2.apple.com/api/",
concurrentRequests : 3
};
_.extend(this.options, options);
// Task Executor
this._queue = async.queue(
this.executeRequest.bind(this),
this.options.concurrentRequests
);
this._queue.pause();
this.login(username, password);
}
iTunesConnect.prototype.request = function(query, completed) {
this._queue.push({query: query, completed: completed});
return this;
}
iTunesConnect.prototype.executeRequest = function(task, callback) {
var query = task.query;
var completed = task.completed;
console.log(query.body());
request.post({
url : this.options.apiURL + query.endpoint,
body : query.body(),
headers : {
'Content-Type': 'application/json',
'Cookie': this._cookies
}
}, function(error, response, body) {
if(response.statusCode == 401) {
error = new Error('This request requires authentication. Please check your username and password.');
body = null;
}
else {
try {
body = JSON.parse(body);
} catch (e) {
error = new Error('There was an error while parsing JSON.');
body = null;
}
}
completed(error, body);
callback();
})
}
iTunesConnect.prototype.login = function(username, password) {
var self = this;
// Request ITC to get fresh post action
request.get(this.options.loginURL, function(error, response, body) {
// Handle Errors
// Search for action attribute
var html = cheerio.load(body);
var action = html('form').attr('action');
// Login to ITC
request.post({
url : self.options.loginURL + action,
form: {
'theAccountName' : username,
'theAccountPW' : password,
'theAuxValue' : ""
}
}, function(error, response, body) {
var cookies = response.headers['set-cookie'];
// Handle Errors
if(error || !cookies.length) {
}
else {
// Set Cookies and emit event
self._cookies = cookies;
self._queue.resume();
}
});
});
}
function ReportQuery(query) {
this.type = null;
this.endpoint = null;
this._body = {
start : moment(),
end : moment(),
interval : 'day',
filters : [],
group : null,
measures : ['units'],
limit : 100
};
// Extend options with user stuff
_.extend(this._body, query);
// Private Options
this._body._time = null;
}
ReportQuery.prototype.body = function() {
// Ensure date is moment object
this._body.start = parseToMoment(this._body.start);
this._body.end = parseToMoment(this._body.end);
// If start and end date are same and time() was used in query calculate new start date
if (!this._body.end.diff(this._body.start, 'days') && _.isArray(this._body._time)) {
this._body.start = this._body.start.subtract(this._body._time[0], this._body._time[1]);
}
// Ensure if ranked and group = null set group to default
if(this.type == 'ranked' && this._body.group == null) {
this._body.group = 'content';
}
// Building body
var body = {
"start_date" : this._body.start.format("YYYY-MM-DD[T00:00:00.000Z]"),
"end_date" : this._body.end.format("YYYY-MM-DD[T00:00:00.000Z]"),
"interval" : this._body.interval,
"filters" : this._body.filters,
"group" : this._body.group,
"measures" : this._body.measures,
"limit" : this._body.limit
};
return JSON.stringify(body);
}
ReportQuery.prototype.timed = function() {
this.type = 'timed';
this.endpoint = 'data/timeseries';
return this;
}
ReportQuery.prototype.ranked = function() {
this.type = 'ranked';
this.endpoint = 'data/ranked';
return this;
}
ReportQuery.prototype.interval = function(value) {
this._body.interval = value;
return this;
}
ReportQuery.prototype.date = function(start, end) {
this._body.start = parseToMoment( start );
this._body.end = parseToMoment( ((typeof end == 'undefined') ? start : end) );
return this;
}
ReportQuery.prototype.time = function(value, unit) {
this._body._time = [value, unit];
return this;
}
ReportQuery.prototype.group = function(value) {
this._body.group = value;
return this;
}
ReportQuery.prototype.measures = function(value) {
this._body.measures = value;
return this;
}
ReportQuery.prototype.limit = function(value) {
this._body.limit = value;
return this;
}
ReportQuery.prototype.addFilters = function(dimension, values) {
if (!_.isArray(values)) {
values = [values];
};
this._body.filters.push({
dimension_key : dimension,
option_keys : values
});
}
ReportQuery.prototype.content = function(value) {
this.addFilters('content', value);
return this;
}
function parseToMoment(date) {
// Quick check if moment
if(moment.isMoment(date)) {
return date;
}
else if(date instanceof Date) {
return moment(date);
}
else if(typeof date === "string" && !!(date.match(new RegExp(/([0-9]{4})-([0-9]{2})-([0-9]{2})/)))) {
return moment(date, "YYYY-MM-DD");
}
return false;
}