-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
258 lines (225 loc) Β· 8.08 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
'use strict';
const WebTorrent = require('webtorrent');
const Telegraf = require('telegraf');
const Axios = require('axios');
const Composer = require('telegraf/composer');
const session = require('telegraf/session');
const Stage = require('telegraf/stage');
const Markup = require('telegraf/markup');
const WizardScene = require('telegraf/scenes/wizard');
const Fs = require('fs');
const Path = require('path');
const config = JSON.parse(Fs.readFileSync('config.json'));
const bot = new Telegraf(config.botToken);
const client = new WebTorrent();
client.on('error', (err) => console.log(err));
function addTorrentMagnet(ctx, path) {
const message = ctx.message.text;
const torrent = client.add(message, { path: path }, function (torrent) { });
torrent.on('error', (err) => ctx.reply(`Error downloading torrent: ${err}`));
torrent.on('noPeers', () => ctx.reply(`Torrent: ${torrent.name} has no peers`));
torrent.on('done', function () {
ctx.reply(`Torrent: ${torrent.name} download finished`)
});
}
async function addTorrentFile(ctx, path) {
if (ctx.message.document.file_id !== undefined) {
const fileId = ctx.message.document.file_id;
const url = await ctx.telegram.getFileLink(fileId);
const torrent = client.add(url, { path: path }, function (torrent) { });
torrent.on('error', (err) => ctx.reply(`Error downloading torrent: ${err}`));
torrent.on('noPeers', () => ctx.reply(`Torrent: ${torrent.name} has no peers`));
torrent.on('done', function () {
ctx.reply(`Torrent: ${torrent.name} download finished`)
});
}
else
ctx.reply('A proper file wasn\'t sent, please retry');
}
async function downloadFile(ctx, url, path) {
path = Path.resolve(path, ctx.message.document.file_name);
const writer = Fs.createWriteStream(path);
const response = await Axios({
url,
method: 'GET',
responseType: 'stream'
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', resolve)
writer.on('error', reject)
});
}
const mediaTypeHandlerTor = new Composer();
mediaTypeHandlerTor.action('movie', ctx => {
ctx.wizard.state.mediaType = 'movie';
return askTorrentSource(ctx);
});
mediaTypeHandlerTor.action('tv', ctx => {
ctx.wizard.state.mediaType = 'tv';
return askTorrentSource(ctx);
});
mediaTypeHandlerTor.action('music', ctx => {
ctx.wizard.state.mediaType = 'music';
return askTorrentSource(ctx);
});
mediaTypeHandlerTor.action('other', ctx => {
ctx.wizard.state.mediaType = 'other';
return askTorrentSource(ctx);
});
const askTorrentSource = (ctx) => {
ctx.reply('Is a .torrent file or a magnet URL?', Markup.inlineKeyboard([
Markup.callbackButton("Magnet π§²", "magnet"),
Markup.callbackButton("File π", "file")
]).extra());
return ctx.wizard.next();
};
const torrentSourceHandler = new Composer();
torrentSourceHandler.action('magnet', ctx => {
ctx.wizard.state.torrentSource = 'magnet';
ctx.reply('Enter magnet URL (or send "abort" to cancel):');
return ctx.wizard.next();
});
torrentSourceHandler.action('file', ctx => {
ctx.wizard.state.torrentSource = 'file';
ctx.reply('Send the .torrent file (or send "abort" to cancel):');
return ctx.wizard.next();
});
const torrentWiz = new WizardScene('torrentWiz',
(ctx) => {
ctx.reply('In which directory will it be?', Markup.inlineKeyboard([
Markup.callbackButton("Movies π¬", "movie"),
Markup.callbackButton("TV Shows πΊ", "tv"),
Markup.callbackButton("Music π΅", "music"),
Markup.callbackButton("Other", "other")
]).extra());
return ctx.wizard.next();
},
mediaTypeHandlerTor,
torrentSourceHandler,
(ctx) => {
if (ctx.message === undefined)
return ctx.scene.leave();
if (ctx.message.text === 'abort') {
ctx.reply('Abortting.');
return ctx.scene.leave();
}
const path = ctx.wizard.state.mediaType === "other" ? config.outputDir : `${config.outputDir}/${ctx.wizard.state.mediaType}`;
if (ctx.wizard.state.torrentSource === "magnet")
addTorrentMagnet(ctx, path);
else addTorrentFile(ctx, path);
ctx.reply('Done!');
ctx.reply(
`How can I help you, ${ctx.from.first_name}?`,
Markup.inlineKeyboard([
Markup.callbackButton("Download a torrent β¬οΈ", "torrent"),
Markup.callbackButton("Download a file π", "file")
]).extra()
);
return ctx.scene.leave();
}
);
const mediaTypeHandlerFile = new Composer();
mediaTypeHandlerFile.action('movie', ctx => {
ctx.wizard.state.mediaType = 'movie';
return askSendFile(ctx);
});
mediaTypeHandlerFile.action('tv', ctx => {
ctx.wizard.state.mediaType = 'tv';
return askSendFile(ctx);
});
mediaTypeHandlerFile.action('music', ctx => {
ctx.wizard.state.mediaType = 'music';
return askSendFile(ctx);
});
mediaTypeHandlerFile.action('other', ctx => {
ctx.wizard.state.mediaType = 'other';
return askSendFile(ctx);
});
const askSendFile = (ctx) => {
ctx.reply('Send the file: (or send "abort" to cancel)');
return ctx.wizard.next();
};
const fileWiz = new WizardScene('fileWiz',
ctx => {
ctx.reply('In which directory will it be?', Markup.inlineKeyboard([
Markup.callbackButton("Movies π¬", "movie"),
Markup.callbackButton("TV Shows πΊ", "tv"),
Markup.callbackButton("Music π΅", "music"),
Markup.callbackButton("Other", "other")
]).extra());
return ctx.wizard.next();
},
mediaTypeHandlerFile,
ctx => {
if (ctx.message === undefined)
return ctx.scene.leave();
if (ctx.message.text === 'abort') {
ctx.reply('Abortting.');
return ctx.scene.leave();
}
const path = ctx.wizard.state.mediaType === "other" ? config.outputDir : `${config.outputDir}/${ctx.wizard.state.mediaType}`;
try {
const fileId = ctx.message.document.file_id;
ctx.telegram.getFileLink(fileId)
.then((url) => downloadFile(ctx, url, path)
.then(() => ctx.reply(`File: ${ctx.message.document.file_name} download finished successfully!`))
.catch(() => ctx.reply(`Error: could not download file: ${ctx.message.document.file_name}`)));
}
catch (error) {
ctx.reply('A proper file wasn\'t sent, please try again');
}
ctx.reply(
`How can I help you, ${ctx.from.first_name}?`,
Markup.inlineKeyboard([
Markup.callbackButton("Download a torrent β¬οΈ", "torrent"),
Markup.callbackButton("Download a file π", "file")
]).extra()
);
return ctx.scene.leave();
}
);
const stage = new Stage();
stage.register(torrentWiz);
stage.register(fileWiz);
bot.start(ctx => {
if (ctx.scene !== undefined)
ctx.scene.leave();
ctx.reply(
`How can I help you, ${ctx.from.first_name}?`,
Markup.inlineKeyboard([
Markup.callbackButton("Download a torrent β¬οΈ", "torrent"),
Markup.callbackButton("Download a file π", "file")
]).extra()
);
});
bot.use(session());
bot.use(stage.middleware());
bot.action("torrent", ctx => {
if (ctx.scene !== undefined)
ctx.scene.leave();
ctx.scene.enter('torrentWiz')
});
bot.command("torrent", ctx => {
if (ctx.scene !== undefined)
ctx.scene.leave();
ctx.scene.enter('torrentWiz')
});
bot.action("file", ctx => {
if (ctx.scene !== undefined)
ctx.scene.leave();
ctx.scene.enter('fileWiz')
});
bot.command("file", ctx => {
if (ctx.scene !== undefined)
ctx.scene.leave();
ctx.scene.enter('fileWiz')
});
process.on('uncaughtException', function (err) {
if (err) {
const date = new Date().toLocaleString();
Fs.appendFileSync(config.logPath, `Error ${date}: ${err.stack}\n`);
process.exit(1);
}
});
bot.launch();