-
Notifications
You must be signed in to change notification settings - Fork 4
/
magelo_item_meta_data_scrape.js
137 lines (106 loc) · 3.13 KB
/
magelo_item_meta_data_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var request = require('requestretry');
console.log("\n");
console.log("Note: Script relies on items table data to get item_id's");
console.log("\n");
var mysql = require('mysql');
var config = require('./config.json');
var con = mysql.createConnection({
host: "localhost",
user: config.mysql_user,
password: config.mysql_password,
database: config.mysql_database
});
var items = [];
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("select id FROM items GROUP BY id", function (err, result, fields) {
if (err) throw err;
for (var i = 0; i < result.length; i++) {
items.push(result[i].id);
}
// process.exit(1);
});
});
last_poll_action_time = get_unix_time();
setTimeout(function(){
var index = 0;
setInterval(function() {
current_time = get_unix_time();
if(current_time > (last_poll_action_time + 10)){
// console.log("Ready to poll");
poll_item(items[index]);
if(!items[index]){
process.exit(1);
}
index++;
}
else {
// console.log("Still polling");
}
}, 10);
}, 2000);
function get_unix_time() {
return Math.round((new Date()).getTime());
}
function poll_item (item_id) {
(function(item_id){
request(
{
url: 'https://eq.magelo.com/item/' + item_id,
maxAttempts: 100,
retryDelay: 5000,
retrySrategy: request.RetryStrategies.HTTPOrNetworkError // (default) retry on 5xx or network errors
}
,
function (error, response, body) {
if(error)
console.log('error:', error); // Print the error if one occurred
if(typeof body === "undefined")
return false;
if(body == "")
return false;
if(error)
return false;
console.log("Polling item: %s", item_id);
var cheerio = require('cheerio'),
$ = cheerio.load(body);
initial_entry = 0;
initial_entry_human = "";
last_updated = 0;
last_updated_human = "";
$( "table td" ).each(function() {
html = $( this ).html();
if(/PM|AM/.test(html)){
date = new Date(html).toLocaleDateString();
time = new Date(html).toLocaleTimeString();
unix_time = Math.round((new Date(html)).getTime() / 1000);
if(initial_entry == 0){
initial_entry = unix_time;
initial_entry_human = date + ' ' + time;
}
else if(last_updated == 0){
last_updated = unix_time;
last_updated_human = date + ' ' + time;
}
// console.log("entry: %s date: %s time: %s unix_time: %s", html, date, time, unix_time);
insert_data = [item_id, initial_entry, initial_entry_human, last_updated, last_updated_human];
}
});
// console.log("initial: %s last: %s", initial_entry_human, last_updated_human);
if(initial_entry == 0 && last_updated == 0)
return false;
console.log(insert_data);
con.query(
'REPLACE INTO `magelo_item_meta_data` (item_id, initial_entry_date_unix, initial_entry_date_human, last_updated_date_unix, last_updated_date_human) ' +
'VALUES (?, ?, ?, ?, ?)',
insert_data,
function (err, results) {
if(err)
console.log(err);
}
);
last_poll_action_time = get_unix_time();
});
})(item_id);
}