-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
156 lines (133 loc) · 4.22 KB
/
app.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
/**
* Module dependencies.
*/
var express = require('express');
var app = module.exports = express.createServer();
var http = require('http');
var url = require('url');
// Configuration
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.cache = {screen_names: new Cache('screen_names')};
app.set("view options", {layout: false});
app.register('.html', {
compile: function (str, options) {
return function (locals) {
return str.replace(/<\!--%\s*(\w+)\s*%-->/g, function (tag, key, index, full) {
return locals[key] || '';
});
};
}
});
});
app.configure('development', function () {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function () {
app.use(express.errorHandler());
});
// Routes
app.get('/', function (req, res) {
var parsed = url.parse(req.url);
var query = getQueryStringArgs(parsed.query) || {};
var screen_name = query.screen_name;
if (screen_name) {
getTwitterInfo(screen_name, function (err, twitter) {
// TODO: give *some* indication to user about the issue
if (err) console.log("Got error: " + e.message);
res.render('index.html', twitter);
});
}
else {
res.render('index.html', query);
}
});
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
function getQueryStringArgs(query)
{
var obj = {};
if (query) {
query.split('&').forEach(function (pair) {
var arr = pair.split('=');
obj[arr[0]] = arr[1];
});
}
return obj;
}
function getTwitterInfo(screen_name, cb)
{
var cache = app.cache.screen_names;
var twitter = cache.get(screen_name);
if (twitter) {
cb(null, twitter);
}
else {
console.log('hit twitter for', screen_name)
var options = {
host: 'twitter.com',
path: '/users/show.json?screen_name=' + screen_name
}
var calls_left;
http
.get(options, function (response) {
calls_left = parseInt(response.headers['x-ratelimit-remaining'], 10);
console.log(calls_left, 'calls left');
var json = '';
response.on('data', function (data) { json += data });
response.on('end', function () {
var twitter;
try {
twitter = JSON.parse(json);
}
catch (ex) {}
var args = twitter;
args.user_id = args.id;
args.score = getScore(args.id);
args.flag_position = getFlagPosition(args.created_at);
app.cache.screen_names.set(screen_name, args)
cb(null, args)
})
})
.on('error', function (err) { cb(err) })
}
}
function getScore(user_id)
{
var your_place = parseInt(user_id, 10);
var total_users = 400000000;
var score = 100 * (1 - ( your_place / total_users ));
return parseInt(score, 10);
}
function getFlagPosition(account_created)
{
const DAYS_MS = (1000 * 60 * 60 * 24);
var twitter_started = new Date('3/1/06');
var diff_ms = new Date(account_created) - twitter_started;
var left_margin = 86;
var chart_width = 960 - left_margin;
var total_days = (new Date() - twitter_started) / DAYS_MS;
var per_day = chart_width / total_days;
var days = diff_ms / DAYS_MS;
return (days * per_day) + left_margin;
}
function Cache(name)
{
var RedisStore = require('connect-redis')(express);
this._store = {};
}
Cache.prototype.key = function (str) {
return str.toLowerCase();
}
Cache.prototype.get = function (key) {
return this._store[ this.key(key) ];
}
Cache.prototype.set = function (key, value) {
return this._store[ this.key(key) ] = value;
}