-
Notifications
You must be signed in to change notification settings - Fork 33
/
tinderbot.js
148 lines (120 loc) · 4.05 KB
/
tinderbot.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
var express = require('express');
var bodyParser = require('body-parser');
var tinder = require('tinderjs').TinderClient;
var request = require('request');
/**
* Initializes a new instance of the TinderBot class
*/
function TinderBot() {
/**
* When the current facebook token expires
*/
var fbTokenExpiresIn = null;
/**
* The current express server associated with this bot
*/
var app = express();
var _this = this;
var server = null;
/**
* The amount of time, in milliseconds, between executing the mainLoop function
*/
this.mainLoopInterval = 5000;
/**
* The function to be executed repeatedly after each timeout
*/
this.mainLoop = function() { };
/**
* The Facebook app ID from which user tokens will be generated
*/
this.FBClientId = null;
/**
* The Facebook app secret
*/
this.FBClientSecret = null;
/**
* The port on which the express server will listen on
*/
this.port = "8080";
/**
* The client you can use to issue requests to tinder
*/
this.client = new tinder();
/**
* Starts the express server
*/
this.live = function() {
server = app.listen(this.port);
};
/**
* Closes the express server
*/
this.die = function(){
if (server) {
server.close();
}
};
app.use(bodyParser());
app.set('views', __dirname + '/views');
app.set('view options', { layout: false });
app.set('view engine', 'jade');
/**
* Renders the page for when a user has been authenticated by Facebook
*/
app.get('/fbtoken', function(req, res){
res.render("auth");
});
/**
* Since the Facebook access_token is only available in the browser URL fragment,
* it is not available server-side so we need to post it to the server from the browser.
* This is the entry point for persisting that token
*/
app.post('/fbtoken', function(req, res){
var hash = req.body.hash;
var tokenField = "access_token=";
var expiresField = "&expires_in=";
var access_token = hash.substring(hash.indexOf(tokenField) + tokenField.length, hash.indexOf(expiresField));
var expiryInSeconds = hash.substring(hash.indexOf(expiresField) + expiresField.length);
fbTokenExpiresIn = new Date(new Date().getTime() + expiryInSeconds * 1000);
// Once we have the Facebook access token, we need the user ID associated with the token.
request({
url: 'https://graph.facebook.com/debug_token?input_token=' + access_token + '&access_token=' + _this.FBClientId + '|' + _this.FBClientSecret,
method: 'GET'
}, function(err, response, body){
if (err) {
throw new "Failed to get user id: " + err;
} else {
body = JSON.parse(body);
if (!body.data.user_id) {
throw new "Failed to get user id.";
}
// Once we have the Facebook access token and user id, we can use it to authorize our bot
// to start issuing requests to the Tinder API
_this.client.authorize(access_token, body.data.user_id, function(){
var timer = setInterval(function(){
if (new Date().getTime() >= fbTokenExpiresIn.getTime()) {
clearInterval(timer);
// Have the client refresh their screen to obtain
// a new Facebook client token
res.json({
redirect: '/login'
});
} else {
if (_this.mainLoop) {
_this.mainLoop();
}
}
}, _this.mainLoopInterval);
});
}
});
});
/**
* Entry point for getting the Facebook access token. This will bring you to a Facebook auth
* dialogue and eventually grant your access token
*/
app.get('/login', function(req, res){
res.redirect('https://www.facebook.com/dialog/oauth?client_id=' + _this.FBClientId + '&response_type=token&redirect_uri=http://localhost:' + _this.port + '/fbtoken');
});
}
module.exports = TinderBot;