-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsupport.js
175 lines (149 loc) · 4.75 KB
/
support.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
var Twit = require('twit');
var Q = require('q');
var TUser = require('./models/TwitterUser');
var cypherQueries = require('./cypher/queries');
var config = require('./config');
var neo4j = require('neo4j');
var db = new neo4j.GraphDatabase(config.neo4j.dbPath || 'http://localhost:7474');
var T = new Twit(config.twitterAuth);
var connector = module.exports;
var getRateLimitInfo = function(resp) {
if(!resp || !resp.headers) {
// This should never happen
return {
remainingRequests: 0,
resetMs: Date.now() + (5 * 60 * 1000)
};
}
return {
remainingRequests: resp.headers['x-rate-limit-remaining'],
resetMs: resp.headers['x-rate-limit-reset'] * 1000 + 1000 // add a 1 second buffer
};
};
var scheduleNextCall = function(rateLimitInfo, nextMethod, optParam){
if(rateLimitInfo.remainingRequests === "0") {
logger('Rate Limit Exceeded, waiting until '+ (new Date(rateLimitInfo.resetMs)));
return setTimeout(nextMethod, rateLimitInfo.resetMs - Date.now(), optParam);
} else {
return nextMethod();
}
};
var logger = function(msg) {
console.log(new Date() + ' - ' + msg);
};
/**
* opts is an object that must have the following keys
* - screen_name
* - url
* - cypherQuery
* - onSuccess
* - onError
**/
var createBatchMethod = function(opts) {
var next_cursor = -1;
return function batchMethod(){
logger('Starting HTTP request');
T.get(opts.url, {screen_name: opts.screenName, count: 200, cursor: next_cursor, skip_status: true}, function(err, reply, resp){
var rateLimitInfo = getRateLimitInfo(resp), userList;
if(err) {
if(err.code == 88) {
scheduleNextCall(rateLimitInfo, batchMethod);
} else {
opts.onError(err);
}
} else {
userList = reply.users.map(TUser.staticToNeoData);
logger(opts.url + ' request completed, ' + rateLimitInfo.remainingRequests + ' requests remaining before rate limiting');
logger('Starting DB Query');
db.query(opts.cypherQuery, {userList: userList, screenName: opts.screenName}, function(err, arr){
logger('DB request completed');
if(err) {
opts.onError(err);
} else if(reply.next_cursor) {
next_cursor = reply.next_cursor;
scheduleNextCall(rateLimitInfo, batchMethod);
} else {
opts.onSuccess();
}
});
}
});
};
};
connector.getUserFromTwitter = function(handle) {
var response = Q.defer();
T.get('users/lookup', {screen_name: handle}, function(err, reply){
if(err) {
return response.reject(err);
}
response.resolve(new TUser(reply[0]));
});
return response.promise;
};
connector.cypherUpsertOne = function(user) {
var response = new Q.defer();
var userMap = user.toNeoData();
db.query(cypherQueries.upsertOne, {userMap: userMap, screenName: userMap.screen_name},
function(err, arr){
if(err) {
response.reject(err);
} else {
response.resolve(userMap.screen_name);
}
});
return response.promise;
};
connector.cypherGetUnconnectedUsersForHandle = function(handle) {
var response = new Q.defer();
db.query(cypherQueries.getUnconnectedUsers, {screenName: handle},
function(err, arr){
if(err) {
response.reject(err);
} else {
response.resolve(arr.map(function(one){ return one.sn; }));
}
});
return response.promise;
};
connector.getWebForHandle = function(screen_name){
var followerResponse = new Q.defer();
var friendResponse = new Q.defer();
logger('getting web for ' + screen_name);
var getFollowersBatch = createBatchMethod({
screenName: screen_name,
url: 'followers/list',
cypherQuery: cypherQueries.upsertManyAndFollows,
onSuccess: followerResponse.resolve,
onError: followerResponse.reject
});
var getFriendsBatch = createBatchMethod({
screenName: screen_name,
url: 'friends/list',
cypherQuery: cypherQueries.upsertManyAndFriends,
onSuccess: friendResponse.resolve,
onError: friendResponse.reject
});
getFollowersBatch();
getFriendsBatch();
return Q.all([friendResponse.promise, followerResponse.promise]);
};
connector.getWebForHandles = function(list) {
if(!list || !list.length || list.length === 0) {
throw new Error('Must supply a list of handles');
}
var idx = 0;
function getNext() {
var targetHandle = list[idx];
if(targetHandle) {
console.log('Getting web for ' + targetHandle);
connector.getWebForHandle(targetHandle).then( function(counts) {
console.log(targetHandle + ' completed.');
idx++;
if(list[idx]) getNext();
else console.log('List completed.');
}, logger);
}
}
//Kick it off
getNext();
};