@crawlee/playwright 4.0.0-beta.118 → 4.0.0-beta.119
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2,6 +2,7 @@ import { RecoverableState } from '@crawlee/core';
|
|
|
2
2
|
import LogisticRegression from 'ml-logistic-regression';
|
|
3
3
|
import { Matrix } from 'ml-matrix';
|
|
4
4
|
import stringComparison from 'string-comparison';
|
|
5
|
+
import { z } from 'zod';
|
|
5
6
|
const urlComponents = (url) => {
|
|
6
7
|
return [url.hostname, ...url.pathname.split('/')];
|
|
7
8
|
};
|
|
@@ -24,6 +25,41 @@ const calculateUrlSimilarity = (a, b) => {
|
|
|
24
25
|
};
|
|
25
26
|
const sum = (values) => values.reduce((acc, value) => acc + value);
|
|
26
27
|
const mean = (values) => (values.length > 0 ? sum(values) / values.length : undefined);
|
|
28
|
+
const renderingType = z.enum(['clientOnly', 'static']);
|
|
29
|
+
const predictorState = z.object({
|
|
30
|
+
logreg: z.instanceof(LogisticRegression),
|
|
31
|
+
detectionResults: z.map(renderingType, z.map(z.string().optional(), z.array(z.array(z.string())))),
|
|
32
|
+
});
|
|
33
|
+
const persistedState = z.object({
|
|
34
|
+
logreg: z
|
|
35
|
+
.record(z.string(), z.unknown())
|
|
36
|
+
.prefault(() => new LogisticRegression({ numSteps: 1000, learningRate: 0.05 }).toJSON()),
|
|
37
|
+
detectionResults: z
|
|
38
|
+
.array(z.object({
|
|
39
|
+
renderingType,
|
|
40
|
+
urlPartsByLabel: z.array(z.object({
|
|
41
|
+
label: z.string().optional(),
|
|
42
|
+
urlParts: z.array(z.array(z.string())),
|
|
43
|
+
})),
|
|
44
|
+
}))
|
|
45
|
+
.prefault([]),
|
|
46
|
+
});
|
|
47
|
+
const stateCodec = z.codec(persistedState, predictorState, {
|
|
48
|
+
decode: ({ logreg, detectionResults }) => ({
|
|
49
|
+
logreg: LogisticRegression.load(logreg),
|
|
50
|
+
detectionResults: new Map(detectionResults.map(({ renderingType, urlPartsByLabel }) => [
|
|
51
|
+
renderingType,
|
|
52
|
+
new Map(urlPartsByLabel.map(({ label, urlParts }) => [label, urlParts])),
|
|
53
|
+
])),
|
|
54
|
+
}),
|
|
55
|
+
encode: ({ logreg, detectionResults }) => ({
|
|
56
|
+
logreg: logreg.toJSON(),
|
|
57
|
+
detectionResults: Array.from(detectionResults.entries()).map(([renderingType, urlPartsByLabel]) => ({
|
|
58
|
+
renderingType,
|
|
59
|
+
urlPartsByLabel: Array.from(urlPartsByLabel.entries()).map(([label, urlParts]) => ({ label, urlParts })),
|
|
60
|
+
})),
|
|
61
|
+
}),
|
|
62
|
+
});
|
|
27
63
|
/**
|
|
28
64
|
* Stores rendering type information for previously crawled URLs and predicts the rendering type for URLs that have yet to be crawled and recommends when rendering type detection should be performed.
|
|
29
65
|
*
|
|
@@ -36,30 +72,10 @@ export class RenderingTypePredictor {
|
|
|
36
72
|
constructor({ detectionRatio, persistenceOptions }) {
|
|
37
73
|
this.#detectionRatio = detectionRatio;
|
|
38
74
|
this.state = new RecoverableState({
|
|
39
|
-
defaultState: {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
serialize: (state) => JSON.stringify({
|
|
44
|
-
logreg: state.logreg.toJSON(),
|
|
45
|
-
detectionResults: Array.from(state.detectionResults.entries()).map(([renderingType, urlPartsByLabel]) => ({
|
|
46
|
-
renderingType,
|
|
47
|
-
urlPartsByLabel: Array.from(urlPartsByLabel.entries()).map(([label, urlParts]) => ({
|
|
48
|
-
label,
|
|
49
|
-
urlParts,
|
|
50
|
-
})),
|
|
51
|
-
})),
|
|
52
|
-
}),
|
|
53
|
-
deserialize: (serializedState) => {
|
|
54
|
-
const { logreg, detectionResults = [] } = JSON.parse(serializedState);
|
|
55
|
-
return {
|
|
56
|
-
logreg: LogisticRegression.load(logreg),
|
|
57
|
-
detectionResults: new Map(detectionResults.map((serializedItem) => [
|
|
58
|
-
serializedItem.renderingType,
|
|
59
|
-
new Map(serializedItem.urlPartsByLabel.map((item) => [item.label, item.urlParts])),
|
|
60
|
-
])),
|
|
61
|
-
};
|
|
62
|
-
},
|
|
75
|
+
defaultState: () => stateCodec.decode({}),
|
|
76
|
+
// The codec validates in the decode direction, so it is a Standard Schema as-is; encoding needs a call.
|
|
77
|
+
deserialize: stateCodec,
|
|
78
|
+
serialize: (state) => stateCodec.encode(state),
|
|
63
79
|
persistStateKey: 'rendering-type-predictor-state',
|
|
64
80
|
persistenceEnabled: true,
|
|
65
81
|
...persistenceOptions,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crawlee/playwright",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.119",
|
|
4
4
|
"description": "The scalable web crawling and scraping library for JavaScript/Node.js. Enables development of data extraction and web automation jobs (not only) with headless Chrome and Puppeteer.",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22.0.0"
|
|
@@ -49,13 +49,13 @@
|
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"@apify/datastructures": "^2.0.3",
|
|
51
51
|
"@apify/timeout": "^0.4.4",
|
|
52
|
-
"@crawlee/basic": "4.0.0-beta.
|
|
53
|
-
"@crawlee/browser": "4.0.0-beta.
|
|
54
|
-
"@crawlee/browser-pool": "4.0.0-beta.
|
|
55
|
-
"@crawlee/cheerio": "4.0.0-beta.
|
|
56
|
-
"@crawlee/core": "4.0.0-beta.
|
|
57
|
-
"@crawlee/types": "4.0.0-beta.
|
|
58
|
-
"@crawlee/utils": "4.0.0-beta.
|
|
52
|
+
"@crawlee/basic": "4.0.0-beta.119",
|
|
53
|
+
"@crawlee/browser": "4.0.0-beta.119",
|
|
54
|
+
"@crawlee/browser-pool": "4.0.0-beta.119",
|
|
55
|
+
"@crawlee/cheerio": "4.0.0-beta.119",
|
|
56
|
+
"@crawlee/core": "4.0.0-beta.119",
|
|
57
|
+
"@crawlee/types": "4.0.0-beta.119",
|
|
58
|
+
"@crawlee/utils": "4.0.0-beta.119",
|
|
59
59
|
"cheerio": "^1.0.0",
|
|
60
60
|
"jquery": "^3.7.1",
|
|
61
61
|
"ml-logistic-regression": "^2.0.0",
|
|
@@ -63,7 +63,8 @@
|
|
|
63
63
|
"ow": "^2.0.0",
|
|
64
64
|
"string-comparison": "^1.3.0",
|
|
65
65
|
"tslib": "^2.8.1",
|
|
66
|
-
"type-fest": "^4.0.0"
|
|
66
|
+
"type-fest": "^4.0.0",
|
|
67
|
+
"zod": "^4.1.0"
|
|
67
68
|
},
|
|
68
69
|
"peerDependencies": {
|
|
69
70
|
"idcac-playwright": "^0.2.0",
|
|
@@ -84,5 +85,5 @@
|
|
|
84
85
|
}
|
|
85
86
|
}
|
|
86
87
|
},
|
|
87
|
-
"gitHead": "
|
|
88
|
+
"gitHead": "552fe2371a3c6e9e9f3514010536ea6abdc27eb4"
|
|
88
89
|
}
|