forked from matthewmueller/x-ray
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
264 lines (227 loc) · 6.9 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
'use strict'
var objectAssign = require('./lib/util').objectAssign
var streamToPromise = require('./lib/promisify')
var compact = require('./lib/util').compact
var isArray = require('./lib/util').isArray
var absolutes = require('./lib/absolutes')
var streamHelper = require('./lib/stream')
var isUrl = require('./lib/util').isUrl
var Crawler = require('x-ray-crawler')
var resolve = require('./lib/resolve')
var root = require('./lib/util').root
var params = require('./lib/params')
var debug = require('debug')('x-ray')
var cheerio = require('cheerio')
var enstore = require('enstore')
var walk = require('./lib/walk')
var fs = require('fs')
var CONST = {
CRAWLER_METHODS: ['concurrency', 'throttle', 'timeout', 'driver', 'delay', 'limit', 'abort'],
INIT_STATE: {
stream: false,
concurrency: Infinity,
paginate: false,
limit: Infinity,
abort: false
}
}
function Xray (options) {
var crawler = Crawler()
options = options || {}
var filters = options.filters || {}
function xray (source, scope, selector) {
var args = params(source, scope, selector)
selector = args.selector
source = args.source
scope = args.context
var state = objectAssign({}, CONST.INIT_STATE)
var store = enstore()
var pages = []
var stream
var walkHTML = WalkHTML(xray, selector, scope, filters)
var request = Request(crawler)
function node (source2, fn) {
if (arguments.length === 1) {
fn = source2
} else {
source = source2
}
debug('params: %j', {
source: source,
scope: scope,
selector: selector
})
if (isUrl(source)) {
debug('starting at: %s', source)
request(source, function (err, html) {
if (err) return next(err)
var $ = load(html, source)
walkHTML($, next)
})
} else if (scope && ~scope.indexOf('@')) {
debug('resolving to a url: %s', scope)
var url = resolve(source, false, scope, filters)
// ensure that a@href is a URL
if (!isUrl(url)) {
debug('%s is not a url. Skipping!', url)
return walkHTML(load(''), next)
}
debug('resolved "%s" to a %s', scope, url)
request(url, function (err, html) {
if (err) return next(err)
var $ = load(html, url)
walkHTML($, next)
})
} else if (source) {
var $ = load(source)
walkHTML($, next)
} else {
debug('%s is not a url or html. Skipping!', source)
return walkHTML(load(''), next)
}
function next (err, obj, $) {
if (err) return fn(err)
var paginate = state.paginate
var limit = --state.limit
// create the stream
if (!stream) {
if (paginate) stream = streamHelper.array(state.stream)
else stream = streamHelper.object(state.stream)
}
if (paginate) {
if (isArray(obj)) {
pages = pages.concat(obj)
} else {
pages.push(obj)
}
if (limit <= 0) {
debug('reached limit, ending')
stream(obj, true)
return fn(null, pages)
}
var url = resolve($, false, paginate, filters)
debug('paginate(%j) => %j', paginate, url)
if (!isUrl(url)) {
debug('%j is not a url, finishing up', url)
stream(obj, true)
return fn(null, pages)
}
if (state.abort && state.abort(obj, url)) {
debug('abort check passed, ending')
stream(obj, true)
return fn(null, pages)
}
stream(obj)
// debug
debug('paginating %j', url)
isFinite(limit) && debug('%s page(s) left to crawl', limit)
request(url, function (err, html) {
if (err) return next(err)
var $ = load(html, url)
walkHTML($, next)
})
} else {
stream(obj, true)
fn(null, obj)
}
}
return node
}
node.abort = function (validator) {
if (!arguments.length) return state.abort
state.abort = validator
return node
}
node.paginate = function (paginate) {
if (!arguments.length) return state.paginate
state.paginate = paginate
return node
}
node.limit = function (limit) {
if (!arguments.length) return state.limit
state.limit = limit
return node
}
node.stream = function () {
state.stream = store.createWriteStream()
var rs = store.createReadStream()
streamHelper.waitCb(rs, node)
return rs
}
node.write = function (path) {
if (!arguments.length) return node.stream()
state.stream = fs.createWriteStream(path)
streamHelper.waitCb(state.stream, node)
return state.stream
}
node.then = function (cb) {
return streamToPromise(node.stream()).then(cb)
}
return node
}
CONST.CRAWLER_METHODS.forEach(function (method) {
xray[method] = function () {
if (!arguments.length) return crawler[method]()
crawler[method].apply(crawler, arguments)
return this
}
})
return xray
}
function Request (crawler) {
return function request (url, fn) {
debug('fetching %s', url)
crawler(url, function (err, ctx) {
if (err) return fn(err)
debug('got response for %s with status code: %s', url, ctx.status)
return fn(null, ctx.body)
})
}
}
function load (html, url) {
html = html || ''
var $ = html.html ? html : cheerio.load(html)
if (url) $ = absolutes(url, $)
return $
}
function WalkHTML (xray, selector, scope, filters) {
return function walkHTML ($, fn) {
walk(selector, function (v, k, next) {
if (typeof v === 'string') {
var value = resolve($, root(scope), v, filters)
return next(null, value)
} else if (typeof v === 'function') {
return v($, function (err, obj) {
if (err) return next(err)
return next(null, obj)
})
} else if (isArray(v)) {
if (typeof v[0] === 'string') {
return next(null, resolve($, root(scope), v, filters))
} else if (typeof v[0] === 'object') {
var $scope = $.find ? $.find(scope) : $(scope)
var pending = $scope.length
var out = []
// Handle the empty result set (thanks @jenbennings!)
if (!pending) return next(null, out)
return $scope.each(function (i, el) {
var $innerscope = $scope.eq(i)
var node = xray(scope, v[0])
node($innerscope, function (err, obj) {
if (err) return next(err)
out[i] = obj
if (!--pending) {
return next(null, compact(out))
}
})
})
}
}
return next()
}, function (err, obj) {
if (err) return fn(err)
fn(null, obj, $)
})
}
}
module.exports = Xray