theonlykid commited on
Commit
f1cfc72
·
verified ·
1 Parent(s): af87e37

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +49 -2
index.js CHANGED
@@ -3,8 +3,11 @@ 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();
@@ -191,4 +194,48 @@ bot.on('text', async (ctx) => {
191
 
192
  bot.launch();
193
 
194
- console.log('Bot is running...');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  const fs = require('fs');
4
  const axios = require('axios');
5
  const { Headers } = fetch;
6
+ const express = require('express');
7
+ const app = express();
8
+ const port = 7860;
9
 
10
+ const botToken = '6773241108:AAHz1TCLqpjR880ZLYHdaqJsL9Vxoqcr7jo'; // Replace with your Telegram bot token
11
  const bot = new Telegraf(botToken);
12
 
13
  const headers = new Headers();
 
194
 
195
  bot.launch();
196
 
197
+ console.log('Bot is running...');
198
+
199
+ app.get('/download', async (req, res) => {
200
+ const url = req.query.url;
201
+ const watermark = req.query.watermark === 'true';
202
+
203
+ if (!url) {
204
+ return res.status(400).json({ error: 'URL parameter is required' });
205
+ }
206
+
207
+ try {
208
+ const resolvedUrl = await getRedirectUrl(url);
209
+ const data = await getVideo(resolvedUrl, watermark);
210
+
211
+ if (data == null) {
212
+ return res.status(404).json({ error: 'Video not found or has been deleted' });
213
+ }
214
+
215
+ await downloadMedia(data);
216
+
217
+ if (data.images.length > 0) {
218
+ res.json({
219
+ message: 'Slideshow downloaded successfully',
220
+ files: data.images.map((_, index) => `${data.id}_${index}.jpeg`)
221
+ });
222
+ } else {
223
+ const filePath = `downloads/${data.id}.mp4`;
224
+ res.download(filePath, (err) => {
225
+ if (err) {
226
+ console.error('Error sending file:', err);
227
+ res.status(500).json({ error: 'Error sending file' });
228
+ }
229
+ // Optionally delete the file after sending
230
+ // fs.unlinkSync(filePath);
231
+ });
232
+ }
233
+ } catch (error) {
234
+ console.error('Error:', error);
235
+ res.status(500).json({ error: 'Internal server error' });
236
+ }
237
+ });
238
+
239
+ app.listen(port, () => {
240
+ console.log(`TikTok Downloader API listening at http://localhost:${port}`);
241
+ });