Agent/scrapper/controllers/lot.js

84 lines
1.8 KiB
JavaScript

const asyncHandler = require("express-async-handler");
const {ScraperTools} = require('../AuctionServices/Scraper/Scraper.js')
const Drouot = require('../AuctionServices/Scraper/Drouot/Drouot.js')
const Interencheres = require('../AuctionServices/Scraper/Interencheres/Interencheres.js')
let getAuctionPlatform = function(Url){
let AuctionPlatform
let STools = new ScraperTools();
switch (STools.detectPlatform(Url)) {
case STools._CONST_INTERENCHERES:
AuctionPlatform = new Interencheres(Url);
break;
case STools._CONST_DROUOT:
AuctionPlatform = new Drouot(Url);
break;
default:
break;
}
return AuctionPlatform
}
let CleanUrl = function(url){
if(String(url).split("http").length > 1){
url = 'http'+String(url).split("http")[1]
}else{
url = ""
}
return url
}
// Display list of all pictures of a lot.
exports.getPictures = asyncHandler(async (req, res, next) => {
let url = req.params.url
url = decodeURIComponent(url);
url = CleanUrl(url)
if(url == ""){
res.status(400).send("URL not supported")
}
try{
let AuctionPlatform = getAuctionPlatform(url);
if(AuctionPlatform){
const PictList = await req.puppeteerCluster.execute(AuctionPlatform.getPictures);
res.json(PictList);
}else{
res.status(400).send("URL not supported")
}
}catch(e){
res.status(500).send("Error: "+e)
}
});
exports.getInfos = asyncHandler(async (req, res, next) => {
let url = req.params.url
url = decodeURIComponent(url);
url = CleanUrl(url)
if(url == ""){
res.status(400).send("URL not supported")
}
try{
let AuctionPlatform = getAuctionPlatform(url);
if(AuctionPlatform){
const LotInfos = await req.puppeteerCluster.execute(AuctionPlatform.getLotInfos);
res.json(LotInfos);
}else{
res.status(400).send("URL not supported")
}
}catch(e){
res.status(500).send("Error: "+e)
}
});