-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacebook.js
executable file
·95 lines (84 loc) · 1.9 KB
/
facebook.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
var request = require('request');
var fs = require("fs");
var Q = require("q");
var RateLimiter = require('limiter').RateLimiter;
var limiter = new RateLimiter(600,600000);
exports.endpoint = '';
exports.token = '';
exports.get= function(url) {
var deferred = Q.defer();
var getURL=exports.endpoint + url + "?&access_token=" + exports.token;
limiter.removeTokens(1, function() {
request.get({url:getURL, json:true}, function (err, response, body) {
if(err)
{
hasError(err,deferred);
}
else
{
deferred.resolve(body);
}
});
});
return deferred.promise;
};
exports.post= function(url, form) {
var deferred = Q.defer();
var postURL=exports.endpoint + url + "?&access_token=" + exports.token;
limiter.removeTokens(1, function() {
request({method: 'POST', url:postURL, form:form, json:true}, function (err, response, body) {
if(err)
{
hasError(err,deferred);
}
else
{
deferred.resolve(body);
}
});
});
return deferred.promise;
};
exports.put= function(url, form) {
var deferred = Q.defer();
var putURL=exports.endpoint + url + "?&access_token=" + exports.token;
limiter.removeTokens(1, function() {
request({method: 'PUT', url:putURL, form:form, json:true}, function (err, response, body) {
if(err)
{
hasError(err,deferred);
}
else
{
deferred.resolve(body);
}
});
});
return deferred.promise;
};
exports.delete= function(url) {
var deferred = Q.defer();
var delURL=exports.endpoint + url + "?&access_token=" + exports.token;
limiter.removeTokens(1, function() {
request.del(delURL, function (err, response, body) {
if(err)
{
hasError(err,deferred);
}
else
{
deferred.resolve(body);
}
});
});
return deferred.promise;
};
function hasError(err, deferred) {
fs.appendFileSync(
"request_log.txt",
new Date().toUTCString()
+ " ----- "
+ JSON.stringify(err) + "\n"
);
deferred.reject(err);
};