-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
108 lines (90 loc) · 2.26 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
// API
const express = require("express");
const request = require("request-promise");
const {
PRODUCT_DETAILS,
PRODUCT_REVIEWS,
PRODUCT_OFFERS,
PRODUCT_SEARCH,
ROOT,
} = require("./routes/routes");
// Initialize app
const app = express();
const PORT = process.env.PORT || 5000;
app.use(express.json()); // Allow app to parse json
const returnScraperApiUrl = (apiKey) =>
`http://api.scraperapi.com?api_key=${apiKey}&autoparse=true`;
// Routes
// Base Route
app.get(ROOT, (req, res) => {
res.send("Welcome to Amazon Scraper API from Junaid.");
});
// Get Product Details
app.get(PRODUCT_DETAILS, async (req, res) => {
// Get Id from params
const { productId } = req.params;
const { api_key } = req.query;
try {
const response = await request(
`${returnScraperApiUrl(
api_key
)}&url=https://www.amazon.com/dp/${productId}`
);
res.json(JSON.parse(response));
} catch (error) {
res.json(error);
}
});
// Get Product Reviews
app.get(PRODUCT_REVIEWS, async (req, res) => {
// Get Id from params
const { productId } = req.params;
const { api_key } = req.query;
try {
const response = await request(
`${returnScraperApiUrl(
api_key
)}&url=https://www.amazon.com/product-reviews/${productId}`
);
res.json(JSON.parse(response));
} catch (error) {
res.json(error);
}
});
// Get Product Offers
app.get(PRODUCT_OFFERS, async (req, res) => {
// Get Id from params
const { productId } = req.params;
const { api_key } = req.query;
try {
const response = await request(
`${returnScraperApiUrl(
api_key
)}&url=https://www.amazon.com/gp/offer-listing/${productId}`
);
res.json(JSON.parse(response));
} catch (error) {
res.json(error);
}
});
// Get Search Results
app.get(PRODUCT_SEARCH, async (req, res) => {
// Get Id from params
const { searchQuery } = req.params;
const { api_key } = req.query;
try {
const response = await request(
`${returnScraperApiUrl(
api_key
)}&url=https://www.amazon.com/s?k=${searchQuery}`
);
res.json(JSON.parse(response));
} catch (error) {
res.json(error);
}
});
// Start
app.listen(PORT, () => {
// After app is run
console.log(`Server running on PORT : ${PORT}`);
});