-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
60 lines (51 loc) · 2.34 KB
/
app.py
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
import logging
import os
from telegram import Update, InputFile
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters
from downloader import Downloader
import config
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(chat_id=update.effective_chat.id, text="Paste youtube video or instagram reel link to download it")
async def link(update: Update, context):
link = update.message.text
await update.message.reply_text("Downloading.... this may take a while")
downloader = Downloader(link)
status = False
try:
if link.find('instagram') != -1:
print("dowloading insta")
await downloader.download_instagram()
elif link.find('youtu.be') != -1 or link.find('youtube'):
print("downloading YT")
await downloader.download_youtube()
except Exception as e:
logging.error("Invalid URL")
await update.message.reply_text("Invalid URL")
if downloader.downloaded_path is not None:
await update.message.reply_text("Now Uploading....")
# Upload the downloaded file
await upload(update, context, path=downloader.downloaded_path)
# Delete the downloaded file after uploading
del downloader
else:
await update.message.reply_text("Uploading Failed")
async def upload(update: Update, context: ContextTypes.DEFAULT_TYPE, path):
try:
with open(path, 'rb') as file:
await context.bot.send_document(chat_id=update.effective_chat.id, document=InputFile(file))
# After successful upload, delete the file
os.remove(path)
await logging.INFO(f"File Uploaded: {path}")
except Exception as e:
logging.error(f"Error uploading or deleting file: {e}")
if __name__ == '__main__':
application = ApplicationBuilder().token(config.bot_token).build()
start_handler = CommandHandler('start', start)
link_handler = MessageHandler(filters.TEXT & (filters.Entity("url") | filters.Entity("text_link")), link)
application.add_handler(start_handler)
application.add_handler(link_handler)
application.run_polling(timeout= 600, write_timeout= 600)