-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (54 loc) · 1.89 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
const express = require('express');
const request = require('request-promise');
const app = express();
const PORT = process.env.PORT || 5000;
const generateScraperUrl = apiKey => `http://api.scraperapi.com?api_key=${apiKey}&autoparse=true`;
app.use(express.json());
app.get('/', (req, res) => {
res.send('Welcome to Amazon Scrapper API!');
});
//GET Product Details
app.get('/products/:productId', async (req, res) => {
const { productId } = req.params
const { api_key } = req.query
try {
const response = await request(`${generateScraperUrl(api_key)}&url=https://www.amazon.in/dp/${productId}`)
res.json(JSON.parse(response))
} catch (error) {
res.json(error)
}
})
//GET Product Reviews
app.get('/products/:productId/reviews', async (req, res) => {
const { productId } = req.params
const { api_key } = req.query
try {
const response = await request(`${generateScraperUrl(api_key)}&url=https://www.amazon.in/product-reviews/${productId}`)
res.json(JSON.parse(response))
} catch (error) {
res.json(error)
}
})
//GET Product Offers
app.get('/products/:productId/offers', async (req, res) => {
const { productId } = req.params
const { api_key } = req.query
try {
const response = await request(`${generateScraperUrl(api_key)}&url=https://www.amazon.in/gp/offer-listing/${productId}`)
res.json(JSON.parse(response))
} catch (error) {
res.json(error)
}
})
//GET Search Results
app.get('/search/:searchQuery', async (req, res) => {
const { searchQuery } = req.params
const { api_key } = req.query
try {
const response = await request(`${generateScraperUrl(api_key)}&url=https://www.amazon.in/s?k=${searchQuery}`)
res.json(JSON.parse(response))
} catch (error) {
res.json(error)
}
})
app.listen(PORT, () => console.log(`Server running on port ${PORT}`))