Spaces:
Runtime error
Runtime error
Create index.js
Browse files
index.js
ADDED
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const { Telegraf } = require('telegraf');
|
2 |
+
const fetch = require('node-fetch');
|
3 |
+
const fs = require('fs');
|
4 |
+
const axios = require('axios');
|
5 |
+
const { Headers } = fetch;
|
6 |
+
|
7 |
+
const botToken = 'YOUR_TELEGRAM_BOT_TOKEN'; // Replace with your Telegram bot token
|
8 |
+
const bot = new Telegraf(botToken);
|
9 |
+
|
10 |
+
const headers = new Headers();
|
11 |
+
|
12 |
+
const getRedirectUrl = async (url) => {
|
13 |
+
if (url.includes("vm.tiktok.com") || url.includes("vt.tiktok.com")) {
|
14 |
+
url = await fetch(url, {
|
15 |
+
redirect: "follow",
|
16 |
+
follow: 10,
|
17 |
+
});
|
18 |
+
url = url.url;
|
19 |
+
console.log("[*] Redirecting to: " + url);
|
20 |
+
}
|
21 |
+
return url;
|
22 |
+
};
|
23 |
+
|
24 |
+
const getIdVideo = async (url) => {
|
25 |
+
if (url.includes("/t/")) {
|
26 |
+
url = await new Promise((resolve) => {
|
27 |
+
require("follow-redirects").https.get(url, function (res) {
|
28 |
+
return resolve(res.responseUrl);
|
29 |
+
});
|
30 |
+
});
|
31 |
+
}
|
32 |
+
const matching = url.includes("/video/");
|
33 |
+
const matchingPhoto = url.includes("/photo/");
|
34 |
+
let idVideo = url.substring(
|
35 |
+
url.indexOf("/video/") + 7,
|
36 |
+
url.indexOf("/video/") + 26
|
37 |
+
);
|
38 |
+
if (matchingPhoto)
|
39 |
+
idVideo = url.substring(
|
40 |
+
url.indexOf("/photo/") + 7,
|
41 |
+
url.indexOf("/photo/") + 26
|
42 |
+
);
|
43 |
+
else if (!matching) {
|
44 |
+
throw new Error("URL not found");
|
45 |
+
}
|
46 |
+
return idVideo.length > 19
|
47 |
+
? idVideo.substring(0, idVideo.indexOf("?"))
|
48 |
+
: idVideo;
|
49 |
+
};
|
50 |
+
|
51 |
+
const getVideo = async (url, watermark) => {
|
52 |
+
const idVideo = await getIdVideo(url);
|
53 |
+
const API_URL = `https://api22-normal-c-alisg.tiktokv.com/aweme/v1/feed/?aweme_id=${idVideo}&iid=7318518857994389254&device_id=7318517321748022790&channel=googleplay&app_name=musical_ly&version_code=300904&device_platform=android&device_type=ASUS_Z01QD&version=9`;
|
54 |
+
const request = await fetch(API_URL, {
|
55 |
+
method: "OPTIONS",
|
56 |
+
headers: headers,
|
57 |
+
});
|
58 |
+
const body = await request.text();
|
59 |
+
try {
|
60 |
+
var res = JSON.parse(body);
|
61 |
+
} catch (err) {
|
62 |
+
console.error("Error:", err);
|
63 |
+
console.error("Response body:", body);
|
64 |
+
throw err;
|
65 |
+
}
|
66 |
+
|
67 |
+
if (res.aweme_list[0].aweme_id != idVideo) {
|
68 |
+
return null;
|
69 |
+
}
|
70 |
+
|
71 |
+
let urlMedia = "";
|
72 |
+
let image_urls = [];
|
73 |
+
|
74 |
+
if (!!res.aweme_list[0].image_post_info) {
|
75 |
+
console.log("[*] Video is slideshow");
|
76 |
+
res.aweme_list[0].image_post_info.images.forEach((element) => {
|
77 |
+
image_urls.push(element.display_image.url_list[1]);
|
78 |
+
});
|
79 |
+
} else {
|
80 |
+
urlMedia = watermark
|
81 |
+
? res.aweme_list[0].video.download_addr.url_list[0]
|
82 |
+
: res.aweme_list[0].video.play_addr.url_list[0];
|
83 |
+
}
|
84 |
+
|
85 |
+
return {
|
86 |
+
url: urlMedia,
|
87 |
+
images: image_urls,
|
88 |
+
id: idVideo,
|
89 |
+
};
|
90 |
+
};
|
91 |
+
|
92 |
+
const downloadMedia = async (item) => {
|
93 |
+
const folder = "downloads/";
|
94 |
+
if (!fs.existsSync(folder)) fs.mkdirSync(folder, { recursive: true });
|
95 |
+
|
96 |
+
if (item.images.length != 0) {
|
97 |
+
console.log("[*] Downloading Slideshow");
|
98 |
+
let index = 0;
|
99 |
+
for (const image_url of item.images) {
|
100 |
+
const fileName = `${item.id}_${index}.jpeg`;
|
101 |
+
if (fs.existsSync(folder + fileName)) {
|
102 |
+
console.log(`[!] File '${fileName}' already exists. Skipping`);
|
103 |
+
continue;
|
104 |
+
}
|
105 |
+
index++;
|
106 |
+
const response = await fetch(image_url);
|
107 |
+
const buffer = await response.buffer();
|
108 |
+
fs.writeFileSync(folder + fileName, buffer);
|
109 |
+
}
|
110 |
+
} else {
|
111 |
+
const fileName = `${item.id}.mp4`;
|
112 |
+
if (fs.existsSync(folder + fileName)) {
|
113 |
+
console.log(`[!] File '${fileName}' already exists. Skipping`);
|
114 |
+
return;
|
115 |
+
}
|
116 |
+
const response = await fetch(item.url);
|
117 |
+
const buffer = await response.buffer();
|
118 |
+
fs.writeFileSync(folder + fileName, buffer);
|
119 |
+
}
|
120 |
+
};
|
121 |
+
|
122 |
+
bot.start((ctx) => ctx.reply('My love! send me a TikTok or Instagram video link baby 😘'));
|
123 |
+
|
124 |
+
bot.on('text', async (ctx) => {
|
125 |
+
const url = ctx.message.text;
|
126 |
+
|
127 |
+
if (!url.includes('instagram.com') && !url.includes('tiktok.com')) {
|
128 |
+
return ctx.reply('Baby, the url is not correct...');
|
129 |
+
}
|
130 |
+
|
131 |
+
try {
|
132 |
+
const processingMessage = await ctx.reply('🤗Honey, wait a bit my love. Processing the URL...');
|
133 |
+
let data;
|
134 |
+
if (url.includes('instagram.com')) {
|
135 |
+
const dataList = await instagramDl(url);
|
136 |
+
const downloadLink = dataList[0].download_link;
|
137 |
+
const response = await axios.get(downloadLink, { responseType: 'stream' });
|
138 |
+
await ctx.editMessageText('Uploading the video...my baby ><', { message_id: processingMessage.message_id });
|
139 |
+
const filePath = `video_${Date.now()}.mp4`;
|
140 |
+
const writer = fs.createWriteStream(filePath);
|
141 |
+
response.data.pipe(writer);
|
142 |
+
|
143 |
+
writer.on('finish', () => {
|
144 |
+
ctx.replyWithVideo({ source: filePath })
|
145 |
+
.then(() => {
|
146 |
+
fs.unlinkSync(filePath); // Delete the file after sending
|
147 |
+
})
|
148 |
+
.catch((error) => {
|
149 |
+
console.error('Error sending video:', error);
|
150 |
+
ctx.reply('Failed to send the video.');
|
151 |
+
});
|
152 |
+
});
|
153 |
+
|
154 |
+
writer.on('error', (error) => {
|
155 |
+
console.error('Error downloading video:', error);
|
156 |
+
ctx.reply('Failed to download the video.');
|
157 |
+
});
|
158 |
+
} else if (url.includes('tiktok.com')) {
|
159 |
+
const processingMessage = await ctx.reply('🤗Honey, wait a bit my love. Processing the URL...');
|
160 |
+
const resolvedUrl = await getRedirectUrl(url);
|
161 |
+
data = await getVideo(resolvedUrl, false);
|
162 |
+
|
163 |
+
if (data == null) {
|
164 |
+
return ctx.reply('Video not found or has been deleted.');
|
165 |
+
}
|
166 |
+
|
167 |
+
await downloadMedia(data);
|
168 |
+
|
169 |
+
if (data.images.length > 0) {
|
170 |
+
data.images.forEach((image, index) => {
|
171 |
+
ctx.replyWithPhoto({ url: image });
|
172 |
+
});
|
173 |
+
} else {
|
174 |
+
const filePath = `downloads/${data.id}.mp4`;
|
175 |
+
await ctx.editMessageText('Uploading the video...my baby ><', { message_id: processingMessage.message_id });
|
176 |
+
ctx.replyWithVideo({ source: filePath }, { caption: 'Downloaded TikTok Video' })
|
177 |
+
.then(() => {
|
178 |
+
fs.unlinkSync(filePath); // Delete the file after sending
|
179 |
+
})
|
180 |
+
.catch((error) => {
|
181 |
+
console.error('Error sending video:', error);
|
182 |
+
ctx.reply('Failed to send the video.');
|
183 |
+
});
|
184 |
+
}
|
185 |
+
}
|
186 |
+
} catch (error) {
|
187 |
+
console.error('Error:', error);
|
188 |
+
ctx.reply('Failed to process the URL.');
|
189 |
+
}
|
190 |
+
});
|
191 |
+
|
192 |
+
bot.launch();
|
193 |
+
|
194 |
+
console.log('Bot is running...');
|