/
index.js
141 lines (116 loc) · 3.13 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use strict';
var colors = require('colors');
var jsdom = require('jsdom');
var jquery = require('jquery');
var fs = require('fs');
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
var $Dashes = '--------------------------------------------------------------------------------';
var log = function(message) {
if(message) {
console.log(message.toString().info);
}
};
var error = function(message) {
if (message) {
console.error(message.toString().error);
}
};
/*
var warn = function(message) {
if (message) {
console.warn(message.toString().warn); //jshint ignore:line
}
};
*/
var bigInfo = function(message) {
message = '\n' + $Dashes + '\n' + message + '\n' + $Dashes + '\n';
log(message);
};
var $ProcessedCount = 0;
var $ProcessedPage = 1;
var $BaseURL = 'http://www.generalhobby.com/airplanes-browse-airplane-c-21_37.html';
var $TotalAvailable = 0;
var $JSONData = [];
var dumpJSONInfo = function() {
var jsonData = {
date: new Date(),
products: $JSONData
};
fs.writeFile('app/scripts/data.json', 'var $DATA = ' + JSON.stringify(jsonData, null, 4) + ';', function(e) {
if (e) {
error(e);
}
else {
log('Data saved!');
}
});
};
var $Config = {
url: $BaseURL,
scripts: null,
done: function(errors, window) {
if (errors) {
error(errors);
}
var $ = jquery(window);
var countText = $('DIV#main.col-main FORM TABLE.pager TBODY TD STRONG').text();
var totalCount = parseInt(countText.substring(0, countText.length - 8), 10);
var listingItems = $('DIV#main.col-main div.listing-item');
log('Processing ' + listingItems.length + ' items');
listingItems.each(function() {
var itemText = $('DIV.product-shop H5 A:last-child', this).text();
var pageUrl = $('DIV.product-image A', this).attr('href');
var imageUrl = $('DIV.product-image A IMG', this).attr('src');
var item = {
name: itemText,
soldOut: true,
imageUrl: 'http://www.generalhobby.com/' + imageUrl,
pageUrl: pageUrl,
regularPrice: null,
specialPrice: null
};
var regularPriceElement = $('.price-box .regular-price .price', this);
var specialPriceElement = $('S', regularPriceElement);
if (specialPriceElement.length) {
item.regularPrice = specialPriceElement.text();
item.specialPrice = $('.productSpecialPrice', regularPriceElement).text();
}
else {
item.regularPrice = regularPriceElement.text();
}
$JSONData.push(item);
if ($('DIV.product-shop SPAN:contains("Sold Out")', this).length) {
return;
}
else {
item.soldOut = false;
$TotalAvailable++;
log(itemText);
}
});
$ProcessedCount += listingItems.length;
bigInfo('Completed processing of ' + $ProcessedCount + ' items!');
if ($ProcessedCount < totalCount) {
var page = $BaseURL + '?page=' + (++$ProcessedPage).toString() + '&sort=5a';
log(page);
$Config.url = page;
jsdom.env($Config);
}
else {
dumpJSONInfo();
bigInfo('Processed ' + $TotalAvailable + ' available items!');
}
}
};
jsdom.env($Config);