-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (64 loc) · 1.73 KB
/
index.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
'use strict';
var request = require('request');
// setting 'jar' to true allows our lib to remember cookies. We need this to
// access parts of the site that need loging in.
request = request.defaults({jar: true});
var Filebit_API = (function() {
// URL where we login
var loginUrl = 'http://filebit.pl/panel/login';
// URL with FileBit api.
var finalUrl = 'http://filebit.pl/includes/ajax.php';
var login = function(login, password, callback) {
request({
url: loginUrl,
method:'POST',
form: {login: login, password: password}
},
function(error,response,body) {
// This is ugly but I have no idea how to solve it in smart way
// on 6:30AM after 14 hours of working
if (
error ||
response.body.indexOf('Podane hasło jest') > -1 ||
response.body.indexOf('Podany użytkownik nie istnieje') > -1
) {
error = error || new Error('Login error!');
callback(error);
return;
}
callback(null, true);
});
};
var getLink = function(link, callback) {
request({
url: finalUrl,
method:'POST',
form: {
a: 'serverNewFile',
url: link,
t: +(new Date())
}
},
function(error,response,body) {
if (error){
callback(error);
return;
}
try {
body = JSON.parse(body)[0];
if ('error' in body && parseInt(body.error, 10) !== 0) {
callback(new Error('Server error'));
} else {
callback(null, body.array.downloadStream);
}
} catch (e) {
callback(new Error('Cannot parse server answer'));
}
});
};
return {
login: login,
getLink: getLink
};
})();
module.exports = Filebit_API;