Skip to content

Instantly share code, notes, and snippets.

@araguaci
Forked from adrianhajdin/finished.js
Created July 17, 2021 06:12
Show Gist options
  • Save araguaci/753eeeeff8cfed5516210a54d11b4063 to your computer and use it in GitHub Desktop.
Save araguaci/753eeeeff8cfed5516210a54d11b4063 to your computer and use it in GitHub Desktop.
Amazon Scraper API
const express = require('express');
const request = require('request-promise');
const PORT = process.env.PORT || 5000;
const app = express();
app.use(express.json());
const returnScraperApiUrl = (apiKey) => `http://api.scraperapi.com?api_key=${apiKey}&autoparse=true`;
// Welcome route
app.get('/', async (req, res) => {
res.send('Welcome to Amazon Scraper 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(`${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('/products/:productId/reviews', async (req, res) => {
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('/products/:productId/offers', async (req, res) => {
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('/search/:searchQuery', async (req, res) => {
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);
}
});
app.listen(PORT, () => console.log(`Server Running on Port: ${PORT}`));
`${baseUrl}&url=https://www.amazon.com/dp/${productId}`)
`${baseUrl}&url=https://www.amazon.com/product-reviews/${productId}`);
`${baseUrl}&url=https://www.amazon.com/gp/offer-listing/${productId}`);
`${baseUrl}&url=https://www.amazon.com/s?k=${searchQuery}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment