-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.js
115 lines (109 loc) · 3.58 KB
/
scrape.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
var request = require('request');
var cheerio = require('cheerio');
var async = require('async');
var mongoose = require('mongoose');
var baseUrl = 'http://www3.consumer.org.hk/pricewatch/supermarket/';
var detailUrl = 'detail.php?itemcode=';
var productSchema = new mongoose.Schema({
_id: String,
name: String,
brand: String,
// Queries will usually be on _id or category
category: { type: String, index: true },
prices: [new mongoose.Schema({
date: Date,
wellcome: { price: String, discount: Boolean },
parknshop: { price: String, discount: Boolean },
marketplace: { price: String, discount: Boolean },
aeon: { price: String, discount: Boolean },
dch: { price: String, discount: Boolean }
}, {_id: false})]
});
var Product = mongoose.model('Product', productSchema);
mongoose.connect('mongodb://localhost/groceries');
mongoose.connection.on('error', function() {
console.error('MongoDB Connection Error. Make Sure MongoDB is running.');
});
mongoose.connection.on('open', function() {
console.log('Connected to MongoDB successfully.')
});
var timerId = setInterval(main, 20000);
// Extract data and push it to MongoDB
function main() {
request(baseUrl + 'index.php?keyword=&lang=en', function(err, res, html) {
if (!err && res.statusCode == 200) {
var $ = cheerio.load(html);
console.log('Loaded HTML');
// Get siblings of the table's header row
var items = $('tr[bgcolor=#FED785]').siblings();
console.log('Parsed '+items.length+' products');
var tasks = []; // Collect updates for async.parallel
items.each(function(i, el) {
var cols = $(el).children();
var category = $(cols).eq(1);
var brand = $(cols).eq(2);
var name = $(cols).eq(3);
// Store id portion of product url
var itemcode = name.find('a').attr('href').substring(20);
var wellcome = $(cols).eq(4);
var parknshop = $(cols).eq(5);
var marketplace = $(cols).eq(6);
var aeon = $(cols).eq(7);
var dch = $(cols).eq(8);
var price = Price(wellcome, parknshop, marketplace, aeon, dch);
if (price) {
tasks.push(function(callback) {
Product.update(
{
_id: itemcode,
name: name.text(),
brand: brand.text(),
category: category.text()
},
{
$push: {
prices: price
}
},
{upsert: true},
function(e, res) {
if (e) return console.log(e);
callback(null, res);
});
});
}
});
async.parallel(
tasks,
function(err, results) {
console.log('Upserted '+results.length+' products');
});
}
});
}
// XXX: Custom factory for price schema instead of Mongoose Model
function Price(wellcome, parknshop, marketplace, aeon, dch) {
wellcome = normalizePriceData(wellcome);
parknshop = normalizePriceData(parknshop);
marketplace = normalizePriceData(marketplace);
aeon = normalizePriceData(aeon);
dch = normalizePriceData(dch);
if (!(wellcome || parknshop || marketplace || aeon || dch)) {
return undefined;
}
return {
date: Date.now(),
wellcome: wellcome,
parknshop: parknshop,
marketplace: marketplace,
aeon: aeon,
dch: dch
};
}
function normalizePriceData (price) {
if (price.text() == '--') return undefined; // Mongoose ignores undefined fields
return {
price: price.text().trim().replace(' ', '').substring(1),
discount: Boolean(price.find('a').attr('href'))
};
}