-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
173 lines (163 loc) · 4.83 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const WebSocketClient = require('websocket').client;
const storage = require('node-persist');
const alltomp3 = require('alltomp3');
const fs = require('fs');
const SERVER_URL = 'wss://server.alltomp3.org/telegram/';
const MAX_PARALLEL = 4;
const TEMP_FOLDER = '/tmp/';
let client = new WebSocketClient();
let con;
let waittry = 1;
let credentials;
let confirmk;
let processing = {
active: 0,
queue: [],
newQuery: function (q) {
this.queue.push(q);
this.processNext();
},
processQuery: function (q) {
console.log('Processing', q.id, q.trackURL || q.singleURL);
this.active += 1;
if (q.trackURL) {
var e = alltomp3.downloadTrackURL(q.trackURL, TEMP_FOLDER);
} else if (q.singleURL) {
var e = alltomp3.downloadAndTagSingleURL(q.singleURL, TEMP_FOLDER);
} else {
return;
}
let progress = 0;
e.on('download', (infos) => {
if (infos.progress / 2 - progress > 25) {
progress = infos.progress / 2;
senda({ id: q.id, progress: progress });
}
});
e.on('download-end', (infos) => {
progress = 50;
senda({ id: q.id, progress: progress });
});
e.on('convert', (infos) => {
if (infos.progress / 2 + 50 - progress > 25) {
progress = infos.progress / 2 + 50;
senda({ id: q.id, progress: progress });
}
});
e.once('end', (infos) => {
const sendFile = (filepath) => {
fs.readFile(filepath, (err, data) => {
const bufId = Buffer.from(q.id);
const len = bufId.length + data.length;
const buf = Buffer.concat([bufId, data], len);
console.log('End of', q.id, 'sending buffer of length', len);
con.sendBytes(buf);
this.active -= 1;
this.processNext();
fs.unlinkSync(filepath);
});
};
const fileStats = fs.statSync(infos.file);
if (fileStats.size >= 49 * 1000 * 1000 && q.singleURL) {
fs.unlinkSync(infos.file);
console.log('File too large: converting to 128 kb/s');
// Telegram has a max size of 50 MB
const newEvents = alltomp3.downloadAndTagSingleURL(q.singleURL, TEMP_FOLDER, null, null, null, null, {
bitrate: '128k',
});
newEvents.once('end', (newInfos) => sendFile(newInfos.file));
} else {
sendFile(infos.file);
}
});
e.on('error', (error) => {
senda({ id: q.id, error: error });
console.error('An error occured', error);
this.active -= 1;
this.processNext();
});
},
processNext: function () {
if (this.active < MAX_PARALLEL && this.queue.length > 0) {
let q = this.queue.shift();
this.processQuery(q);
}
},
};
client.on('connectFailed', function (error) {
console.log('Connect Error: ' + error.toString());
});
let reconnectTimer;
client.on('connect', function (connection) {
con = connection;
if (reconnectTimer) {
clearInterval(reconnectTimer);
reconnectTimer = null;
}
console.log('Connected!\n');
connection.on('error', function (error) {
console.log('Connection Error: ' + error.toString());
});
connection.on('close', function () {
console.log('Connection closed, retrying in 30s...');
reconnectTimer = setInterval(() => {
client.connect(SERVER_URL, 'echo-protocol');
}, 30000);
});
connection.on('message', function (message) {
if (message.type === 'utf8') {
try {
var message = JSON.parse(message.utf8Data);
} catch (e) {}
if (message.key && message.password && message.confirm) {
storage.setItem('credentials', {
key: message.key,
password: message.password,
});
storage.setItem('confirm', message.confirm);
console.log(
'Now registered.\nTo manage your bot from Telegram, talk to @AllToMP3_bot.\n\nYour key is: ' +
message.key +
'-' +
message.confirm +
'\n\n',
);
} else if (message.query) {
processing.newQuery(message.query);
}
}
});
storage.getItem('credentials', function (err, cred) {
if (!cred) {
senda({ hello: 'world!' });
} else {
credentials = cred;
storage.getItem('confirm', function (err, confirm) {
if (confirm) {
confirmk = confirm;
console.log('Your key is: ' + credentials.key + '-' + confirm + '\n\n');
}
});
senda(credentials);
}
});
});
storage
.init({
dir: './data/',
stringify: JSON.stringify,
parse: JSON.parse,
encoding: 'utf8',
})
.then(function () {
storage.getItem('credentials', function (err, cred) {
credentials = cred;
storage.getItem('confirm', function (err, confirm) {
confirmk = confirm;
client.connect(SERVER_URL, 'echo-protocol');
});
});
});
let senda = (message) => {
con.sendUTF(JSON.stringify(message));
};