-
Notifications
You must be signed in to change notification settings - Fork 109
/
DomSelectFunctions.kt
414 lines (347 loc) · 16.8 KB
/
DomSelectFunctions.kt
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
package ai.platon.pulsar.ql.h2.udfs
import ai.platon.pulsar.common.RegexExtractor
import ai.platon.pulsar.common.Strings
import ai.platon.pulsar.dom.nodes.A_LABELS
import ai.platon.pulsar.dom.nodes.node.ext.minimalHtml
import ai.platon.pulsar.dom.nodes.node.ext.namedRect
import ai.platon.pulsar.dom.nodes.node.ext.slimHtml
import ai.platon.pulsar.dom.select.appendSelectorIfMissing
import ai.platon.pulsar.dom.select.selectFirstOrNull
import ai.platon.pulsar.dom.select.select2
import ai.platon.pulsar.ql.annotation.UDFGroup
import ai.platon.pulsar.ql.annotation.UDFunction
import ai.platon.pulsar.ql.h2.Queries
import ai.platon.pulsar.ql.types.ValueDom
import org.h2.value.ValueArray
import org.h2.value.ValueFloat
import org.h2.value.ValueInt
import org.h2.value.ValueString
import org.jsoup.nodes.Element
/**
* Created by vincent on 17-11-1.
* Copyright @ 2013-2023 Platon AI. All rights reserved.
*/
@Suppress("unused")
@UDFGroup(namespace = "DOM")
object DomSelectFunctions {
@UDFunction(description = "Select the nth element from a DOM by the given css query and return a DOM")
@JvmStatic
fun selectAll(dom: ValueDom, cssQuery: String): ValueArray {
return Queries.select(dom, cssQuery) { it }
}
@UDFunction(description = "Select the first element from a DOM by the given css query and return a DOM")
@JvmStatic
fun selectFirst(dom: ValueDom, cssQuery: String): ValueDom {
return dom.takeIf { it.isNil }?:ValueDom.getOrNil(dom.element.selectFirstOrNull(cssQuery))
}
@UDFunction(description = "Select the nth element from a DOM by the given css query and return a DOM")
@JvmStatic
fun selectNth(dom: ValueDom, cssQuery: String, n: Int): ValueDom {
return dom.takeIf { it.isNil }?:ValueDom.getOrNil(nthElement(dom.element, cssQuery, n))
}
@UDFunction(description = "Select all elements from a DOM by the given css query and return the the element texts")
@JvmStatic
fun allTexts(dom: ValueDom, cssQuery: String): ValueArray {
return Queries.select(dom, cssQuery) { it.text() }
}
@UDFunction(description = "Select the first element from a DOM by the given css query and return the element text")
@JvmStatic
fun firstText(dom: ValueDom, cssQuery: String): String {
return Queries.selectFirstOrNull(dom, cssQuery) { it.text() } ?: ""
}
@UDFunction(description = "Select the nth element from a DOM by the given css query and return the element text")
@JvmStatic
fun nthText(dom: ValueDom, cssQuery: String, n: Int): String {
return Queries.selectNthOrNull(dom, cssQuery, n) { it.text() } ?: ""
}
@UDFunction(description = "Select all elements from a DOM by the given css query and return the the element's own texts")
@JvmStatic
fun allOwnTexts(dom: ValueDom, cssQuery: String): ValueArray {
return Queries.select(dom, cssQuery) { it.ownText() }
}
@UDFunction(description = "Select the first element from a DOM by the given css query and return the element's own text")
@JvmStatic
fun firstOwnText(dom: ValueDom, cssQuery: String): String {
return Queries.selectFirstOrNull(dom, cssQuery) { it.ownText() } ?: ""
}
@UDFunction(description = "Select the nth element from a DOM by the given css query and return the element's own text")
@JvmStatic
fun nthOwnText(dom: ValueDom, cssQuery: String, n: Int): String {
return Queries.selectNthOrNull(dom, cssQuery, n) { it.ownText() } ?: ""
}
@UDFunction(description = "Select all elements from a DOM by the given css query and return the the element whole texts")
@JvmStatic
fun wholeTexts(dom: ValueDom, cssQuery: String): ValueArray {
return Queries.select(dom, cssQuery) { it.wholeText() }
}
@UDFunction(description = "Select the first element from a DOM by the given css query and return the element whole text")
@JvmStatic
fun firstWholeText(dom: ValueDom, cssQuery: String): String {
return Queries.selectFirstOrNull(dom, cssQuery) { it.wholeText() } ?: ""
}
@UDFunction(description = "Select the nth element from a DOM by the given css query and return the element whole text")
@JvmStatic
fun nthWholeText(dom: ValueDom, cssQuery: String, n: Int): String {
return Queries.selectNthOrNull(dom, cssQuery, n) { it.wholeText() } ?: ""
}
@UDFunction(description = "Select all elements from a DOM by the given css query and return the the element texts")
@JvmStatic
fun allSlimHtmls(dom: ValueDom, cssQuery: String): ValueArray {
return Queries.select(dom, cssQuery) { it.slimHtml }
}
@UDFunction(description = "Select the first element from a DOM by the given css query and return the element text")
@JvmStatic
fun firstSlimHtml(dom: ValueDom, cssQuery: String): String {
return Queries.selectFirstOrNull(dom, cssQuery) { it.slimHtml } ?: ""
}
@UDFunction(description = "Select the nth element from a DOM by the given css query and return the element text")
@JvmStatic
fun nthSlimHtml(dom: ValueDom, cssQuery: String, n: Int): String {
return Queries.selectNthOrNull(dom, cssQuery, n) { it.slimHtml } ?: ""
}
@UDFunction(description = "Select all elements from a DOM by the given css query and return the the element texts")
@JvmStatic
fun allMinimalHtmls(dom: ValueDom, cssQuery: String): ValueArray {
return Queries.select(dom, cssQuery) { it.minimalHtml }
}
@UDFunction(description = "Select the first element from a DOM by the given css query and return the element text")
@JvmStatic
fun firstMinimalHtml(dom: ValueDom, cssQuery: String): String {
return Queries.selectFirstOrNull(dom, cssQuery) { it.minimalHtml } ?: ""
}
@UDFunction(description = "Select the nth element from a DOM by the given css query and return the element text")
@JvmStatic
fun nthMinimalHtml(dom: ValueDom, cssQuery: String, n: Int): String {
return Queries.selectNthOrNull(dom, cssQuery, n) { it.minimalHtml } ?: ""
}
@UDFunction(description = "Select all the element from a DOM by the given css query " +
"and try to extract an integer from the element text, if the text is not an integer, fill the default value")
@JvmStatic
@JvmOverloads
fun allIntegers(dom: ValueDom, cssQuery: String, defaultValue: Int = 0): ValueArray {
return Queries.select(dom, cssQuery) { ValueInt.get(Strings.getFirstInteger(it.text(), defaultValue)) }
}
@UDFunction(description = "Select the first element from a DOM by the given css query " +
"and try to extract an integer from the element text")
@JvmStatic
fun firstInteger(dom: ValueDom, cssQuery: String, defaultValue: Int = 0): Int {
val s = firstText(dom, cssQuery)
return Strings.getFirstInteger(s, defaultValue)
}
@UDFunction(description = "Select the nth element from a DOM by the given css query " +
"and try to extract an integer from the element text")
@JvmStatic
@JvmOverloads
fun nthInteger(dom: ValueDom, cssQuery: String, n: Int, defaultValue: Int = 0): Int {
val s = nthText(dom, cssQuery, n)
return Strings.getFirstInteger(s, defaultValue)
}
@UDFunction(description = "Select all the element from a DOM by the given css query " +
"and try to extract an number from the element text")
@JvmStatic
@JvmOverloads
fun allFloats(dom: ValueDom, cssQuery: String, defaultValue: Float = 0.0f): ValueArray {
return Queries.select(dom, cssQuery) { ValueFloat.get(Strings.getFirstFloatNumber(it.text(), defaultValue)) }
}
/**
* convert to ValueFloat manually,
* TODO: kotlin.Float can not convert to ValueFloat automatically
* */
@UDFunction(description = "Select the first element from a DOM by the given css query " +
"and try to extract an number from the element text")
@JvmStatic
fun firstFloat(dom: ValueDom, cssQuery: String, defaultValue: Float = 0.0f): ValueFloat {
val s = firstText(dom, cssQuery)
return Strings.getFirstFloatNumber(s, defaultValue).let { ValueFloat.get(it) }
}
@UDFunction(description = "Select the nth element from a DOM by the given css query " +
"and try to extract an number from the element text")
@JvmStatic
@JvmOverloads
fun nthFloat(dom: ValueDom, cssQuery: String, n: Int, defaultValue: Float = 0.0f): ValueFloat {
val s = nthText(dom, cssQuery, n)
return Strings.getFirstFloatNumber(s, defaultValue).let { ValueFloat.get(it) }
}
@UDFunction(description = "Select all the element from a DOM by the given css query " +
"and return the attribute value associated by the attribute name")
@JvmStatic
@JvmOverloads
fun allAttrs(dom: ValueDom, cssQuery: String = ":root", attrName: String): ValueArray {
return Queries.select(dom, cssQuery) { it.attr(attrName) }
}
@UDFunction(description = "Select the first element from a DOM by the given css query " +
"and return the attribute value associated by the attribute name")
@JvmStatic
@JvmOverloads
fun firstAttr(dom: ValueDom, cssQuery: String = ":root", attrName: String): String {
return Queries.selectFirstOrNull(dom, cssQuery) { it.attr(attrName) } ?: ""
}
@UDFunction(description = "Select the nth element from a DOM by the given css query " +
"and return the attribute value associated by the attribute name")
@JvmStatic
fun nthAttr(dom: ValueDom, cssQuery: String, n: Int, attrName: String): String {
return Queries.selectNthOrNull(dom, cssQuery, n) { it.attr(attrName) } ?: ""
}
@UDFunction(description = "Select all the element from a DOM by the given css query " +
"and return the attribute value associated by the attribute name")
@JvmStatic
@JvmOverloads
fun allMultiAttrs(dom: ValueDom, cssQuery: String = ":root", attrNames: Array<String>): ValueArray {
return Queries.select(dom, cssQuery) { ele -> attrNames.map { ele.attr(it) } }
}
@UDFunction(description = "Select the first element from a DOM by the given css query " +
"and return the attribute value associated by the attribute name")
@JvmStatic
@JvmOverloads
fun firstMultiAttrs(dom: ValueDom, cssQuery: String = ":root", attrNames: Array<String>): List<String> {
val result = Queries.selectFirstOrNull(dom, cssQuery) { ele ->
attrNames.map { ele.attr(it) }
} ?: listOf()
return result
}
@UDFunction(description = "Select the nth element from a DOM by the given css query " +
"and return the attribute value associated by the attribute name")
@JvmStatic
fun nthMultiAttrs(dom: ValueDom, cssQuery: String, n: Int, attrNames: Array<String>): List<String> {
val result = Queries.selectNthOrNull(dom, cssQuery, n) { ele ->
attrNames.map { ele.attr(it) }
} ?: listOf()
return result
}
@UDFunction(description = "Select all the image elements from a DOM by the given css query " +
"and return the src of the image element")
@JvmStatic
@JvmOverloads
fun allImgs(dom: ValueDom, cssQuery: String = ":root"): ValueArray {
val q = appendSelectorIfMissing(cssQuery, "img")
return Queries.select(dom, q) { it.attr("abs:src") }
}
@UDFunction(description = "Select the first image element from a DOM by the given css query " +
"and return the src of it")
@JvmStatic
@JvmOverloads
fun firstImg(dom: ValueDom, cssQuery: String = ":root"): String {
val q = appendSelectorIfMissing(cssQuery, "img")
return Queries.selectFirstOrNull(dom, q) { it.attr("abs:src") } ?: ""
}
@UDFunction(description = "Select the nth image element from a DOM by the given css query " +
"and return the src of it")
@JvmStatic
fun nthImg(dom: ValueDom, cssQuery: String, n: Int): String {
val q = appendSelectorIfMissing(cssQuery, "img")
return Queries.selectNthOrNull(dom, q, n) { it.attr("abs:src") } ?: ""
}
@UDFunction
@JvmStatic
@JvmOverloads
fun allHrefs(dom: ValueDom, cssQuery: String = ":root"): ValueArray {
val q = appendSelectorIfMissing(cssQuery, "a")
return Queries.select(dom, q) { it.attr("abs:href") }
}
@UDFunction(description = "Select the first anchor element from a DOM by the given css query " +
"and return the href of it")
@JvmStatic
@JvmOverloads
fun firstHref(dom: ValueDom, cssQuery: String = ":root"): String {
val q = appendSelectorIfMissing(cssQuery, "a")
return Queries.selectFirstOrNull(dom, q) { it.attr("abs:href") } ?: ""
}
@UDFunction(description = "Select the nth anchor element from a DOM by the given css query " +
"and return the href of it")
@JvmStatic
fun nthHref(dom: ValueDom, cssQuery: String, n: Int): String {
val q = appendSelectorIfMissing(cssQuery, "a")
return Queries.selectNthOrNull(dom, q, n) { it.attr("abs:href") } ?: ""
}
@UDFunction(description = "Select the first element from a DOM by the given css query " +
"and return the label of it")
@JvmStatic
@JvmOverloads
fun allNodesLabels(dom: ValueDom, cssQuery: String = ":root"): ValueArray {
return allAttrs(dom, cssQuery, A_LABELS)
}
@UDFunction
@JvmStatic
@JvmOverloads
fun firstNodeLabels(dom: ValueDom, cssQuery: String = ":root"): String {
return firstAttr(dom, cssQuery, A_LABELS)
}
@UDFunction(description = "Select the nth element from a DOM by the given css query " +
"and return the label of it")
@JvmStatic
fun nthNodeLabels(dom: ValueDom, cssQuery: String, n: Int): String {
return nthAttr(dom, cssQuery, n, A_LABELS)
}
@UDFunction
@JvmStatic
fun allRe1(dom: ValueDom, regex: String): ValueArray {
return allRe1(dom, ":root", regex)
}
@UDFunction
@JvmStatic
fun allRe1(dom: ValueDom, cssQuery: String, regex: String): ValueArray {
val extractor = RegexExtractor()
return Queries.select(dom, cssQuery) { extractor.re1(it.text(), regex) }
}
@UDFunction(description = "Select the first element from a DOM whose text matches the regex " +
"and return the element text")
@JvmStatic
fun firstRe1(dom: ValueDom, regex: String): String {
return firstRe1(dom, ":root", regex)
}
@UDFunction(description = "Select the first element from a DOM whose text matches the regex " +
"and return the element text")
@JvmStatic
fun firstRe1(dom: ValueDom, cssQuery: String, regex: String): String {
val text = text(selectFirst(dom, cssQuery))
return RegexExtractor().re1(text, regex)
}
@UDFunction(description = "Select the first element from a DOM whose text matches the regex " +
"and return the element text")
@JvmStatic
fun firstRe1(dom: ValueDom, cssQuery: String, regex: String, group: Int): String {
val text = text(selectFirst(dom, cssQuery))
return RegexExtractor().re1(text, regex, group)
}
@UDFunction
@JvmStatic
fun allRe2(dom: ValueDom, regex: String): ValueArray {
return allRe2(dom, ":root", regex)
}
@UDFunction
@JvmStatic
fun allRe2(dom: ValueDom, cssQuery: String, regex: String): ValueArray {
val extractor = RegexExtractor()
return Queries.select(dom, cssQuery) { extractor.re2(it.text(), regex).toString() }
}
@UDFunction
@JvmStatic
fun firstRe2(dom: ValueDom, cssQuery: String, regex: String): ValueArray {
val text = text(selectFirst(dom, cssQuery))
val result = RegexExtractor().re2(text, regex)
val array = arrayOf(ValueString.get(result.key), ValueString.get(result.value))
return ValueArray.get(array)
}
@UDFunction
@JvmStatic
fun firstRe2(dom: ValueDom, cssQuery: String, regex: String, keyGroup: Int, valueGroup: Int): ValueArray {
val text = text(selectFirst(dom, cssQuery))
val result = RegexExtractor().re2(text, regex, keyGroup, valueGroup)
val array = arrayOf(ValueString.get(result.key), ValueString.get(result.value))
return ValueArray.get(array)
}
@UDFunction
@JvmStatic
fun allRe2(dom: ValueDom, cssQuery: String, regex: String, keyGroup: Int, valueGroup: Int): ValueArray {
val extractor = RegexExtractor()
return Queries.select(dom, cssQuery) { extractor.re2(it.text(), regex, keyGroup, valueGroup).toString() }
}
private fun text(dom: ValueDom): String {
return dom.element.text()
}
private fun nthElement(root: Element, cssQuery: String, n: Int): Element? {
if (n < 1) throw IndexOutOfBoundsException("n should be in [1, )")
val elements = root.select2(cssQuery)
return if (elements.size >= n) { elements[n - 1] } else null
}
}