-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathtinder.js
258 lines (231 loc) · 6.68 KB
/
tinder.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
var TINDER_HOST = "https://api.gotinder.com";
var request = require('request');
/**
* Constructs a new instance of the TinderClient class
*
* @constructor
* @this {TinderClient}
*/
function TinderClient() {
var xAuthToken = null;
var lastActivity = new Date();
var _this = this;
/**
* The current profile's user id
*/
this.userId = null;
/**
* Helper for getting the request object
* @param path {String} path the relative URI path
* @param data {Object} an object of extra values
*/
var getRequestOptions = function(path, data) {
var options = {
url: TINDER_HOST + "/" + path,
json: data
};
var headers = {
'User-Agent' : 'Tinder Android Version 2.2.3',
'os_version' : '16'
};
if (xAuthToken) {
headers['X-Auth-Token'] = xAuthToken;
}
options.headers = headers;
return options;
};
/**
* Issues a GET request to the tinder API
* @param {String} path the relative path
* @param {Object} data an object containing extra values
* @param {Function} callback the callback to invoke when the request completes
*/
var tinderGet = function(path, data, callback) {
var opts = getRequestOptions(path, data);
opts.method = 'GET';
request(opts, callback);
};
/**
* Issues a POST request to the tinder API
* @param {String} path the relative path
* @param {Object} data an object containing extra values
* @param {Function} callback the callback to invoke when the request completes
*/
var tinderPost = function(path, data, callback) {
var opts = getRequestOptions(path, data);
opts.method = 'POST';
request(opts, callback);
};
/**
* Helper for transforming the request callback values
* @param {Function} callback the callback
*/
var makeTinderCallback = function(callback) {
return function(error, res, body) {
var data = null;
if (!error) {
if (typeof body === "string")
{
try
{
data = JSON.parse(body);
} catch (err) {
// todo
}
}
else if (typeof body === "object") {
data = body;
}
}
if (callback) {
callback(error, data);
}
};
};
/**
* Gets a list of profiles nearby
* @param {Number} limit the maximum number of profiles to fetch
* @param {Function} callback the callback to invoke when the request completes
*/
this.getRecommendations = function(limit, callback) {
tinderGet('user/recs',
{
limit: limit
},
makeTinderCallback(callback));
};
/**
* Sends a message to a user
* @param {String} userId the id of the user
* @param {String} message the message to send
* @param {Function} callback the callback to invoke when the request completes
*/
this.sendMessage = function(userId, message, callback) {
tinderPost('user/matches/' + userId,
{
message: message
},
makeTinderCallback(callback));
};
/**
* Swipes left for a user
* @param {String} userId the id of the user
* @param {Function} callback the callback to invoke when the request completes
*/
this.pass = function(userId, callback) {
tinderGet('pass/' + userId,
null,
makeTinderCallback(callback));
};
/**
* Swipes right for a user
* @param {String} userId the id of the user
* @param {Function} callback the callback to invoke when the request completes
*/
this.like = function(userId, callback) {
tinderGet('like/' + userId,
null,
makeTinderCallback(callback));
};
/**
* Authorize this tinder client
* @param {String} fbToken the Facebook token. This will be obtained when authenticating the user
* @param {String} fbId the Facebook user id.
* @param {Function} callback the callback to invoke when the request completes
*/
this.authorize = function(fbToken, fbId, callback) {
tinderPost('auth',
{
facebook_token: fbToken,
facebook_id: fbId
},
function(error, res, body) {
if (!error && body.token) {
xAuthToken = body.token;
_this.userId = body.user._id;
_this.defaults = body;
callback(error, res, body);
} else if (body.error){
throw "Failed to authenticate: " + body.error
}
});
};
/**
* Returns whether this client is authorized
* @return whether or not this client is authorized
*/
this.isAuthorized = function() {
return xAuthToken != null;
}
/**
* Returns the xAuthToken
* @return xAuthToken
*/
this.getAuthToken = function() {
return xAuthToken || null;
}
/**
* Returns client information and globals
* Globals are used for interacting with tinder api limits
*/
this.getDefaults = function() {
return _this.defaults;
}
/**
* Gets a list of new updates. This will be things like new messages, people who liked you, etc.
* @param {Function} callback the callback to invoke when the request completes
*/
this.getUpdates = function(callback) {
tinderPost('updates',
{
last_activity_date: lastActivity.toISOString()
},
makeTinderCallback(function(err, data){
lastActivity = new Date(data.last_activity_date);
if (callback) {
callback(err, data);
}
}));
};
/**
* Gets the entire history for the user (all matches, messages, blocks, etc.)
*
* NOTE: Old messages seem to not be returned after a certain threshold. Not yet
* sure what exactly that timeout is. The official client seems to get this update
* once when the app is installed then cache the results and only rely on the
* incremental updates
* @param {Function} callback the callback to invoke when the request completes
*/
this.getHistory = function(callback) {
tinderPost('updates',
{
last_activity_date: ""
},
makeTinderCallback(callback));
};
/**
* Updates the position for this user
* @param {Number} lon the longitude
* @param {Number} lat the latitutde
* @param {Function} callback the callback to invoke when the request completes
*/
this.updatePosition = function(lon, lat, callback) {
tinderPost('user/ping',
{
lon: lon,
lat: lat
},
makeTinderCallback(callback));
};
/**
* Get user by id
* @param {String} userId the id of the user
* @param {Function} callback the callback to invoke when the request completes
*/
this.getUser = function(userId, callback){
tinderGet('user/' + userId,
null,
makeTinderCallback(callback));
};
}
exports.TinderClient = TinderClient;