This repository has been archived by the owner on Jun 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
getAuctions.js
126 lines (114 loc) · 3.88 KB
/
getAuctions.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var cheerio = require('cheerio');
var request = require('request');
var tidy = require('htmltidy').tidy;
var moment = require('moment');
var url = require('url');
var http = require('http');
// var sizeOf = require('image-size');
// var imagesize = require('imagesize');
exports.listAuctions = function(req, res){
var auctionsArray = [];
var queryCat = 0;
var querySeller = "12";
var queryPage = 1;
var queryTerm = "";
if(req.query.page) {
queryPage = req.query.page;
};
if(req.query.category) {
queryCat = req.query.category;
};
if(req.query.seller) {
querySeller = req.query.seller;
};
if(req.query.term) {
queryTerm = req.query.term;
};
var url = {
base: 'http://www.shopgoodwill.com/search/searchKey.asp?showthumbs=on&sortBy=itemEndTime&closed=no&SortOrder=a&sortBy=itemEndTime&',
page: queryPage,
seller: querySeller,
cat: queryCat,
title: queryTerm,
min: null,
max: null,
get full () {
return this.base+'itemTitle='+this.title+'&catid='+this.cat+'&sellerID='+this.seller+'&page='+this.page;
}
};
var scrapeItems = function(html) {
//console.log(html);
var $ = cheerio.load(html);
// get a cheerio object array of the table rows
var itemRows = $('table.productresults tbody').first().children('tr');
// console.log(itemRows.length);
// iterate over rows and pull out available data
if (itemRows.length < 1) {
console.log("less than");
// res.send(204, { error: "looks like this isn't a real page. I mean don't get me wrong. It's there, but there's no table on the page." });
res.status(204, { error: "looks like this isn't a real page. I mean don't get me wrong. It's there, but there's no table on the page." }).send("boo");
}
else {
itemRows.each(function(i, el) {
var auction = {};
var itemTH = $(el).children('th');
auction.itemNumber = itemTH.eq(0).html().trim();
auction.itemName = itemTH.eq(1).children('a').html();
auction.itemName = auction.itemName.replace(/(\r\n|\n|\r)/gm,"");
auction.itemURL = itemTH.eq(1).children('a').attr('href');
auction.itemImage = itemTH.eq(1).children('img').attr('src');
console.log(auction.itemImage);
if(auction.itemImage != undefined) {
auction.itemImage = auction.itemImage.replace("-thumb","");
} else {
console.log(auction.itemImage);
}
auction.itemPrice = itemTH.eq(2).find('b').html();
auction.itemPrice = auction.itemPrice.replace("$","");
auction.itemBids = itemTH.eq(3).html();
auction.itemEnd = itemTH.eq(4).html();
auction.itemEnd = moment(auction.itemEnd, 'M/D/YYYY h:m:s a').fromNow();
auctionsArray.push(auction);
if(itemRows.length === i+1) {
// console.log("sending JSON");
sendJSON();
};
}); // end itemRows.each
}; // end else
}; // end scrapeItems
var getImageSize = function() {
var getImage = http.get(auction.itemImage, function (response) {
imagesize(response, function (err, result) {
if(err) console.log(err);
auction.itemH = result.height;
auction.itemW = result.width;
auction.imageRatio = result.height/result.width;
// addAuction(auction, i, itemRows.length);
getImage.abort();
}); // end imagesize
}); // end getImage
}
var sendJSON = function() {
res.jsonp(auctionsArray);
};
var tidyPage = function(body) {
tidy(body, function(err, html) {
if(err){
res.jsonp(err);
return;
} else {
scrapeItems(html);
}
});
};
request(url.full, function(error, response, body) {
if(error) {
console.log(error);
res.jsonp(error);
} else {
console.log("Dirty HTML received");
console.log(url.full);
tidyPage(body);
}
});
}