-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (115 loc) · 3.93 KB
/
index.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
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var GoogleSpreadsheet = require('google-spreadsheet');
var async = require('async');
var AWS = require('aws-sdk');
var cells;
var doc = new GoogleSpreadsheet('1jhQ_2gULNnl6lLX01dGfXfcaGr-tyoZylDThW04E6co');
var amazon = require('amazon-product-api');
var sheet;
// client.itemLookup({
// idType: 'ASIN',
// itemId: 'B00NXO1U18'
// }).then(function(results) {
// var res = JSON.stringify(results);
// var objres = JSON.parse(res);
// console.log(objres[0].ItemAttributes[0].PackageDimensions);
// }).catch(function(err) {
// console.log(err);
// });
function getASIN(url) {
var urla = url;
var regex = RegExp("http://www.amazon.com/([\\w-]+/)?(dp|gp/product)/(\\w+/)?(\\w{10})");
m = urla.match("/([A-Z0-9]{10})(?:[/?]|$)")
if (m) {
return m[1];
}
}
function getTitle(obj){
return obj[0].ItemAttributes[0].Title[0];
}
function getWeight(obj){
return obj[0].ItemAttributes[0].PackageDimensions[0].Weight[0]._ + ' ' + obj[0].ItemAttributes[0].PackageDimensions[0].Weight[0].$.Units;
}
function getDimensions(obj){
var Height = obj[0].ItemAttributes[0].PackageDimensions[0].Height[0]._ + ' ' + obj[0].ItemAttributes[0].PackageDimensions[0].Height[0].$.Units;
var Width =obj[0].ItemAttributes[0].PackageDimensions[0].Width[0]._ + ' ' + obj[0].ItemAttributes[0].PackageDimensions[0].Width[0].$.Units;
var Length =obj[0].ItemAttributes[0].PackageDimensions[0].Length[0]._ + ' ' + obj[0].ItemAttributes[0].PackageDimensions[0].Length[0].$.Units;
return Height + ', ' + Width + ', ' + Length;
}
AWS.config.loadFromPath('./config.json');
function getReview(url) {
var $;
var price;
var title;
request(url, function(error, response, html) {
console.log("error= " + error +" response = "+ response.statusCode);
if (!error && response.statusCode == 200) {
$ = cheerio.load(html);
title = $('title').text();
price = ($('.a-color-prices') || $('#priceblock_ourprice') || $('#priceblock_saleprice')).text();
console.log(title + ' \n' + price);
return $;
}
});
}
async.series([
function setAuth(step) {
var creds = require('./API Project-ecdd436184d5.json');
doc.useServiceAccountAuth(creds, step);
},
function getInfoAndWorksheets(step) {
doc.getInfo(function(err, info) {
console.log('Loaded doc: '+info.title+' by '+info.author.email);
sheet = info.worksheets[0];
console.log('sheet 1: '+sheet.title+' '+sheet.rowCount+'x'+sheet.colCount);
step();
});
},
function workingWithCells(step) {
sheet.getCells({
'min-row': 3,
'max-row': 10,
'return-empty': true
},function(err, cellss) {
cells = cellss;
var i=2;
(function() {
if (i < cells.length) {
// scraper.init(cells[i].value, function(data){
// console.log(data.price || data.sale_price);
// // console.log(data.url);
// // console.log(data.price);
// });
getReview(cells[i].value,'#priceblock_saleprice');
// allObj.push(getObj);
console.log("i=" + i);
// var asin = getASIN(cells[i].value);
// client.itemLookup({
// idType: 'ASIN',
// itemId: asin
// }).then(function(results) {
// var res = JSON.stringify(results);
// console.log(res);
// var objres = JSON.parse(res);
// var dim = objres[0].ItemAttributes[0].PackageDimensions;
// }).catch(function(err) {
// console.log(err);
// });
i+=27;
setTimeout(arguments.callee, 5000);
} else {
console.log('Закончили');
}
})();
// for(var i=2; i<cells.length; i+=27) {
// console.log('i=' + i + ' Cell R'+cells[i].row+'C'+cells[i].col+' = '+cells[i].value);
// }
step();
});
},
function cherioo(step) {
step();
}
]);