-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·272 lines (250 loc) · 7.95 KB
/
cli.js
File metadata and controls
executable file
·272 lines (250 loc) · 7.95 KB
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
#!/usr/bin/env node
const { Command } = require('commander');
const SearchAdsAPI = require('./index');
const program = new Command();
program
.name('searchads')
.description('CLI for Apple Search Ads API')
.version('1.0.0')
.option('-v, --verbose', 'Enable verbose logging');
// Helper function to create API client
function createClient(options) {
return new SearchAdsAPI({
verbose: options.verbose || false
});
}
// Helper to output results
function output(data) {
console.log(JSON.stringify(data, null, 2));
}
// Helper to handle errors
function handleError(error) {
console.error('Error:', error.message);
if (program.opts().verbose) {
console.error(error.stack);
}
process.exit(1);
}
// Campaigns commands
const campaigns = program.command('campaigns').description('Manage campaigns');
campaigns
.command('list')
.description('List all campaigns')
.option('-l, --limit <number>', 'Limit results (0 for all)', '0')
.option('-o, --offset <number>', 'Offset for pagination', '0')
.action(async (options) => {
try {
const api = createClient(program.opts());
const result = await api.getCampaigns(
parseInt(options.limit),
parseInt(options.offset)
);
output(result);
} catch (error) {
handleError(error);
}
});
campaigns
.command('get')
.description('Get a specific campaign')
.requiredOption('-i, --id <campaignId>', 'Campaign ID')
.action(async (options) => {
try {
const api = createClient(program.opts());
const result = await api.getCampaign(options.id);
output(result);
} catch (error) {
handleError(error);
}
});
campaigns
.command('create')
.description('Create a new campaign')
.requiredOption('-a, --app-id <adamId>', 'App Adam ID')
.requiredOption('-c, --countries <countries>', 'Comma-separated country codes')
.requiredOption('-n, --name <name>', 'Campaign name')
.requiredOption('-b, --budget <amount>', 'Budget amount')
.requiredOption('-d, --daily-budget <amount>', 'Daily budget amount')
.requiredOption('--currency <currency>', 'Currency code (e.g., USD)')
.action(async (options) => {
try {
const api = createClient(program.opts());
const countries = options.countries.split(',');
const result = await api.createCampaign(
parseInt(options.appId),
countries,
options.name,
parseFloat(options.budget),
parseFloat(options.dailyBudget),
options.currency
);
output(result);
} catch (error) {
handleError(error);
}
});
// Ad Groups commands
const adgroups = program.command('adgroups').description('Manage ad groups');
adgroups
.command('list')
.description('List ad groups for a campaign')
.requiredOption('-c, --campaign-id <campaignId>', 'Campaign ID')
.option('-l, --limit <number>', 'Limit results (0 for all)', '0')
.option('-o, --offset <number>', 'Offset for pagination', '0')
.action(async (options) => {
try {
const api = createClient(program.opts());
const result = await api.getAdgroups(
options.campaignId,
parseInt(options.limit),
parseInt(options.offset)
);
output(result);
} catch (error) {
handleError(error);
}
});
adgroups
.command('get')
.description('Get a specific ad group')
.requiredOption('-c, --campaign-id <campaignId>', 'Campaign ID')
.requiredOption('-i, --id <adgroupId>', 'Ad Group ID')
.action(async (options) => {
try {
const api = createClient(program.opts());
const result = await api.getAdgroup(options.campaignId, options.id);
output(result);
} catch (error) {
handleError(error);
}
});
// Keywords commands
const keywords = program.command('keywords').description('Manage targeting keywords');
keywords
.command('list')
.description('List keywords for an ad group')
.requiredOption('-c, --campaign-id <campaignId>', 'Campaign ID')
.requiredOption('-a, --adgroup-id <adgroupId>', 'Ad Group ID')
.option('-l, --limit <number>', 'Limit results (0 for all)', '0')
.option('-o, --offset <number>', 'Offset for pagination', '0')
.action(async (options) => {
try {
const api = createClient(program.opts());
const result = await api.getTargetingKeywords(
options.campaignId,
options.adgroupId,
parseInt(options.limit),
parseInt(options.offset)
);
output(result);
} catch (error) {
handleError(error);
}
});
keywords
.command('add')
.description('Add targeting keywords to an ad group')
.requiredOption('-c, --campaign-id <campaignId>', 'Campaign ID')
.requiredOption('-a, --adgroup-id <adgroupId>', 'Ad Group ID')
.requiredOption('-k, --keywords <json>', 'Keywords as JSON array')
.action(async (options) => {
try {
const api = createClient(program.opts());
const keywords = JSON.parse(options.keywords);
const result = await api.addTargetingKeywords(
options.campaignId,
options.adgroupId,
keywords
);
output(result);
} catch (error) {
handleError(error);
}
});
// Reporting commands
const reports = program.command('reports').description('Get reports');
reports
.command('campaigns')
.description('Get campaign reports')
.requiredOption('-s, --start-date <date>', 'Start date (YYYY-MM-DD)')
.requiredOption('-e, --end-date <date>', 'End date (YYYY-MM-DD)')
.option('-g, --granularity <granularity>', 'Report granularity (HOURLY, DAILY, WEEKLY, MONTHLY)', null)
.option('-l, --limit <number>', 'Limit results (0 for all)', '0')
.action(async (options) => {
try {
const api = createClient(program.opts());
const result = await api.getCampaignsReportByDate(
options.startDate,
options.endDate,
'countryOrRegion',
'ASCENDING',
[],
'countryOrRegion',
true,
options.granularity ? false : true, // No row totals with granularity
options.granularity ? false : true, // No grand totals with granularity
options.granularity,
0,
parseInt(options.limit)
);
output(result);
} catch (error) {
handleError(error);
}
});
reports
.command('adgroups')
.description('Get ad group reports')
.requiredOption('-c, --campaign-id <campaignId>', 'Campaign ID')
.requiredOption('-s, --start-date <date>', 'Start date (YYYY-MM-DD)')
.requiredOption('-e, --end-date <date>', 'End date (YYYY-MM-DD)')
.option('-g, --granularity <granularity>', 'Report granularity (HOURLY, DAILY, WEEKLY, MONTHLY)', null)
.option('-l, --limit <number>', 'Limit results (0 for all)', '0')
.action(async (options) => {
try {
const api = createClient(program.opts());
const result = await api.getAdgroupsReportByDate(
options.campaignId,
options.startDate,
options.endDate,
'adGroupId',
'ASCENDING',
[],
true,
options.granularity ? false : true, // No row totals with granularity
options.granularity ? false : true, // No grand totals with granularity
options.granularity,
null,
0,
parseInt(options.limit)
);
output(result);
} catch (error) {
handleError(error);
}
});
// Geo commands
const geo = program.command('geo').description('Geo search');
geo
.command('search')
.description('Search for geographic locations')
.requiredOption('-w, --word <word>', 'Search word')
.option('-c, --country <code>', 'Country code', 'GB')
.option('-e, --entity <entity>', 'Entity type (Country, AdminArea, Locality)', 'Country')
.option('-l, --limit <number>', 'Limit results', '100')
.action(async (options) => {
try {
const api = createClient(program.opts());
const result = await api.geoSearch(
options.word,
options.entity,
options.country,
parseInt(options.limit),
0
);
output(result);
} catch (error) {
handleError(error);
}
});
program.parse();