summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-06-22 01:24:49 +0500
committerilotterytea <iltsu@alright.party>2025-06-22 01:24:49 +0500
commitbd4b7a66d161a8d1a89472f22d4857cd25de50dc (patch)
tree0d0e49b7578d8e76d31c2a7417dabb68d5fd1df8
parent0e3f431a58a7dffb52566734e294704143d2654f (diff)
feat: reupload external links to hosting
-rw-r--r--extsrc.py65
-rw-r--r--file.py30
-rw-r--r--main.py33
3 files changed, 99 insertions, 29 deletions
diff --git a/extsrc.py b/extsrc.py
new file mode 100644
index 0000000..f321cac
--- /dev/null
+++ b/extsrc.py
@@ -0,0 +1,65 @@
+from os import unlink
+from time import time
+
+import yt_dlp
+from file import upload_file
+from telegram import Update
+from telegram.constants import ParseMode
+from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters, ContextTypes
+
+STATES = dict(
+ extsrc_url=1
+)
+
+
+async def download_from_extenal_source(update: Update, url: str):
+ ydl = yt_dlp.YoutubeDL({
+ "format": "worst",
+ "outtmpl": "./temp/%(id)s.%(ext)s"
+ })
+ msg = await update.message.reply_text('Downloading from external source (may take a while)...',
+ reply_to_message_id=update.message.message_id)
+
+ start_time = time()
+ ydl.download(url)
+
+ await msg.edit_text(f'Uploading to file hosting (downloaded in {int(time() - start_time)}s)...')
+
+ info = ydl.extract_info(url)
+ file_path = f"./temp/{info['id']}.{info['ext']}"
+ url = upload_file(file_path, '')
+
+ await msg.delete()
+ await update.message.reply_text(f'Saved! Here is your URL: [{url}]({url})', ParseMode.MARKDOWN,
+ reply_to_message_id=update.message.message_id)
+
+ unlink(file_path)
+
+
+async def extsrc(update: Update, context: ContextTypes.DEFAULT_TYPE):
+ if context.args:
+ await download_from_extenal_source(update, context.args[0])
+ return ConversationHandler.END
+ else:
+ await update.message.reply_text("Send an URL and I will try to reupload it to file hosting.")
+ return STATES["extsrc_url"]
+
+
+async def extsrc_continue(update: Update, _: ContextTypes.DEFAULT_TYPE):
+ await download_from_extenal_source(update, update.message.text)
+ return ConversationHandler.END
+
+
+async def cancel(update: Update, _: ContextTypes.DEFAULT_TYPE):
+ await update.message.reply_text("Command cancelled.")
+ return ConversationHandler.END
+
+
+def extsrc_add_handler(app):
+ app.add_handler(ConversationHandler(
+ entry_points=[CommandHandler("extsrc", extsrc)],
+ states={
+ STATES["extsrc_url"]: [MessageHandler(filters.TEXT & ~filters.COMMAND, extsrc_continue)]
+ },
+ fallbacks=[CommandHandler('cancel', cancel)]
+ ))
diff --git a/file.py b/file.py
new file mode 100644
index 0000000..1109bcb
--- /dev/null
+++ b/file.py
@@ -0,0 +1,30 @@
+from mimetypes import guess_type
+
+from requests import post
+
+
+def upload_file(file_path: str, comment: str) -> str:
+ mime_type, _ = guess_type(file_path)
+
+ if mime_type is None:
+ raise Exception("Unknown MIME type")
+
+ response = post(
+ 'https://tnd.quest/upload.php',
+ files=dict(
+ file=(file_path.split('/')[-1], open(file_path, 'rb'), mime_type)
+ ),
+ data=dict(
+ comment=comment,
+ visibility=1
+ ),
+ headers=dict(
+ accept='application/json'
+ )
+ )
+
+ if response.status_code == 201:
+ j = response.json()
+ return j['data']['urls']['download_url']
+ else:
+ raise Exception(f"Failed to send a file ({response.status_code}): {response.text}")
diff --git a/main.py b/main.py
index d7783d1..d04f1b1 100644
--- a/main.py
+++ b/main.py
@@ -1,39 +1,13 @@
-import mimetypes
import traceback
from os import mkdir, unlink
from os.path import exists
-import requests
from telegram import Update
from telegram.constants import ParseMode
from telegram.ext import ApplicationBuilder, MessageHandler, CallbackContext, filters
-
-def upload_file(file_path: str, comment: str) -> str:
- mime_type, _ = mimetypes.guess_type(file_path)
-
- if mime_type is None:
- raise Exception("Unknown MIME type")
-
- response = requests.post(
- 'https://tnd.quest/upload.php',
- files=dict(
- file=(file_path.split('/')[-1], open(file_path, 'rb'), mime_type)
- ),
- data=dict(
- comment=comment,
- visibility=1
- ),
- headers=dict(
- accept='application/json'
- )
- )
-
- if response.status_code == 201:
- j = response.json()
- return j['data']['urls']['download_url']
- else:
- raise Exception(f"Failed to send a file ({response.status_code}): {response.text}")
+from extsrc import extsrc_add_handler
+from file import upload_file
async def download_locally_file(update, file_id):
@@ -92,9 +66,10 @@ def run():
app.add_handler(MessageHandler(
filters.PHOTO | filters.VIDEO | filters.VIDEO_NOTE |
filters.VOICE | filters.AUDIO | filters.ANIMATION |
- filters.Document.ALL | (filters.TEXT & ~filters.COMMAND),
+ filters.Document.ALL,
download_file
))
+ extsrc_add_handler(app)
app.run_polling()