-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
546 lines (504 loc) · 14.8 KB
/
Copy pathindex.js
File metadata and controls
546 lines (504 loc) · 14.8 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
//
// Fraidyscrape
//
// import fraidyscrape from './fraidyscrape'
// var scraper = new fraidyscrape(options)
// scraper.
//
const entDecode = require('ent/decode')
const entEncode = require('ent/encode')
const jp = require('jsonpath/jsonpath.min.js')
const normalizeUrl = require('normalize-url')
const urlp = require('url')
const unkZones = require('./unkZones.js')
module.exports = F
function F (options, parser, xpath, vars) {
if (!(this instanceof F)) return new F (options, parser, xpath, vars)
for (let id in options) {
let site = options[id]
if (site.match) {
site.match = new RegExp(site.match)
}
if (site.render) {
for (let r of site.render) {
if (r.match) {
r.match = new RegExp(r.match)
}
}
}
}
this.options = options
this.parser = parser
this.xpath = xpath
this.vars = vars || {}
this.watch = {}
}
F.prototype.parseHtml = function (str, mimeType) {
return this.parser(str, mimeType)
}
F.prototype.searchHtml = function (node, path, asText, vars) {
if (!(path instanceof Array)) {
path = [path]
}
for (let i = 0; i < path.length; i++) {
let p = path[i]
try {
let list = this.xpath(vars.doc, node, p, asText, vars.namespaces)
return asText ? list.join('').trim() : list
} catch (e) {
}
}
return asText ? "" : []
}
async function responseToObject (resp) {
let headers = {}
let body = await resp.text()
for (let h of resp.headers)
headers[h[0].toLowerCase()] = h[1]
return {status: resp.status, ok: resp.ok, url: resp.url, body, headers}
}
function urlToNormal (link) {
return normalizeUrl(link, {stripProtocol: true, removeDirectoryIndex: true, stripHash: true})
}
F.prototype.addWatch = function (url, entry) {
entry.externs = []
this.watch[url] = entry
}
F.prototype.removeWatch = function(url) {
let entry = this.watch[url]
if (entry) {
if (entry.resolve) {
entry.resolve(entry.tasks ? entry.tasks.vars : {})
}
delete this.watch[url]
entry.remove()
}
}
F.prototype.updateWatch = function(url, tasks, error) {
let entry = this.watch[url]
if (entry) {
entry.tasks = tasks
if (error) {
entry.reject(error)
delete entry.resolve
}
if (typeof(entry.externs) !== 'undefined') {
for (let ext of entry.externs) {
this.processExtern(url, entry, ext[0], ext[1], ext[2])
}
delete entry.externs
}
if (entry.render.length === 0 || error) {
this.removeWatch(url)
}
}
}
F.prototype.processExtern = async function (watchUrl, watch, render, match, fn) {
if (!render.validate ||
render.validate.map(v => watch.tasks.vars[v]).join("|") === match.slice(1).join("|"))
{
await fn(render, watch.tasks)
watch.render = watch.render.filter(wr => wr !== render)
this.updateWatch(watchUrl, watch.tasks)
}
}
F.prototype.lookupWatch = async function (url, fn) {
let norm = urlToNormal(url)
for (let wurl in this.watch) {
let w = this.watch[wurl]
for (let r of w.render) {
if (r.match) {
let m = norm.match(r.match)
if (m) {
if (typeof(w.externs) !== 'undefined') {
w.externs.push([r, m, fn])
} else {
this.processExtern(wurl, w, r, m, fn)
}
}
}
}
}
}
F.prototype.detect = function (url) {
for (let id in this.options) {
let site = this.options[id], match = null, norm = urlToNormal(url)
if (!site.match || !(match = norm.match(site.match)))
continue
let vars = Object.assign({url}, this.vars)
if (site.arguments) {
for (let i = 0; i < site.arguments.length; i++) {
let v = site.arguments[i]
if (typeof(v) === 'string') {
vars[v] = match[i]
} else if (typeof(v) === 'object') {
this.assign(vars, {[v.var]: match[i]}, vars, v.mod, true)
}
}
}
let queue = (site.depends || []).concat([id])
return {queue, vars}
}
return {queue: ["default"], vars: {url}}
}
function varr(vars, x) {
let k = x.split(':')
while (vars && k.length > 0) {
vars = vars[k.shift()]
}
return vars || ''
}
function varx(str, vars) {
return typeof(str) === 'string' ? str.replace(/\${(.+)}/g, x => {
let k = x.slice(2, -1)
let v = `${varx(k, vars)}`
return varr(vars, v)
}).replace(/\$([:\w]+)/g, x => varr(vars, x.slice(1))) : str
}
F.prototype.assign = function (options, additions, vars, mods, plainValue) {
for (let id in additions) {
let val = additions[id]
id = varx(id, vars)
let keys = id.split(':'), node = options
while (keys.length > 1) {
if (typeof(node[keys[0]]) !== 'object') {
node[keys[0]] = {}
}
node = node[keys[0]]
keys.shift()
}
if (!val) {
delete node[keys[0]]
continue
}
if (!plainValue)
val = varx(val, vars)
if (typeof(mods) === 'object') {
for (let i in mods) {
let trans = mods[i], match
if (trans === 'date') {
if (typeof(val) === 'string') {
if (val.match(/^\d{14,}/)) {
val = val.slice(0,4) + "-" + val.slice(4,6) + "-" + val.slice(6,8) +
" " + val.slice(8,10) + ":" + val.slice(10,12) + ":" + val.slice(12,14) + "Z"
} else if (val.match(/^\w+\s+\d{1,2}[a-z]*$/)) {
val = val + ", " + (new Date()).getFullYear()
} else if ((match = val.match(/^(.+) ([A-Z]{1,5})$/)) !== null) {
let z = unkZones[match[2]]
if (z) {
val = match[1] + " " + unkZones[match[2]]
}
}
}
val = new Date(val)
} else if (trans === 'ago') {
val = new Date(new Date() - val)
} else if (trans === 'int') {
val = Number(val)
} else if (trans === 'slug') {
val = '#' + encodeURIComponent(val)
} else if (trans === 'url') {
val = urlp.resolve(vars['url'], val)
} else if (trans === 'decode-uri') {
val = decodeURI(val)
} else if (trans === 'encode-uri') {
val = encodeURI(val)
} else if (trans === 'html-to-text') {
val = entDecode(val)
} else if (trans === 'text-to-html') {
val = entEncode(val)
} else if (trans.startsWith('valid-now')) {
let d = new Date(), field = trans.split(':')[1]
val = val.filter(x => x[field] < d)
} else if (trans.startsWith('*')) {
val = val * Number(trans.slice(1))
} else if (trans.startsWith('[')) {
let m = varx(trans, vars).slice(1, -1).split(',')
val = val.toString().slice(Number(m[0]), m[1] && (Number(m[1]) + 1))
} else if (trans.startsWith('s/')) {
let m = trans.slice(2).split(/(?<!\\)\//, 3)
val = val.toString().replace(new RegExp(m[0], m[2] || ''), (m[1] || '').replace(/\\\//g, "/"))
val = varx(val, vars)
} else if (trans === 'lowercase') {
val = val.toString().toLowerCase()
} else if (trans === 'uppercase') {
val = val.toString().toUpperCase()
} else if (trans === 'str') {
val = val.toString()
}
}
}
node[keys[0]] = val
}
return options
}
F.prototype.nextRequest = function (tasks) {
if (tasks.queue.length == 0)
return
let id = tasks.queue.shift()
let req = this.setupRequest(tasks, this.options[id])
req.id = id
return req
}
F.prototype.setupRequest = function (tasks, req) {
let options = this.assign({},
{url: req.url || tasks.vars.url, headers: {}, credentials: 'omit'},
tasks.vars)
if (this.options.domains) {
this.assign(options, this.options.domains[urlp.parse(options.url).hostname],
tasks.vars)
}
if (req.request) {
this.assign(options, req.request, tasks.vars)
}
let url = urlp.parse(options.url)
url.query = options.query
delete options.url
delete options.query
return {url: urlp.format(url), options,
render: req.render && req.render.concat()}
}
//
// Run a series of commands, populating 'vars'.
//
// vars: The hash to store output in.
// script: The list of commands.
// node: The parent node to scan.
// pathFn: The function to use for xpath or jsonpath or whatever.
//
//
F.prototype.scanScript = async function (vars, script, node, pathFn) {
for (let i = 0; i < script.length; i++) {
let cmd = script[i]
if (cmd.rule) {
let rule = vars.rules && vars.rules[cmd.rule]
if (rule) {
await this.scanScript(vars, rule, node, pathFn)
}
}
let ops = cmd.op, val = null
if (!(ops instanceof Array))
ops = [ops]
for (let j = 0; j < ops.length; j++) {
let op = varx(ops[j], vars)
if (op) {
let hasChildren = cmd.acceptJson || cmd.acceptText || cmd.acceptHtml || cmd.acceptXml || cmd.patch || cmd.use
let asText = !(hasChildren && !cmd.match)
if (op[0] === '=') {
val = op.slice(1)
} else if (op[0] === '&') {
val = jp[asText ? 'value' : 'query'](vars, '$' + op.slice(1))
} else {
val = pathFn(op, asText)
}
if (cmd.match) {
if (val.match && (match = val.match(new RegExp(cmd.match))) !== null) {
val = match[1] || val
} else {
continue
}
}
if (this.options && cmd.use && val.length > 0) {
let use = this.options[cmd.use]
return await this.scanSite(vars, use, node)
}
//
// If there is a nested ruleset, process it.
//
if (hasChildren) {
let v = vars
if (cmd.var) {
if (cmd.var !== "*") {
vars = Object.assign({}, vars)
delete vars.out
}
} else if (val instanceof Array) {
val = val.shift()
}
if (val) {
await this.scan(vars, cmd, val)
if (cmd.var) {
if (cmd.var !== "*") {
val = vars.out
vars = v
}
}
}
}
}
//
// If object contains anything at all, no need to run
// further ops in a chain.
//
if (val instanceof Array) {
if (val.length > 0)
break
} else if (val && Object.entries(val).length > 0) {
break
}
}
//
// See 'assign' method above.
//
if (cmd.var && cmd.var !== "*") {
this.assign(vars, {[cmd.var]: val}, vars, cmd.mod, true)
}
}
}
var reISO = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.{0,1}\d*))(?:Z|(\+|-)([\d|:]*))?$/;
function jsonDateParser(_, value) {
var parsedValue = value;
if (typeof value === 'string') {
var a = reISO.exec(value);
if (a) {
parsedValue = new Date(value);
}
}
return parsedValue;
}
//
// Scan a document (HTML or JSON) following a site ruleset.
//
// vars: A hash to use for storage.
// site: The ruleset to use.
// obj: The document to scan.
//
F.prototype.scan = async function (vars, site, obj) {
let script = null
let fn = (path, asText) => jp[asText ? 'value' : 'query'](obj, path)
if (site.accept) {
for (let i = 0; i < site.accept.length; i++) {
await this.scanSite(vars, this.options[site.accept[i]], obj)
if (vars.out)
break
}
}
if (site.acceptJson) {
if (obj.innerHTML) {
obj = obj.innerHTML
}
if (typeof(obj) === 'string') {
vars.mime = 'application/json'
obj = JSON.parse(obj, jsonDateParser)
} else if (!vars.mime) {
vars.mime = 'application/json'
} else if (vars.mime !== 'application/json') {
return vars
}
script = site.acceptJson
} else if (site.acceptText) {
if (obj.innerText) {
obj = obj.innerText
}
if (typeof(obj) === 'string' || !vars.mime) {
vars.mime = 'text/plain'
} else if (vars.mime !== 'text/plain') {
return vars
}
obj = obj.toString()
script = site.acceptText
fn = function (path, asText) {
let match = null
if (asText) {
if ((match = obj.match(new RegExp(path, 'm'))) !== null) {
return match[1] || obj
}
} else {
let re = new RegExp(path, 'mg'), ary = []
while ((match = re.exec(obj)) !== null) {
ary.push(Object.assign({}, match.groups))
}
return ary
}
}
} else if (site.acceptHtml || site.acceptXml) {
if (typeof(obj) === 'string') {
vars.mime = site.acceptHtml ? 'text/html' : 'text/xml'
} else if (!vars.mime) {
vars.mime = site.acceptHtml ? 'text/html' : 'text/xml'
} else if (vars.mime === 'application/json') {
return vars
}
script = site.acceptHtml || site.acceptXml
fn = (path, asText) => this.searchHtml(obj, path, asText, vars)
} else if (site.patch) {
script = site.patch
if (!site.op)
obj = vars.out
}
if (script) {
if (obj instanceof Array) {
let out = []
if (site.var !== "*")
delete vars.out
for (let i = 0; i < obj.length; i++) {
let v = (site.var === "*" ? vars : Object.assign({}, vars))
v.index = i.toString()
await this.scan(v, site, obj[i])
if (site.var !== "*")
out.push(v.out)
}
if (site.var !== "*")
vars.out = out
} else if (fn) {
setTimeout(() => {1}, 0)
await this.scanScript(vars, script, obj, fn)
}
}
return vars
}
F.prototype.scanSite = async function (vars, site, obj) {
let oldNs = vars.namespaces, oldRules = vars.rules
vars.namespaces = site.namespaces
vars.rules = site.rules
let v = await this.scan(vars, site, obj)
vars.namespaces = oldNs
vars.rules = oldRules
return v
}
F.prototype.scrapeRule = async function (tasks, res, site) {
res = await responseToObject(res)
let mime = res.headers['content-type']
let body = res.body.trim()
if (/^\s*[{\[]/.test(body)) {
tasks.vars.doc = JSON.parse(body, jsonDateParser)
if (tasks.vars.doc instanceof Array) {
tasks.vars.doc = {list: tasks.vars.doc}
}
mime = 'application/json'
} else if (/^\s*</m.test(body)) {
// The [\s\S] matches ANY char - while the dot (,) doesn't match newlines
if (/^\s*<\?xml\s+[\s\S]+<(rss|atom)/i.test(body)) {
mime = 'text/xml'
}
tasks.vars.doc = this.parseHtml(body, /html/.test(mime) ? 'text/html' : 'text/xml')
} else {
mime = 'text/plain'
tasks.vars.doc = body
}
tasks.vars.mime = mime
// console.log([tasks, res, site])
let vars = {}
try {
vars = await this.scanSite(tasks.vars, site, tasks.vars.doc)
} finally {
delete tasks.vars.doc
}
return vars
}
F.prototype.scrape = async function (tasks, req, res) {
let site = this.options[req.id]
let vars = await this.scrapeRule(tasks, res, site)
vars.rule = req.id
return vars
}
F.prototype.scrapeRender = async function (tasks, site, win) {
tasks.vars.doc = site.acceptJson ? win : win.document
let vars = {}
try {
vars = await this.scanSite(tasks.vars, site, tasks.vars.doc)
} finally {
delete tasks.vars.doc
}
return vars
}