-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
191 lines (145 loc) · 3.86 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
import fetch from "node-fetch"
import { load } from "cheerio"
import urlParser from "url"
import {macbookPro} from "./DB/Shemas/macbookpro.js"
import {macbookAir} from "./DB/Shemas/macbookair.js"
import {iphoneColl} from "./DB/Shemas/iphone.js"
import {bigDataX} from "./DB/Shemas/bigData.js"
const seenUrls = {};
function isEmpty(arr) {
try {
let somArray = []
for (const key in arr) {
if (Object.hasOwnProperty.call(arr, key)) {
if (arr[key]) {
somArray.push(arr[key])
}
}
}
return somArray
} catch (error) {
console.error("Fim ", error)
}
}
function ChangeData(arr) {
if (arr.length > 0) {
let myStringArr = arr.join(" ")
var newarr = myStringArr.split("€")
}
return newarr
}
function makeIterable(arr) {
try {
let somArray = []
for (const key in arr) {
if (Object.hasOwnProperty.call(arr, key)) {
somArray.push(arr[key])
}
}
return somArray
} catch (error) {
console.error("Fim ", error)
}
}
function saveTodatabaseImage(shema, array) {
try {
for (let index = 0; index < array.length; index++) {
if (array[index].startsWith("http") || array[index].startsWith("https")) {
shema.findOneAndUpdate({ image: array[index] }, { image: array[index] }, {
new: true,
upsert: true
});
}
}
} catch (error) {
console.error("Done in DB ", error)
}
}
async function saveTodatabaseTitle(shema, array) {
try {
for (let index = 0; index < array.length; index++) {
if (array[index].length > 0) {
await shema.findOneAndUpdate({ title: array[index] }, { title: array[index] }, {
new: true,
upsert: true
})
}
}
} catch (error) {
console.error("Done in DB ", error)
}
}
function saveTodatabasePrice(shema, array) {
try {
for (let index = 0; index < array.length; index++) {
if (array[index].length > 0) {
shema.findOneAndUpdate({ price: array[index] }, { price: array[index] }, {
new: true,
upsert: true
});
}
}
} catch (error) {
console.error("Done in DB ", error)
}
}
const getUrl = (link, host, protocol) => {
if (link.startsWith("https")) {
return link;
} else if (link.startsWith("/")) {
return `${protocol}//${host}${link}`;
} else {
return `${protocol}//${host}/${link}`;
}
};
const crawl = async ({ url, ignore }) => {
if (seenUrls[url]) return;
seenUrls[url] = true;
const { host, protocol } = urlParser.parse(url);
const response = await fetch(url);
const html = await response.text();
const $ = load(html);
const links = $("a")
.map((i, link) => link.attribs.href)
.get();
let myArray = []
myArray.push($("title")
.map((i, link) => { return link })
.text());
let objTitle = {}
let arrPrice = []
let arrImage = []
$("img")
.map((i, link) => arrImage.push(getUrl(link.attribs.src)))
.get();
for (let index = 0; index < myArray.length; index++) {
const Regex = /[a-zA-Z]$/
const myText = Regex.test(myArray[index])
if (myText == false) { continue }
else {
let data = $(".has-padding-small-bottom")
let dataTitle = $(".has-padding-small-bottom")
let dataPrice = $(".price")
for (let index = 0; index < data.length; index++) {
let regex = /^\n/
let dataVerify = regex.test(dataTitle.text())
if (dataVerify == false) {
objTitle.title = dataTitle.text()
}
if (dataPrice.text() != '' && dataPrice.text() != {}) arrPrice.push((dataPrice.text()))
}
}
}
links
.filter((link) => link.includes(host) && !link.includes(ignore))
.forEach((link) => {
crawl({
url: getUrl(link, host, protocol),
ignore,
});
});
};
crawl({
//url: process.env.Source_LINK,
ignore: "/search",
});