-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
157 lines (134 loc) · 3.42 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
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
var http = require('follow-redirects').http;
var test = require('net');
var runnel = require('runnel');
var fs = require('fs');
var downloader = {};
var host = 'www.youtube-mp3.org';
var pushItemLink = null;
var itemInfoLink = null;
var pingLink = 'http://ping.aclst.com/ping.php/12790944/{0}';
var downloadLink = '/get?ab=256&video_id={0}&h={1}&r={2}';
var filename = null;
var _cc = function(a) {
var __AM = 65521;
if ("string" != typeof a) throw Error("se");
var c = 1,
b = 0,
d, e;
for (e = 0; e < a.length; e++) d = a.charCodeAt(e), c = (c + d) % __AM, b = (b + c) % __AM;
return b << 16 | c
}
/**
* Push the item to their API
**/
var pushItem = function(cb) {
http.get({
host: host,
schema: 'http',
path: pushItemLink
}, function(res) {
res.setEncoding('utf8');
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
return cb(null, body);
});
});
}
/**
* Get the item info link
**/
var getItemInfo = function(token, cb) {
http.get({
host: host,
schema: 'http',
path: itemInfoLink.replace('{0}', token)
}, function(res) {
res.setEncoding('utf8');
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
cb(null, token, body);
});
});
}
/**
* Ping the item on the aclst.com service
**/
var pingItem = function(token, reqBody, cb) {
if(reqBody.indexOf('info') !== 0) return cb('Fail from api: ' + reqBody);
eval(reqBody);
http.get(info.pf, function(res) {
cb(null, token, info);
}).on('error', function(e) {
return cb(e.message);
});
}
/**
* Download the file
**/
var downloadFile = function(token, info, cb) {
var timestamp = (new Date).getTime();
var options = {
path: downloadLink.replace('{0}', token).
replace('{1}', info.h).
replace('{2}', timestamp + '.' + _cc(token + timestamp)),
host: host,
agent: false
};
var req = http.get(options, function(res) {
var writeFile = fs.createWriteStream(filename, {'flags': 'a'});
res.on('data', function(chunk) {
writeFile.write(chunk, encoding='binary');
});
res.on('end', function() {
writeFile.end();
cb(null);
});
}).on('error', function(e) {
cb(e);
});
}
var getDownloadLink = function(token, info, cb) {
var timestamp = (new Date).getTime();
var url = host + downloadLink.replace('{0}', token).
replace('{1}', info.h).
replace('{2}', timestamp + '.' + _cc(token + timestamp));
cb(null, url);
}
downloader.getDownloadLink = function(youtubeLink, finalCallback) {
pushItemLink = '/a/pushItem' +
'/?item=' + encodeURI(youtubeLink) +
'&el=na&bf=false';
itemInfoLink = '/a/itemInfo' +
'/?video_id={0}&ac=www&t=grp';
runnel(
pushItem,
getItemInfo,
pingItem.bind(this),
getDownloadLink.bind(this),
function done(err, url) {
finalCallback(err, url);
});
}
downloader.download = function(youtubeLink, file, finalCallback) {
filename = file;
pushItemLink = '/a/pushItem' +
'/?item=' + encodeURI(youtubeLink) +
'&el=na&bf=false';
itemInfoLink = '/a/itemInfo' +
'/?video_id={0}&ac=www&t=grp';
runnel(
pushItem,
getItemInfo,
pingItem.bind(this),
downloadFile.bind(this),
function done(err) {
finalCallback(err);
});
}
module.exports = downloader;