-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.html
68 lines (64 loc) · 2.16 KB
/
scraper.html
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
<script src="https://code.jquery.com/jquery-2.1.3.js"></script>
<script>
// Get all the days we want.
var endDate = new Date(2050, 0 , 1);
var daysOfYear = [];
for (var d = new Date(2015, 0, 1); d <= endDate; d.setDate(d.getDate() + 1)) {
daysOfYear.push(new Date(d));
}
var colorToLuckMap = {
'#FF0000': 1, // red is lucky
'#0000FF': 0, // blue is neutral
'#000000': -1 // black is bad luck
};
var periodsPerYear = {};
var getPeriodsInDay = function(year, month, day, callback) {
$.post('http://www.chinesefortunecalendar.com/TDB/Luckyhours.asp', {
SunYear: year,
SunMonth: month,
SunDay: day
}, function(data) {
data = data.replace(/(href|src|img)/g, 'bullshit');
// d is the raw string response of html data.
// create a temporary node to work with the data.
var rootNode = document.createElement('div');
rootNode.innerHTML += data;
var table = rootNode.querySelectorAll('table')[2];
var rows = table.querySelectorAll('tr');
// should have 4 columns per row, representing a 2-hour period each.
var allPeriodsPerDay = [];
Array.prototype.forEach.call(rows, function(row) {
var columns = row.querySelectorAll('td');
Array.prototype.forEach.call(columns, function(column) {
var color = column.querySelector('font').getAttribute('color');
allPeriodsPerDay.push(colorToLuckMap[color]);
});
});
delete rootNode;
var key = [year, month, day].join('-');
periodsPerYear[key] = allPeriodsPerDay;
console.log(key + ' --> '+ allPeriodsPerDay);
callback();
});
};
$(function() {
var getWaitPeriod = function() {
return Math.floor(Math.random() * 10000);
};
var currentCount = 0;
daysOfYear.forEach(function(date) {
setTimeout(function() {
console.log(date.getFullYear(), date.getMonth() + 1, date.getDate());
getPeriodsInDay(date.getFullYear(), date.getMonth() + 1, date.getDate(), function() {
++currentCount;
if (currentCount === daysOfYear.length - 1) {
// all done here...
document.body.innerHTML += '<pre>' +
JSON.stringify(periodsPerYear).replace(/],/g, "],\n");
+ '</pre>';
}
});
}, getWaitPeriod());
});
});
</script>