-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (70 loc) · 2.13 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
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
const express = require("express");
const app = express();
const cors = require("cors");
const PORT = 3000;
const { mongoClient } = require("./Database/db.js");
const amazonScraper = require("./Webscrapers/AmazonScraper/amazon");
const noonScraper = require("./Webscrapers/NoonScraper/noon");
const alibabaScraper = require("./Webscrapers/AlibabaScrapper/alibaba");
app.use(cors());
app.get("/amazonscraper", async (req, res) => {
try {
const data = await amazonScraper();
res.json(data);
} catch (e) {
res.status(500).send("sorry, an error has occured");
}
});
app.get("/alibabascraper", async (req, res) => {
try {
const data = await alibabaScraper();
const query = req.query.q;
if (query) {
res.json(
data.filter((d) => d.title.toLowerCase().includes(query.toLowerCase()))
);
return;
}
res.json(data);
} catch (e) {
res.status(500).send("sorry, an error has occured");
}
});
app.get("/noonscraper", async (req, res) => {
try {
const data = await noonScraper();
res.json(data);
} catch (e) {
res.status(500).send("sorry, an error has occured");
}
});
app.get("/all", async (req, res) => {
const amazonData = await amazonScraper();
const noonData = await noonScraper();
const alibabaData = await alibabaScraper();
const dataAggregate = amazonData.concat(noonData, alibabaData);
if (dataAggregate && dataAggregate.length) {
const db = await mongoClient("iphones");
if (!db) {
return res
.status(500)
.json({ message: "Unable to establish database connection" });
}
for (const item of dataAggregate) {
const existing = await db.findOne({ link: item.link });
//If an item exists with the same URL, then just update the name and price
if (existing) {
db.updateOne(
{ link: item.link },
{ $set: { title: item.title, price: item.price } }
);
} else {
db.insertOne(item);
}
}
}
res.json(dataAggregate);
});
app.listen(PORT, () => console.log(`Server Started on PORT ${PORT}`));
module.exports = { app };