-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
84 lines (79 loc) · 2.2 KB
/
middleware.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
const scrape = require('scrape-metadata')
const axios = require('axios')
const cheerio = require("cheerio")
const scrapeSeedSavers = (url, callback) => {
scrape(url, (err, meta) => {
const data = {
title: meta["ogTitle"],
source: meta["ogSiteName"],
img: meta["ogImage"],
description: meta["description"],
url: meta["ogUrl"]
}
callback(err, data)
})
}
const scrapeSouthernExposure = (url, callback) => {
axios.get(url).then((res, err) => {
const $ = cheerio.load(res.data)
const data = {
description: $("#productDescription").text(),
img: `http://www.southernexposure.com/${$("#productMainImage a").attr("href")}`,
source: "Southern Exposure Seed Exchange",
url: url,
title: $("#productGeneral .page-header h1").text()
}
scrape(data.url, (err, meta) => {
data.description = meta["description"]
callback(err, data)
})
})
}
const scrapeRareSeeds = (url, callback) => {
axios.get(url).then((res, err) => {
const $ = cheerio.load(res.data)
const data = {
description: $(".longDescription").text(),
title: $(".reviewsWrapper .multiColumn .reviewItem .grid_6 .item .fn").text(),
source: "Baker Creek Heirloom Seeds",
url: url,
img: `http://www.rareseeds.com${$("#productImage .mainImage").attr("src")}`
}
callback(err, data)
})
}
const scrapeData = (req, res, next) => {
if(req.originalUrl != "/api"){
next()
}else{
const url = req.body.url
if(url.match("southernexposure.com")){
scrapeSouthernExposure(req.body.url, (err, data) => {
if(err){
res.send(500)
}
req.body.payload = data
next()
})
}else if(url.match("seedsavers.org")){
scrapeSeedSavers(req.body.url, (err, data) => {
if(err){
res.send(500)
}
req.body.payload = data;
next()
})
}else if(url.match("rareseeds.com")){
scrapeRareSeeds(url, (err, data) => {
if(err){
res.send(500)
}
req.body.payload=data
next()
})
}else{
res.send("400 Error: I don't know how to scrape that site")
}
}
}
module.exports.scrapeData = scrapeData