-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
209 lines (173 loc) · 4.75 KB
/
main.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
var form = document.container,
results = document.getElementById('results');
form.onsubmit = function (e) {
e.preventDefault();
this.implore.disabled = true;
empty(results);
conceptionConnection();
};
function conceptionConnection () {
var bday = new Date(Date.parse(form.date.value)),
cday = calcConceptionDate(
bday, Number(form.premature.value), Number(form.term.value));
console.log(cday);
announce('You were conceived at approx. ' + cday.toDateString());
doRequest(cday.getFullYear(), function (res) {
res = filterResults(res, cday);
output(res);
});
}
function calcConceptionDate (bday, offset, dir) {
var ret = new Date(bday.getTime());
ret.setDate(ret.getDate() - 280); //40(weeks) * 7(days/week) = 280(days)
ret.setDate(ret.getDate() + dir * offset)
return ret;
}
function filterResults (resp, date) {
var html = getActualResult(resp);
//give a leeway of a 8 days. why 8? I vaguely remember that's the accuarcy
// of the 40 weeks thing.
var minDate = new Date(date.getTime()),
maxDate = new Date(date.getTime());
minDate.setDate(date.getDate() - 8);
maxDate.setDate(date.getDate() + 8);
//now comes to tricky part: doing bastard parsing of the returned html
//we cheat by creating a container for that page
var root = document.createElement('body');
root.innerHTML = html; //forgive me...
var months = [
'january', 'february', 'march', 'april',
'may', 'june', 'july', 'august',
'september', 'october', 'november', 'december'
];
var min = {
month : months[minDate.getMonth()],
day : minDate.getDate()
};
var max = {
month : months[maxDate.getMonth()],
day : maxDate.getDate()
};
console.log(min, max);
//if they're on the same month, this eases things for us, since we don't
// have to select events from 2 distinct months
var comparer;
if (max.month === min.month) {
comparer = function (otherMonth, otherDay) {
return otherMonth === max.month && (
otherDay <= max.day && otherDay >= min.day );
};
}
else {
comparer = function (otherMonth, otherDay) {
return (
//max
(otherMonth === max.month && otherDay <= max.day)
||
//min
(otherMonth === min.month && otherDay >= min.day)
);
};
}
//we only look at events, not deaths/births
var stopNode = root.getElementsByTagName('h2')[1];
return getEvents(comparer);
function getEvents (comparer) {
var matches = [];
(function filterEvents (root) {
var node = root.firstElementChild;
for (; node; node = node.nextElementSibling) {
if (node === stopNode) {
return;
}
var tag = node.tagName;
if (tag === 'UL') {
filterEvents(node);
continue;
}
else if (tag !== 'LI') {
continue;
}
var parts = /(\w+)\s(\d+)/.exec(node.firstChild.data);
if (!parts) {
continue;
}
var otherMonth = parts[1].toLowerCase(),
otherDay = Number(parts[2]);
if (comparer(otherMonth, otherDay)) {
console.log(otherMonth, otherDay);
matches.push(node);
}
}
})(root);
//we need to flatten out the resulting elements, and we're done!
return flatten(matches);
}
function flatten (lis) {
return [].reduce.call(lis, extract, []);
}
function extract (ret, li) {
console.log(li);
if (li.children.length) {
ret.push.apply(ret, flatten(li.getElementsByTagName('li')));
}
else {
var data = li.firstChild.data;
//October 4 – ...
data = data
.replace(/^\w+\s+\d+\s+\u2013/, '')
.trim();
console.log(data);
ret.push(data);
}
return ret;
}
//PHEW
}
function getActualResult (resp) {
var query = resp.query,
pageid = query.pageids[0];
return query.pages[pageid].extract;
}
function doRequest (year, cb) {
//see the wikipedia API page: https://www.mediawiki.org/wiki/API
var url = 'http://en.wikipedia.org/w/api.php?format=json&callback=API_REQUEST_DONE&action=query&prop=extracts&indexpageids&titles=' + encodeURIComponent(year);
var script = document.createElement('script');
script.src = url;
window.API_REQUEST_DONE = function (resp) {
//cleanup on aisle 4
delete window.API_REQUEST_DONE;
script.parentNode.removeChild(script);
cb(resp);
};
document.head.appendChild(script);
}
function output (res) {
var events = document.createElement('ul'),
text;
if (res.length) {
text = 'Most likely historical events that aroused your parents:';
}
else {
text = 'Nothing historic aroused your parents';
}
announce(text);
form.implore.disabled = false;
res.forEach(outputLi);
results.appendChild(events);
function outputLi (ev) {
var li = document.createElement('li');
li.textContent = ev;
events.appendChild(li);
}
}
function announce (msg) {
var cont = document.createElement('div');
cont.textContent = msg;
results.appendChild(cont);
}
function empty (el) {
while (el.firstChild) {
el.removeChild(el.firstChild);
}
}