forked from alkawryk/tinderjs
-
Notifications
You must be signed in to change notification settings - Fork 17
/
tinder.js
604 lines (549 loc) · 16.8 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
var TINDER_HOST = "https://api.gotinder.com/";
var TINDER_IMAGE_HOST = "https://imageupload.gotinder.com/";
var request = require('superagent');
/**
* Constructs a new instance of the TinderClient class
*
* @constructor
* @this {TinderClient}
*/
function TinderClient() {
var xAuthToken = null;
this.lastActivity = new Date();
var _this = this;
/**
* The current account's user id
*/
this.userId = null;
/**
* Helper for getting the request headers
*/
var getRequestHeaders = function() {
var headers = {
'User-Agent' : 'Tinder Android Version 4.5.5',
'os_version' : '23',
'platform' : 'android',
'app-version' : '854',
'Accept-Language' : 'en'
};
if (xAuthToken) {
headers['X-Auth-Token'] = xAuthToken;
}
return headers;
};
/**
* 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) {
request.get(TINDER_HOST + path)
.set(getRequestHeaders())
.end(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) {
request.post(TINDER_HOST + path)
.set(getRequestHeaders())
.send(data)
.end(callback);
};
/**
* Issues a PUT 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 tinderPut = function(path, data, callback) {
request.put(TINDER_HOST + path)
.set(getRequestHeaders())
.send(data)
.end(callback);
};
/**
* Issues a DELETE 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 tinderDelete = function(path, data, callback) {
request.del(TINDER_HOST + path)
.set(getRequestHeaders())
.send(data)
.end(callback);
};
/**
* Helper for transforming the request callback values
* @param {Function} callback the callback
*/
var makeTinderCallback = function(callback) {
return function(error, res) {
var data = null;
if (!error) {
if (typeof res.body === "string")
{
try
{
data = JSON.parse(res.body);
} catch (err) {
error = data;
}
}
else if (typeof res.body === "object") {
data = res.body;
}
}
if (data && data.status && data.status !== 200) {
error = data;
}
if (callback) {
callback(error, data);
}
};
};
/**
* 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,
locale: 'en'
},
function(error, res) {
// If no body is passed back, return an error
if(res.body === undefined){
throw new Error('No token passed back from Tinder');
}
var body = res.body || { 'token': null };
if (!error && body.token) {
xAuthToken = body.token;
_this.userId = body.user._id;
_this.defaults = body;
callback = makeTinderCallback(callback);
callback(error, res);
} else if (body.error){
throw new Error("Failed to authenticate: " + body.error);
} else {
callback(error, res);
}
});
};
/**
* Set auth token if you have it saved, no need to do fb login every time
*/
this.setAuthToken = function(token) {
xAuthToken = token;
};
this.getAuthToken = function() {
return xAuthToken;
};
/**
* 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 nearby users
* @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} matchId the id of the match
* @param {String} message the message to send
* @param {Function} callback the callback to invoke when the request completes
*/
this.sendMessage = function(matchId, message, callback) {
tinderPost('user/matches/' + matchId,
{
message: message
},
makeTinderCallback(callback));
};
/**
* Likes (swipes right) on 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));
};
/**
* Superlikes a user
* @param {String} userId the id of the user
* @param {Function} callback the callback to invoke when the request completes
*/
this.superLike = function(userId, callback) {
tinderPost('like/' + userId + '/super',
null,
makeTinderCallback(callback));
};
/**
* Passes (swipes left) on 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));
};
/**
* Unmatch with a user
* @param {String} matchID the id of the match
* @param {Function} callback the callback to invoke when the request completes
*/
this.unmatch = function(matchId, callback) {
tinderDelete('user/matches/' + matchId,
null,
makeTinderCallback(callback));
};
/**
* Gets a list of new updates. This will be things like new messages, users who liked you, etc.
* @param {Function} callback the callback to invoke when the request completes
*/
this.getUpdates = function(callback) {
tinderPost('updates',
{
last_activity_date: _this.lastActivity.toISOString()
},
makeTinderCallback(function(err, data){
if (data && data.last_activity_date) {
_this.lastActivity = new Date(data.last_activity_date);
}
if (callback) {
callback(err, data);
}
}));
};
/**
* Gets the entire history for the current account (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 geographical position for the current account
* @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));
};
/**
* Gets the current account info
* @param {Function} callback the callback to invoke when the request completes
*/
this.getAccount = function(callback) {
tinderGet('meta',
null,
makeTinderCallback(callback));
};
/**
* Update your gender
* @param {Number} gender is your gender (0 = Male, 1 = Female)
* @param {Function} callback the callback to invoke when the request completes
*/
this.updateGender = function(gender, callback) {
tinderPost('profile',
{
gender: gender
},
makeTinderCallback(callback));
};
/**
* Update your bio
* @param {String} bio is you bio (500 characters max.)
* @param {Function} callback the callback to invoke when the request completes
*/
this.updateBio = function(bio, callback) {
tinderPost('profile',
{
bio: bio
},
makeTinderCallback(callback));
};
/**
* Update your job
* @param {String} id is the facebook id of the job
* @param {Function} callback the callback to invoke when the request completes
*/
this.updateJob = function(id, callback) {
tinderPut('profile/job',
{
"company": {
id: id
}
},
makeTinderCallback(callback));
};
/**
* Delete your current job
* @param {Function} callback the callback to invoke when the request completes
*/
this.deleteJob = function(callback) {
tinderDelete('profile/job',
null,
makeTinderCallback(callback));
};
/**
* Update your school
* @param {String} id is the facebook id of the school
* @param {Function} callback the callback to invoke when the request completes
*/
this.updateSchool = function(id, callback) {
tinderPut('profile/school',
{
schools: [{
id: id
}]
},
makeTinderCallback(callback));
};
/**
* Delete your current school
* @param {Function} callback the callback to invoke when the request completes
*/
this.deleteSchool = function(callback) {
tinderDelete('profile/school',
null,
makeTinderCallback(callback));
};
/**
* Updates the preferences for the current account
* @param {Boolean} discovery whether or not to show user's card
* @param {Number} ageMin the minimum age to show recommendations
* @param {Number} ageMax the maximum age to show recommendations
* @param {Number} gender the gender to show recommentations (0 = Male, 1 = Female, -1 = Both)
* @param {Number} distance the distance in miles to show recommendations
* @param {Function} callback the callback to invoke when the request completes
*/
this.updatePreferences = function(discovery, ageMin, ageMax, gender, distance, callback) {
tinderPost('profile',
{
discoverable: discovery,
age_filter_min: ageMin,
age_filter_max: ageMax,
gender_filter: gender,
distance_filter: distance
},
makeTinderCallback(callback));
};
/**
* Delete the current account
* @param {Function} callback the callback to invoke when the request completes
*/
this.deleteAccount = function(callback) {
tinderDelete('profile',
null,
makeTinderCallback(callback));
};
/**
* Gets a 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));
};
/**
* Upload a new picture to the current account
* @param {Buffer} file the picture that you want to upload
* @param {Function} callback the callback to invoke when the request completes
*/
this.uploadPicture = function(file, callback) {
request.post(TINDER_IMAGE_HOST + 'image?client_photo_id=ProfilePhoto' +
new Date().getTime())
.set(getRequestHeaders())
.field('userId', _this.userId)
.attach('file', file)
.end(makeTinderCallback(callback));
};
/**
* Post a new picture to the current account from Facebook
* @param {String} pictureId is the facebook id of the picture
* @param {Float} xdistance_percent is the zoom percentage in x 0 full Zoom 1 no Zoom
* @param {Float} ydistance_percent is the zoom percentage in x 0 full Zoom 1 no Zoom
* @param {Float} xoffset_percent is the offset from the left corner in percentage
* @param {Float} yoffset_percent is the offset from the top corner in percentage
* @param {Function} callback the callback to invoke when the request completes
*/
this.uploadFBPicture = function(pictureId, xdistance_percent, ydistance_percent, xoffset_percent, yoffset_percent, callback) {
tinderPost('media',
{
transmit: "fb",
assets: [{
ydistance_percent: ydistance_percent,
id: pictureId,
xoffset_percent: xoffset_percent,
yoffset_percent: yoffset_percent,
xdistance_percent: xdistance_percent
}]
},
makeTinderCallback(callback));
};
/**
* Delete a picture from the current account
* @param {String} pictureId the id of the picture
* @param {Function} callback the callback to invoke when the request completes
*/
this.deletePicture = function(pictureId, callback) {
tinderDelete('media',
{
assets: [pictureId]
},
makeTinderCallback(callback));
};
/**
* Get a share URL for a user
*
* @param {String} userId the id of the user
* @param {Function} callback the callback to invoke when the request completes
*/
this.getShareLink = function(userId, callback) {
tinderPost('user/' + userId + '/share',
null,
makeTinderCallback(callback));
};
/**
* Report a user
*
* @param {String} userId the id of the user
* @param {Number} causeId one of 4 (inappropriate photos), 1 (spam), or 0 (other)
* @param {String} causeText optional reason for report when causeId is 0 (other)
* @param {Function} callback the callback to invoke when the request completes
*/
this.report = function(userId, causeId, causeText, callback) {
var data = {
cause: causeId
};
if (causeId === 0 && causeText !== null) data.text = causeText;
tinderPost('report/' + userId,
data,
makeTinderCallback(callback));
};
/**
* Create a web username for the current account
*
* @param {String} userName the username to request be created
* @param {Function} callback the callback to invoke when the request completes
*/
this.createUsername = function(username, callback) {
tinderPost('profile/username',
{
username: username
},
makeTinderCallback(callback));
};
/**
* Change a web username for the current account if it's already been set
*
* @param {String} userName the username to request be created
* @param {Function} callback the callback to invoke when the request completes
*/
this.changeUsername = function(username, callback) {
tinderPut('profile/username',
{
username: username
},
makeTinderCallback(callback));
};
/**
* Deletes the existing web username for the current account
*
* @param {Function} callback the callback to invoke when the request completes
*/
this.deleteUsername = function(callback) {
tinderDelete('profile/username',
null,
makeTinderCallback(callback));
};
///////////// TINDER PLUS /////////////////
/**
* Update the passport location
* @param {Number} lon the longitude
* @param {Number} lat the latitutde
* @param {Function} callback the callback to invoke when the request completes
*/
this.updatePassport = function(lat, lon, callback) {
tinderPost('/passport/user/travel',
{
lon: lon,
lat: lat
},
makeTinderCallback(callback));
};
/**
* Reset the passport location
* @param {Function} callback the callback to invoke when the request completes
*/
this.resetPassport = function(callback) {
tinderPost('/passport/user/reset',
null,
makeTinderCallback(callback));
};
/**
* @deprecated
* Get authenticated user info
* @param {Function} callback the callback to invoke when the request completes
*/
this.getProfile = function(callback) {
console.log('This function is deprecated. Use getAccount(callback) instead.');
return this.getAccount(callback);
};
}
exports.TinderClient = TinderClient;