-
Notifications
You must be signed in to change notification settings - Fork 0
/
CityadsApi.js
294 lines (272 loc) · 9.11 KB
/
CityadsApi.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
const fetch = require('node-fetch');
const CITYADS_API_URL = 'https://cityads.com/api/rest/webmaster/json/';
const STATUS_REJECTED = 'rejected';
const STATUS_OPEN = 'open';
const STATUS_APPROVED = 'approved';
class CityadsApi {
static STATUS_REJECTED = STATUS_REJECTED;
static STATUS_OPEN = STATUS_OPEN;
static STATUS_APPROVED = STATUS_APPROVED;
constructor(webmasterToken) {
this.token = webmasterToken;
}
async getProfile() {
let profile = await this.apiRequest('profile');
profile.id = Number(profile.id);
return profile;
}
async getBalance() {
let data = await this.apiRequest('balanceinfo');
return {
currency: data.currency,
mainBalance: Number(data.main_balance),
holdAdv: Number(data.commission_hold_adv),
holdCity: Number(data.commission_hold_city),
availableBalance: Number(data.available_balance),
}
}
async chargeHistoryList() {
let action = 'chargehistorylist';
let params = new Map();
let result = [];
let start = 0;
let limit = 1000;
let apiData;
do {
params.set('start', start);
params.set('limit', limit);
apiData = await this.apiRequest(action, params);
result = result.concat(apiData.items || [])
start++;
} while (apiData.total > limit * start)
return result;
}
async getTrafficChannels() {
let data = await this.apiRequest('traffic_channels');
let result = [];
for (let i in data.items) {
if (data.items[i].is_active === '1') {
result.push({
id: Number(data.items[i].id),
name: data.items[i].name
});
}
}
return result;
}
async getOfferDataByOfferId(offerId) {
return await this.apiRequest('offer/' + offerId);
}
async getOffersData(offerIds, channelId) {
let action = 'offers/web';
let params = new Map();
if (offerIds) {
params.set('ids', offerIds.join(','));
}
if (channelId) {
params.set('traffic_channel_id', channelId);
}
let result = {};
let start = 0;
let limit = 1000;
let apiData;
do {
params.set('start', start);
params.set('limit', limit);
apiData = await this.apiRequest(action, params);
if (!apiData) {
return false;
}
result = {...result, ...apiData.items};
start++;
} while (apiData.total > limit * start)
return result;
}
async getCrByOfferId(dateFrom, dateTo, offerId, channelId = null) {
dateFrom = this.#toCityadsFormatDate(dateFrom);
dateTo = this.#toCityadsFormatDate(dateTo);
let items = await this.getStatisticsOffersByOfferId(dateFrom, dateTo, offerId, channelId);
if (!items.length) {
return false;
}
let cr = items[0].cr * 100;
let clicks = items[0].clicks;
let leads = items[0].leadsRejected + items[0].leadsOpen + items[0].leadsApproved;
return {cr, clicks, leads, offerId, channelId};
}
async getLeadsByOfferId(dateFrom, dateTo, offerId = null, channelId = null, xid = '') {
dateFrom = this.#toCityadsFormatDate(dateFrom);
dateTo = this.#toCityadsFormatDate(dateTo);
let action = 'orderstatistics/' + dateFrom + '/' + dateTo;
let params = new Map();
params.set('date_type', 'order_upload');
if (offerId) {
params.set('action_id', offerId);
}
if (channelId) {
params.set('channel_id', channelId);
}
if (xid) {
params.set('xid', xid);
}
let result = [];
let start = 0;
let limit = 1000;
let apiData;
do {
params.set('start', start);
params.set('limit', limit);
apiData = await this.apiRequest(action, params);
if (apiData && Array.isArray(apiData.items)) {
apiData.items.map(item => {
item.orderId = item.submissionID;
item.offerId = Number(item.offerID);
item.status = this.#getLeadStatus(item.status);
item.commission = this.#getLeadCommissionByStatus(item);
item.leadTime = this.#getTimestampByTextDate(item.leadTime);
item.uploadTime = this.#getUploadTime(item.status, item.saleTime);
});
result = result.concat(apiData.items)
}
start++;
} while (apiData.total > limit * start)
return result;
}
/**
* short grouped statistics by offer and chanel
* @return items{offerId,clicks,leadsOpen,commissionOpen,...}
*/
async getStatisticsOffersByOfferId(dateFrom, dateTo, offerId = null, channelId = null, group = null) {
dateFrom = this.#toCityadsFormatDate(dateFrom);
dateTo = this.#toCityadsFormatDate(dateTo);
let action = 'statistics-offers/action_id/' + dateFrom + '/' + dateTo;
let params = new Map();
if (offerId) {
params.set('action_id', offerId);
}
if (channelId) {
params.set('channel_id', channelId);
}
if (group) {
params.set('sub_group', group);
}
let result = [];
let start = 0;
let limit = 1000;
let apiData;
do {
params.set('start', start);
params.set('limit', limit);
apiData = await this.apiRequest(action, params);
if (apiData && Array.isArray(apiData.items)) {
result = result.concat(apiData.items.map(item => ({
offerId: Number(item.actionID) || 0,
channelId: Number(item.channelId) || 0,
offerName: item.actionName || '',
clicks: Number(item.clickCount) || 0,
backUrlCount: Number(item.backUrlRedirectCount) || 0,
leadsRejected: Number(item.saleCancelled) || Number(item.leadsCancelled) || 0,
leadsOpen: Number(item.saleOpen) || Number(item.leadsOpen) || 0,
leadsApproved: Number(item.saleApproved) || Number(item.leadsApproved) || 0,
commissionRejected: item.commissionCancelled ? Number(item.commissionCancelled.toFixed(2)) : 0,
commissionOpen: item.commissionOpen ? Number(item.commissionOpen.toFixed(2)) : 0,
commissionApproved: item.commissionApproved ? Number(item.commissionApproved.toFixed(2)) : 0,
cr: Number(item.crTotal) || 0,
})));
}
start++;
} while (apiData.total > limit * start)
return result;
}
async getWebmasterCommissions(dateFrom, dateTo, offerId = null) {
let stats = await this.getStatisticsOffersByOfferId(dateFrom, dateTo, offerId);
let commissionRejected = 0;
let commissionOpen = 0;
let commissionApproved = 0;
for (let item of stats) {
commissionRejected = Number((commissionRejected + item.commissionRejected).toFixed(2));
commissionOpen = Number((commissionOpen + item.commissionOpen).toFixed(2));
commissionApproved = Number((commissionApproved + item.commissionApproved).toFixed(2));
}
return {commissionRejected, commissionOpen, commissionApproved};
}
async getOfferLinksByOfferId(offerId, channelId) {
let action = 'offer-links/' + offerId;
let params = new Map();
if (channelId) {
params.set('traffic_channel_id', channelId);
}
let result = await this.apiRequest(action, params);
if (result && Array.isArray(result.items)) {
return result.items.filter(item => item.is_default);
}
return false;
}
#toCityadsFormatDate(timestamp) {
let mm = new Date(timestamp).getMonth() + 1;
let dd = new Date(timestamp).getDate();
return [new Date(timestamp).getFullYear(), (mm > 9 ? '' : '0') + mm, (dd > 9 ? '' : '0') + dd].join('-');
}
#getUploadTime(status, saleTime) {
if (status === STATUS_APPROVED) {
return saleTime ? this.#getTimestampByTextDate(saleTime) : Date.now();
}
if (status === STATUS_REJECTED) {
return Date.now();
}
return null;
}
#getLeadStatus(status) {
switch (status) {
case 'Open':
case 'Открытая':
return STATUS_OPEN;
case 'Одобрена':
case 'Approved':
return STATUS_APPROVED;
case 'Отклонена':
case 'Rejected':
return STATUS_REJECTED;
default:
return status;
}
}
#getTimestampByTextDate(datetime) {
if (!datetime) {
return null;
}
datetime = datetime.split(' ');
let date = datetime[0].split('.');
return Date.parse(date[2] + '-' + date[1] + '-' + date[0] + ' ' + datetime[1] + ' ' + datetime[2]);
}
#getLeadCommissionByStatus(lead) {
switch (lead.status) {
case STATUS_APPROVED:
return lead.commissionApproved;
case STATUS_OPEN:
return lead.commissionOpen;
case STATUS_REJECTED:
return lead.commissionCancelled;
default:
lead.commissionApproved || lead.commissionOpen || lead.commissionCancelled;
}
}
async apiRequest(action, params = new Map()) {
params.set('remote_auth', this.token)
let url = new URL(action, CITYADS_API_URL).toString() + '?' + new URLSearchParams(params).toString();
// console.info('cityApiRequest', new Date().toLocaleString(), url);
let result;
try {
result = await (await fetch(url)).json();
} catch (e) {
console.error('cityads api error', e);
return false;
}
// console.info('cityResult', new Date().toLocaleString(), result);
if (!result.error && result.status === 200 && result.data) {
return result.data;
}
return false;
}
}
module.exports = CityadsApi;