-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
191 lines (159 loc) · 5.19 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
const _ = require('lodash')
const jQuery = require('jquery')
const { JSDOM } = require('jsdom')
module.exports = async (content, setting) => {
const { window } = new JSDOM((content || '').toString()) // Prevent if content is a buffer
const result = await analysis(window, setting)
window.close()
return result
}
function analysis(window, setting) {
const $ = jQuery(window)
if(pageNotFound($, setting.pageNotFound))
return Promise.reject('Page not found')
const {
container = 'html',
type = 'content',
listOption = [],
crawl: crawlData = {},
} = setting
const result = (type !== 'list')
? crawlContent($, $(container), crawlData)
: crawlList($, $(container), crawlData, listOption)
return Promise.resolve(result)
}
function pageNotFound($, pageNF) { // Not completed tested yet
let result = []
if(pageNF && pageNF.length) {
result = pageNF.map(json => {
if(!$(json.elem).length)
return ''
const check = (json.check && json.check.length) ? json.check[0] : ''
const value = grabValue($(json.elem), json)
switch(check) {
case 'equal': return _.isEqual(value, json.check[1])
default: return value
}
})
}
return (_.compact(result).join('') !== '') // If check is all pass, return value will be ''
}
function crawlList($, $listElems, crawlData, [ _option, ..._list ]) {
const elemLength = $listElems.length
const crawlInRange = ([ start, _end = elemLength ]) => {
const end = (_end <= elemLength) ? _end : elemLength
return _.times((end - start), i => crawlContent($, $listElems.eq(start + i), crawlData))
}
const crawlInFocus = focusList =>
focusList.map(i => (i < elemLength) ? crawlContent($, $listElems.eq(i), crawlData) : null)
const option = _list.length ? _option : ''
const list = _list.map(i => (i < 0) ? (i + elemLength) : i)
switch(option) {
case 'ignore': return crawlInFocus(_.difference(_.range(elemLength), list))
case 'focus': return crawlInFocus(list)
case 'range': return crawlInRange(list)
case 'limit': return crawlInRange([ 0, list[0] ])
default: return crawlInRange([ 0 ])
}
}
function crawlContent($, $content, crawlData) {
return _.mapValues(crawlData, options => {
let $elem = options.outOfContainer ? $('html') : $content
if(options.elem)
$elem = $elem.find(options.elem)
if(!$elem.length)
return ('default' in options) ? options.default : null
if(options.noChild)
$elem = $elem.children().remove().end()
const collectOptions = options.collect
if(!collectOptions)
return grabValue($elem, options)
const dataList = (({ elems = [], loop = false }) => {
switch(true) {
case (elems.length > 0):
return elems.map(tmp => {
const $tmpElem = tmp.elem ? $elem.find(tmp.elem) : $elem
return $tmpElem.length ? grabValue($tmpElem, tmp) : ''
})
case loop:
return $elem.map((i, e) => grabValue($(e), collectOptions)).get()
default:
return []
}
})(collectOptions)
return ('combineWith' in collectOptions && collectOptions.combineWith !== null)
? dataList.join(collectOptions.combineWith)
: dataList
})
}
function grabValue($elem, json) {
const result = format($elem, json.get)
switch(true) {
case ['', null, undefined].includes(result):
return ('default' in json) ? json.default : result
case 'default' in json && result === json.default:
case !json.process:
return result
case Array.isArray(json.process):
return process(result, json.process)
case (typeof json.process === 'function'):
return json.process(result, $elem)
default:
return result
}
}
function format($elem, returnType = 'text') {
switch(returnType.split('-')[0]) {
case 'num':
const num = parseFloat($elem.text().replace(/[^\d\.]/g, '')) // Prevent like $1,000,0.001
return !isNaN(num) ? num : 0
case 'text':
case 'html':
return $elem[returnType]().trim()
case 'length':
return $elem.length
case 'data':
const tmp = returnType.split(/-|:/)
const dataValue = $elem.data(tmp[1])
return tmp[2] ? dataValue[tmp[2]] : dataValue
default:
return $elem.attr(returnType)
}
}
_.mixin({
match(data, regex, address) {
const tmp = data.match(regex)
const pointer = (typeof address !== 'undefined') ? parseFloat(address) : NaN
return (tmp && !isNaN(pointer)) ? (tmp[pointer] || '') : tmp
},
split(data, keyword, address) {
const tmp = data.split(keyword)
const pointer = (typeof address !== 'undefined') ? parseFloat(address) : NaN
return !isNaN(pointer) ? (tmp[pointer] || '') : tmp
},
replace(data, from, to) {
return data.replace(from, to)
},
substring(data, start = 0, end) {
end = end || data.length
return data.substring(start, end)
},
prepend(data, arg) {
return arg + data
},
append(data, arg) {
return data + arg
},
encodeURI,
encodeURIComponent,
decodeURI,
decodeURIComponent,
})
function process(data, processList) {
for(let job of processList) {
if(typeof data !== 'string')
break
data = _[job[0]](data, job[1], job[2])
}
return data
}