-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
85 lines (73 loc) · 2.39 KB
/
commands.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
var request = require('request'),
cheerio = require('cheerio'),
Buffer = require('buffer').Buffer,
iconv = require('iconv').Iconv,
mongoProductsDb = require('./productsDb').getProductsDb(),
baseUrl = 'http://www.goodsmatrix.ru/',
$body,
options = {
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:32.0) Gecko/20100101 Firefox/32.0'
},
encoding: 'binary'
};
exports.findAll = function(req, res) {
mongoProductsDb.collection('products', function(err, collection) {
collection.find().toArray(function(err, items) {
res.send(items);
});
});
};
exports.findById = function (req, res) {
barCode = req.params.id;
mongoProductsDb.collection('products', function(err, collection) {
collection.findOne({barCode: barCode}, function(err, doc) {
if(doc != null) {
res.send(doc);
} else {
options.url = baseUrl + 'goods/' + barCode + '.html';
sendExternalRequest(function (goodObject) {
res.send(goodObject);
}, function() {
res.status(404).send({error: 'requested product not found'});
});
}
});
});
};
function sendExternalRequest (suceessCallback, errorCallback) {
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
$body = cheerio.load(encode(body));
var goodObject = {
img: baseUrl + $body('#ctl00_ContentPH_LSGoodPicture_GoodImg').attr('src'),
title: $body('#ctl00_ContentPH_GoodsName').text().trim(),
barCode: $body('#ctl00_ContentPH_BarCodeL').html(),
content: $body('#ctl00_ContentPH_Composition').text().trim(),
bgu: ($body('#ctl00_ContentPH_ESL')) ? $body('#ctl00_ContentPH_ESL').text().trim() : null
};
if (goodObject.barCode != void 0 || goodObject.barCode != null) {
insert(goodObject);
suceessCallback.apply(this, [goodObject]);
} else {
console.log('Invalid barcode');
errorCallback.apply(this, arguments);
}
} else {
console.log(error + ': ' + response.statusCode);
errorCallback.apply(this, arguments);
}
});
}
function encode(body) {
body = new Buffer(body, 'binary');
conv = new iconv('windows-1251', 'utf8');
return conv.convert(body).toString();
}
function insert(goodObject) {
mongoProductsDb.collection('products', function(err, collection) {
collection.insert(goodObject, function(err, records) {
console.log("Record added as "+records[0].barCode);
});
});
}