-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
340 lines (300 loc) · 9.29 KB
/
helpers.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import "dotenv/config";
import {
createWriteStream,
readFile,
writeFile,
statSync,
existsSync,
mkdirSync,
close,
appendFile,
promises as fsPromises,
unlink,
} from "fs";
import ytdl from "ytdl-core";
import path from "path";
import readline from "readline";
import * as url from "url";
import { exec } from "child_process";
import { promisify } from "util";
import { pipeline } from "stream";
import fetch from "node-fetch";
import { getVideoDurationInSeconds } from "get-video-duration";
import { TelegramClient, Api } from "telegram";
import { StringSession } from "telegram/sessions/index.js";
import { sendFile } from "telegram/client/uploads.js";
import ffprobe from "ffprobe";
import ffprobeStatic from "ffprobe-static";
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
const ffmpeg = promisify(exec);
// CONSTANTS
const COOKIES = process.env.COOKIES;
const CHANNEL_ID = process.env.CHANNEL_ID;
const APP_ID = process.env.APP_ID;
const API_HASH = process.env.API_HASH;
const SESSION_STRING = process.env.SESSION_STRING;
// reverse order file content
export const reverseOrder = async (inputFile, outputFile) => {
const lines = await readFile(inputFile, "utf8").split("\n");
let result = "";
for (let i = 0; i < lines.length; i++) {
result = lines[i] + "\n" + result;
}
writeFile(outputFile, result, (err) => {
if (err) console.log(err);
});
};
export const getVideoInfo = (url) => {
return new Promise(async (resolve, reject) => {
try {
// use cookies to get video info
const { videoDetails } = await ytdl.getInfo(url, {
requestOptions: {
headers: {
cookie: COOKIES,
},
},
});
const result = {
videoTitle: videoDetails.title,
viewCount: videoDetails.viewCount,
thumbnail: videoDetails.thumbnail.thumbnail.pop().url,
lengthSecond: videoDetails.lengthSeconds,
publisheDate: videoDetails.publisheDate,
description: videoDetails.description,
videoUrl: videoDetails.video_url,
likes: videoDetails.likes,
};
resolve(result);
} catch (error) {
reject(error);
}
});
};
export const downloadVideo = (url) => {
return new Promise(async (resolve, reject) => {
try {
const output = path.resolve(__dirname, "video.mp4");
const video = ytdl(url, {
filter: "audioandvideo",
quality: "highestaudio",
requestOptions: {
headers: {
cookie: COOKIES,
},
},
});
let startTime;
video.pipe(createWriteStream(output));
video.once("response", () => {
startTime = Date.now();
});
video.on("progress", (chunkLength, downloaded, total) => {
const percent = ((downloaded / total) * 100).toFixed(2);
const downloadMinutes = (Date.now() - startTime) / 1000 / 60;
const estimateDownloadTime =
downloadMinutes / percent - downloadMinutes;
readline.cursorTo(process.stdout, 0);
process.stdout.write(`${(percent * 100).toFixed(2)} % Downloaded`);
process.stdout.write(
`(${(downloaded / 1024 / 1024).toFixed(2)}MB of ${(
total /
1024 /
1024
).toFixed(2)}MB)\n`
);
process.stdout.write(
`running for: ${downloadMinutes.toFixed(2)}minutes`
);
process.stdout.write(
`, estimated time left: ${estimateDownloadTime.toFixed(2)}minutes `
);
readline.moveCursor(process.stdout, 0, -1);
});
video.on("end", (err) => {
process.stdout.write("\n\n");
resolve(true);
});
} catch (error) {
reject(error);
}
});
};
export const getFileSize = (fileName) => {
const stats = statSync(fileName);
const fileSizeInMegaBytes = stats.size;
return fileSizeInMegaBytes;
};
export const cutFileToParts = async (input, start, end, output) => {
return await new Promise((resolve, reject) => {
ffmpeg(
`ffmpeg -hide_banner -i ${input} -ss ${start} -to ${end} -async 1 -strict -2 -c copy -map -0 ${output}`,
(err) => {
if (err) {
reject(err);
}
resolve("Splitting Done");
}
);
});
};
export const splitFiles = async (inputFile) => {
return await new Promise(async (resolve, reject) => {
try {
const workingDirectory = path.join(__dirname);
const newWorkingDirectory = path.join(workingDirectory, "temp");
if (!existsSync(newWorkingDirectory)) {
mkdirSync(newWorkingDirectory);
}
const meta = await getVideoDurationInSeconds(inputFile);
let totalDuration = meta.duration;
const totalFileSize = getFileSize(inputFile);
let minimumDuration = (totalDuration / totalFileSize) * 102864000; // 1Gb
minimumDuration = parseInt(minimumDuration);
let arr = [];
let startTime = 0;
let endTime = minimumDuration;
let baseFilename = inputFile.split(".")[0];
let fileExtension = inputFile.split(".").pip();
let i = 0;
let flag = false;
while (endTime <= totalDuration) {
let partedFilename = `${baseFilename}_${i}.${fileExtension}`;
let outputFile = path.join(newWorkingDirectory, partedFilename);
await cutFileToParts(inputFile, startTime, endTime, outputFile);
startTime = endTime - 3;
endTime = endTime + minimumDuration;
arr.push(path.join(newWorkingDirectory, partedFilename));
i = i + 1;
if (endTime > totalDuration && !flag) {
endTime = totalDuration;
flag = true;
} else if (flag) {
resolve(arr);
break;
}
}
} catch (error) {
reject(error);
}
});
};
export const downloadFile = async ({ url, path }) => {
const streamPipeline = promisify(pipeline);
const response = await fetch(url);
if (!response.ok) {
throw new Error(`unexpected response ${response.statusText}`);
}
await streamPipeline(response.body, createWriteStream(path));
return response;
};
export const wrietToFile = async (text) => {
function closefd(fd) {
close(fd, (err) => {
if (err) throw err;
});
}
try {
appendFile(fd, `${text}\n`, "utf-8", (err) => {
closefd(fd);
if (err) throw err;
});
} catch (err) {
closefd(fd);
throw err;
}
};
export const asyncReadFile = async (fileName) => {
try {
const content = await fsPromises.readFile(fileName, "utf-8");
const arr = content.split(/\r?\n/);
return arr;
} catch (err) {
console.log(err);
}
};
export const connectToTelegram = async () => {
return new Promise(async (resolve, reject) => {
try {
const client = new TelegramClient(
new StringSession(SESSION_STRING),
APP_ID,
API_HASH
);
await client.connect();
resolve(client);
} catch (error) {
reject(error);
}
});
};
export const sendFileToTelegram = async (client, videoInfo, videoPath) => {
return await new Promise(async (resolve, reject) => {
try {
const videoData = await ffprobe(videoPath, {
path: ffprobeStatic.path,
});
await downloadFile({
url: videoInfo.thumbnail,
path: "./tmp/thumbnail.jpg",
});
const result = await sendFile(client, CHANNEL_ID, {
file: "video.mp4",
thumb: "./tmp/thumbnail.jpg",
supportStreaming: true,
// progressCallback: (progress) => {
// console.log(progress);
// },
attributes: [
new Api.DocumentAttributeVideo({
supportStreaming: true,
duration: parseInt(videoData.streams[0].duration),
h: videoData.streams[0].height,
w: videoData.streams[0].width,
}),
],
});
resolve(true);
} catch (error) {
reject(error);
}
});
};
export const main = async () => {
const client = await connectToTelegram();
await reverseOrder("links.txt", "links.txt");
const videoLink = await asyncReadFile("links.txt");
for (let [index, link] of videoLink.entries()) {
const videoInfo = await getVideoInfo(link);
const downloadVideoResult = await downloadVideo(link);
const caption = `#${index}\n**Title**: \n${videoInfo.videoTitle}\n**Likes**: \n${videoInfo.likes}\n**Duration**: \n${videoInfo.lengthSeconds}s\n**Uploaded At**: \n${videoInfo.uploadDate}\n**Video URL**: \n${videoInfo.videoUrl}\n**Thumbnail URL**: \n${videoInfo.thumbnail}\n**Description**: \n${videoInfo.description}`;
await client.sendMessage(process.env.CHANNEL_ID, {
message: caption,
linkPreview: false,
});
if (downloadVideoResult) {
const videoSize = getFileSize("video.mp4");
if (videoSize >= 1900000000) {
const splitedFiles = splitFiles("video.mp4");
for (let path of splitedFiles) {
const sendFileResult = await sendFileToTelegram(
client,
videoInfo,
path
);
if (sendFileResult) {
await unlink(path, (err) => {
if (err) console.log(err);
else {
console.log("File deleted");
}
});
}
}
}
await sendFileToTelegram(client, videoInfo, "video.mp4");
console.log(`video ${index} sent`);
await wrietToFile(`${index} ${index}`);
}
}
};