-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathscrapeAllSources.js
More file actions
185 lines (156 loc) · 4.97 KB
/
Copy pathscrapeAllSources.js
File metadata and controls
185 lines (156 loc) · 4.97 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
import { logInfo, logWarn } from "../logger.js";
function normalizeJob(job, keyword, adapter) {
return {
...job,
source: job.source || adapter.sourceName || "unknown",
keyword: job.keyword || job.palavraChave || keyword,
palavraChave: job.palavraChave || job.keyword || keyword,
palavra: job.keyword || job.palavraChave || keyword,
};
}
function normalizeComparableText(value) {
return String(value || "")
.normalize("NFD")
.replace(/\p{Diacritic}/gu, "")
.toLowerCase()
.replace(/[^\p{L}\p{N}]+/gu, " ")
.trim()
.replace(/\s+/g, " ");
}
function normalizeComparableUrl(value) {
const rawValue = String(value || "").trim();
if (!rawValue) {
return "";
}
try {
const parsedUrl = new URL(rawValue);
parsedUrl.search = "";
parsedUrl.hash = "";
return `${parsedUrl.origin}${parsedUrl.pathname}`.replace(/\/+$/, "");
} catch {
return rawValue.split(/[?#]/)[0].replace(/\/+$/, "");
}
}
function getMergedKeywords(...jobs) {
return [
...new Set(
jobs.flatMap((job) =>
[
...(Array.isArray(job.keywords) ? job.keywords : []),
job.keyword,
job.palavraChave,
job.palavra,
]
.filter(Boolean)
.map((keyword) => String(keyword).trim()),
),
),
];
}
function getMergedSources(...jobs) {
return [
...new Set(
jobs.flatMap((job) =>
[
...(Array.isArray(job.sources) ? job.sources : []),
job.source,
]
.filter(Boolean)
.map((source) => String(source).trim()),
),
),
];
}
function pickPreferredValue(...values) {
return (
values
.map((value) => String(value || "").trim())
.filter(Boolean)
.sort((left, right) => right.length - left.length)[0] || ""
);
}
function mergeKeywords(existing, incoming) {
const mergedKeywords = getMergedKeywords(existing, incoming);
const mergedSources = getMergedSources(existing, incoming);
return {
...existing,
...incoming,
titulo: pickPreferredValue(existing.titulo, incoming.titulo, existing.title, incoming.title),
empresa: pickPreferredValue(existing.empresa, incoming.empresa, existing.company, incoming.company),
local: pickPreferredValue(existing.local, incoming.local, existing.location, incoming.location),
link: pickPreferredValue(existing.link, incoming.link, existing.jobUrl, incoming.jobUrl),
jobUrl: pickPreferredValue(existing.jobUrl, incoming.jobUrl, existing.link, incoming.link),
source: mergedSources.join(", ") || existing.source || incoming.source || "",
sources: mergedSources,
keyword: mergedKeywords[0] || "",
palavraChave: mergedKeywords[0] || "",
keywords: mergedKeywords,
palavra: mergedKeywords[0] || "",
};
}
function buildDedupKey(job) {
const title = normalizeComparableText(job.titulo || job.title);
const company = normalizeComparableText(job.empresa || job.company);
const location = normalizeComparableText(job.local || job.location);
if (title && company && location) {
return `identity:${title}|${company}|${location}`;
}
if (title && company) {
return `identity:${title}|${company}|${location || "sem-local"}`;
}
const normalizedLink = normalizeComparableUrl(job.link || job.jobUrl);
if (normalizedLink) {
return `url:${normalizedLink}`;
}
return `fallback:${title}|${company}|${location}|${normalizeComparableText(job.source)}`;
}
function dedupeJobs(jobs) {
const unique = new Map();
for (const job of jobs) {
const key = buildDedupKey(job);
if (unique.has(key)) {
const existing = unique.get(key);
unique.set(key, mergeKeywords(existing, job));
continue;
}
const mergedKeywords = getMergedKeywords(job);
const mergedSources = getMergedSources(job);
unique.set(key, {
...job,
source: mergedSources.join(", ") || job.source || "",
sources: mergedSources,
palavra: job.keyword || job.palavraChave || job.palavra || "",
keywords: mergedKeywords,
});
}
return [...unique.values()];
}
export async function scrapeAllSources(adapters, config) {
const allJobs = [];
for (const adapter of adapters) {
logInfo(`Iniciando fonte: ${adapter.sourceName}`);
for (const keyword of config.keywords) {
try {
const jobs = await adapter.search(keyword, config);
if (!Array.isArray(jobs)) {
logWarn(`${adapter.sourceName}: retorno inválido para "${keyword}"`);
continue;
}
const normalizedJobs = jobs.map((job) =>
normalizeJob(job, keyword, adapter),
);
allJobs.push(...normalizedJobs);
logInfo(
`${adapter.sourceName}: ${normalizedJobs.length} vagas para "${keyword}"`,
);
} catch (error) {
logWarn(
`${adapter.sourceName}: falha ao buscar "${keyword}" -> ${
error instanceof Error ? error.message : "erro desconhecido"
}`,
);
}
}
}
return dedupeJobs(allJobs);
}