-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquotes.js
197 lines (185 loc) · 6.01 KB
/
quotes.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
'use strict';
var request = require('request'),
async = require('async');
var error = {
url: '',
avatar: '/avatar/ServerCooties',
author: 'ServerCooties',
body: '<p>+++Error At Address: 14, Treacle Mine Road, Ankh-Morpork+++' +
'<br/>+++Divide By Cucumber Error. Please Reinstall Universe And' +
' Reboot +++</p>',
name: '',
authorTitle: ''
},
avatars = {},
definitions = [error],
users = {},
host = 'http://what.thedailywtf.com';
function getAvatarPath(username, callback) {
request(host + '/users/' + username + '.json', function (err, _, body) {
var letter = '/letter_avatar/' + username +
'/45/3_af0dc75e546be11ab6c09c8b9d61c787.png';
if (err) {
return callback(null, letter);
}
try {
body = JSON.parse(body);
if (!body.user.avatar_template) {
return callback(null, letter);
}
callback(null, body.user.avatar_template.replace('{size}', '45'));
} catch (__) {
callback(null, letter);
}
});
}
function getAvatar(username, callback) {
getAvatarPath(username, function (_, path) {
var req = request(host + path),
parts = [],
res;
req.on('error', callback);
req.on('data', function (d) {
parts.push(d);
});
req.on('response', function (resp) {
res = resp;
});
req.on('end', function () {
callback(null, {
retrievedAt: Date.now(),
contentType: res.headers['content-type'],
lastModified: res.headers['last-modified'],
data: Buffer.concat(parts)
});
});
});
}
function serve(avatar, response) {
response.writeHead(200, {
'Content-Type': avatar.contentType,
'Last-Modified': avatar.lastModified
});
response.write(avatar.data, 'binary');
response.end();
}
exports.serveAvatar = function serveAvatar(uri, _, response) {
var username = /[/]([^/]+)$/.exec(uri)[1];
if (avatars[username]) {
return serve(avatars[username], response);
}
getAvatar(username, function (err, avatar) {
if (err) {
response.writeHead(500);
response.write(err, 'binary');
return response.end();
}
avatars[username] = avatar;
serve(avatar, response);
});
};
function getPosts(id, complete) {
var base = '/t/' + id + '/posts.json?include_raw=1';
request(host + base + '&post_ids=0', function (err, _, topic) {
var posts, results = [];
try {
posts = JSON.parse(topic).post_stream.stream;
} catch (e) {
err = e;
}
if (err) {
return complete([]);
}
async.whilst(function () {
return posts.length > 0;
}, function (next) {
var part = [];
while (part.length < 100 && posts.length > 0) {
part.push(posts.shift());
}
part = part.join('&post_ids[]=');
request(host + base + '&post_ids[]=' + part,
function (err2, resp, defs) {
if (err2 || resp.statusCode !== 200) {
return next();
}
try {
defs = JSON.parse(defs).post_stream.posts;
} catch (e) {
return next(e);
}
results = results.concat(defs.filter(function (f) {
return f.wiki && f.post_number > 1;
}).map(function (post) {
return {
url: host + '/p/' + post.id,
avatar: '/avatar/' + post.username,
author: post.username,
body: post.cooked,
name: post.name,
authorTitle: post.user_title
};
}));
setTimeout(next, 10 * 1000);
});
}, function () {
complete(results);
});
});
}
function loadDefinitions(callback) {
getPosts(3866, function (defs) {
definitions = defs || [error];
var usr = definitions.map(function (d) {
return d.author;
});
usr.push('ServerCooties');
users = {};
usr.forEach(function (u) {
users[u] = users[u] ? users[u] + 1 : 1;
});
callback();
});
}
exports.getQuote = function getQuote() {
var def = definitions[Math.floor(Math.random() * definitions.length)];
def.body = def.body.replace(/(src|href)="([^"]+)"/g,
function (_, prefix, value) {
if (value.substr(0, 2) === '//') {
value = 'http:' + value;
}
if (value[0] === '/') {
value = host + value;
}
return prefix + '="' + value + '"';
});
return def;
};
async.forever(function (next) {
var refresh = 24 * 60 * 60 * 1000;
loadDefinitions(function () {
console.log('quotes loaded'); //eslint-disable-line no-console
var now = Date.now() - refresh;
async.each(Object.keys(users), function (user, innerNext) {
if (!avatars[user] || avatars[user].retrievedAt < now) {
return getAvatar(user, function (err, avatar) {
if (!err) {
avatars[user] = avatar;
}
innerNext();
});
}
innerNext();
});
setTimeout(next, refresh);
});
});
async.forever(function (next) {
var cutoff = Date.now() - 10 * 60 * 1000;
Object.keys(avatars).forEach(function (key) {
if (!users[key] && avatars[key].retrievedAt < cutoff) {
delete users[key];
}
});
setTimeout(next, 60 * 1000);
});