-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.js
134 lines (134 loc) · 4.47 KB
/
user.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
(function() {
var Link, Logger, Twitter, chain, crypto, mongo, sys;
require.paths.unshift('./vendor');
sys = require('sys');
Logger = require('./log').Logger;
crypto = require('crypto');
Twitter = require('./twitter');
mongo = require('./mongo');
Link = require('./link').Link;
chain = require('./vendor/.npm/chain-gang/active/package/lib').create({
workers: (parseInt(process.env.WORKERS) || 5)
});
mongo.mongoose.model('User', {
properties: [
'last_fetched', 'since_id', 'id', 'name', 'screen_name', 'key', {
access: ['token', 'secret']
}
],
indexes: ['id', 'key'],
cast: {
id: Number
},
static: {
findById: function(id, callback) {
return this.find({
'id': id
}).first(callback);
},
fetchOutdated: function(since, callback) {
var self;
self = this;
return self.find({
'last_fetched': {
'$lte': since
}
}).sort([['last_fetched', 1]]).limit(parseInt(process.env.CONCURRENT_FETCHES) || 20).all(function(stale) {
return self.find().where('last_fetched', null).all(function(virgin) {
var _a, _b, _c, user, users;
users = stale.concat(virgin);
_b = users;
for (_a = 0, _c = _b.length; _a < _c; _a++) {
user = _b[_a];
user.fetch();
}
return callback();
});
});
},
fetch: function(id) {
return this.findById(id, function(user) {
return user.fetch();
});
}
},
getters: {
client: function() {
return new Twitter.client(this.access.token, this.access.secret);
}
},
methods: {
save: function(callback) {
this.key = crypto.createHash('sha1').update(("--" + (this._id) + "--url-hash")).digest('hex');
return this.__super__(callback);
},
links: function(callback) {
return Link.find().where('user_id', this.id).sort([['status.created_at', -1]]).all(function(arr) {
return callback(arr);
});
},
job: function(timeout, link) {
Logger.debug('Worker', ("Starting job on " + (link.url)));
return function(worker) {
return link.fetchContent(function() {
return worker.finish();
});
};
},
fetch: function(callback) {
var count, path, self;
if (this.since_id) {
count = 200;
} else {
count = 30;
}
path = ("/statuses/home_timeline.json?include_entities=true&count=" + (count));
if (this.since_id) {
path += ("&since_id=" + (this.since_id));
}
self = this;
return this.client.get(path, function(statuses) {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, link, links, status, url;
Logger.debug("User", ("" + (statuses.length) + " statuses to parse"));
self.last_fetched = new Date();
if (statuses[0]) {
self.since_id = statuses[0].id;
}
self.save();
_a = []; _c = statuses;
for (_b = 0, _d = _c.length; _b < _d; _b++) {
status = _c[_b];
_a.push((function() {
if (status.entities.urls) {
try {
_e = []; _g = status.entities.urls;
for (_f = 0, _h = _g.length; _f < _h; _f++) {
url = _g[_f];
_e.push((function() {
Logger.debug("Link", ("Creating '" + (url.url) + "' from status '" + (status.id) + "'"));
links = Link.fromStatus(self, status);
_i = []; _k = links;
for (_j = 0, _l = _k.length; _j < _l; _j++) {
link = _k[_j];
_i.push((function() {
Logger.debug("User", ("Adding job to retrieve url " + (link.url)));
return chain.add(self.job(("" + (parseInt(process.env.WORKER_TIMEOUT) || 10)), link), ("" + (link._id)));
})());
}
return _i;
})());
}
return _e;
} catch (error) {
return Logger.err("User", "CAUGHT YOU BITCH");
}
}
})());
}
return _a;
});
}
}
});
exports.User = mongo.db.model('User');
})();