-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparsehub.js
315 lines (278 loc) · 7.1 KB
/
parsehub.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
'use strict';
var request = require('request');
var base = 'https://www.parsehub.com';
/**
* Constructor - takes your ParseHub API key
*/
function ParseHub(apiKey)
{
if (!apiKey)
throw Error('Please specify a ParseHub API key');
this._apiKey = apiKey;
}
/**
* Return a list of all jobs belonging to you
*
* Parameters:
*
* include_last_run (Optional) - If set to anything other than '0', each job object in the result includes a last_run property containing the run object for the last run that was initiated for this job. If no runs have been initiated for the job, the last_run property is not included. Defaults to '0'.
*
* On success, returns an object with:
*
* scrapejobs - A list of all your job objects. Each job object may have an additional last_run property, depending on the include_last_run parameter.
*/
ParseHub.prototype.getAllJobs = function(params, callback)
{
var callback = callback || params || function(){};
params = (typeof params === 'object') ? params : {};
params.api_key = this._apiKey;
request({
url: base + '/api/scrapejob',
qs: params
},
function(err, resp, body)
{
if (resp.statusCode !== 200)
return callback(new Error(body));
try
{
var json = JSON.parse(body);
callback(err, json.scrapejobs || []);
}
catch (e)
{
callback(new Error('Could not parse response body'));
}
});
};
/**
* Delete an existing job
*
* Parameters:
*
* token - The token of the job you'd like to delete
*
* On success, returns an object with:
*
* token - The token of the job that was deleted
*/
ParseHub.prototype.deleteJob = function(params, callback)
{
var callback = callback || function(){};
if (!params || !params.token)
throw new Error('Please specify a job token');
params.api_key = this._apiKey;
request.post({
url: base + '/api/scrapejob/delete',
formData: params
},
function(err, resp, body)
{
if (resp.statusCode !== 200)
return callback(new Error(body));
try
{
var json = JSON.parse(body);
callback(err, json.token);
}
catch (e)
{
callback(new Error('Could not parse response body'));
}
});
}
/**
* Run an instance of a job that was previously created
*
* Parameters:
*
* token - The token of the job you'd like to run
* start_url (Optional) - Run the job starting at this url rather than on the default starting url for the job
* start_value_override (Optional) - Override the starting JSON value of the global scope. This can be used to pass parameters to your run. For example, you may pass {'query': 'San Francisco'} in order to use the query in an expression somewhere in your job.
*
* On success, returns an object with:
*
* run_token - The unique identifier of the run that was created
*/
ParseHub.prototype.runJob = function(params, callback)
{
var callback = callback || function(){};
if (!params || !params.token)
throw new Error('Please specify a job token');
params.api_key = this._apiKey;
request.post({
url: base + '/api/scrapejob/run',
formData: params
},
function(err, resp, body)
{
if (resp.statusCode !== 200)
return callback(new Error(body));
try
{
var json = JSON.parse(body);
callback(err, json.run_token);
}
catch (e)
{
callback(new Error('Could not parse response body'));
}
});
};
/**
* Return the status of a single run of one of your jobs
*
* Parameters:
*
* run_token - The unique identifier of the run that you'd like to get the status of
*
* On success, returns an object with:
*
* run - The run object corresponding to the run_token provided
*/
ParseHub.prototype.getRunStatus = function(params, callback)
{
var callback = callback || function(){};
if (!params || !params.run_token)
throw new Error('Please specify a run token');
params.api_key = this._apiKey;
request({
url: base + '/api/scrapejob/run_status',
qs: params
},
function(err, resp, body)
{
if (resp.statusCode !== 200)
return callback(new Error(body));
try
{
var json = JSON.parse(body);
callback(err, json || {});
}
catch (e)
{
callback(new Error('Could not parse response body'));
}
});
};
/**
* Return a list of all runs of one of your jobs
*
* Parameters:
*
* token - The unique identifier of the job that you'd like to get the status of
*
* On success, returns an object with:
*
* runList - A list of all run objects corresponding to this job.
*/
ParseHub.prototype.getStatus = function(params, callback)
{
var callback = callback || function(){};
if (!params || !params.token)
throw new Error('Please specify a job token');
params.api_key = this._apiKey;
request({
url: base + '/api/scrapejob/status',
qs: params
},
function(err, resp, body)
{
if (resp.statusCode !== 200)
return callback(new Error(body));
try
{
var json = JSON.parse(body);
callback(err, json.runList);
}
catch (e)
{
callback(new Error('Could not parse response body'));
}
});
};
/**
* Cancel a single run
*
* Parameters:
*
* run_token The unique identifier of the run you'd like to cancel
*
* On success, returns an object with:
*
* run_token The unique identifier of the run that was cancelled
*/
ParseHub.prototype.cancelRun = function(params, callback)
{
var callback = callback || function(){};
if (!params || !params.run_token)
throw new Error('Please specify a run token');
params.api_key = this._apiKey;
request.post({
url: base + '/api/scrapejob/cancel',
formData: params
},
function(err, resp, body)
{
if (resp.statusCode !== 200)
return callback(new Error(body));
try
{
var json = JSON.parse(body);
callback(err, json.run_token);
}
catch (e)
{
callback(new Error('Could not parse response body'));
}
});
};
/**
* Get the result of a single run
*
* Parameters:
*
* run_token - The unique identifier of the run for which you'd like to download the data
* format (Optional) - If set to 'csv', results will be in a CSV format. Otherwise, they will be in a JSON format. Note that results will be zipped in either case unless the 'raw' parameter is set.
* raw (Optional) - If set to anything other than '0', returns a raw json response instead of a zip file containing a json file. Defaults to '0', and cannot be changed for responses which have an uncompressed size > 3 MB.
*
* On success, if raw is '0':
*
* Returns a zip file with a single json file inside it. The json file has the same name as the run_token.
*
* On success, if raw is not '0':
*
* Returns a json response which is the global scope of the run.
*/
ParseHub.prototype.getResults = function(params, callback)
{
var callback = callback || function(){};
if (!params || !params.run_token)
throw new Error('Please specify a run token');
params.api_key = this._apiKey;
params.raw = 1; // @todo - zip files are not supported
request({
url: base + '/api/scrapejob/dl',
qs: params
},
function(err, resp, body)
{
if (resp.statusCode !== 200)
return callback(new Error(body));
try
{
if (params.raw && params.raw !== 0)
{
var json = JSON.parse(body);
callback(err, json);
}
else
callback(err, body);
}
catch (e)
{
callback(new Error('Could not parse response body'));
}
});
};
module.exports = ParseHub;