summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-06-21 01:54:29 +0500
committerilotterytea <iltsu@alright.party>2025-06-21 01:54:29 +0500
commit35417b04e9ea521645d75e5d01958670d5b944b6 (patch)
tree40811f4ca30e635beb93fcafa4f1b894dc279c6c
parent818713ce93b6bde2e7ac04bfaa221c046000369e (diff)
feat: upload file to hosting
-rw-r--r--LICENSE13
-rw-r--r--main.py58
2 files changed, 67 insertions, 4 deletions
diff --git a/LICENSE b/LICENSE
index e69de29..a9f9bc4 100644
--- a/LICENSE
+++ b/LICENSE
@@ -0,0 +1,13 @@
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+ Version 2, December 2004
+
+Copyright (C) 2025 ilotterytea <me@ilotterytea.kz>
+
+Everyone is permitted to copy and distribute verbatim or modified
+copies of this license document, and changing it is allowed as long
+as the name is changed.
+
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. You just DO WHAT THE FUCK YOU WANT TO. \ No newline at end of file
diff --git a/main.py b/main.py
index d1cf5da..0edb9cd 100644
--- a/main.py
+++ b/main.py
@@ -1,20 +1,69 @@
+import mimetypes
+import traceback
+from os import mkdir
from os.path import exists
+import requests
from telegram import Update
+from telegram.constants import ParseMode
from telegram.ext import ApplicationBuilder, MessageHandler, CallbackContext, filters
-from os import mkdir
+
+
+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}")
+
async def download_file(update: Update, _: CallbackContext) -> None:
if not exists("./temp"):
mkdir("./temp")
+ file_path = None
+ file_comment = ""
+
if len(update.message.photo) > 0:
file_id = update.message.photo[-1]
file = await update.get_bot().get_file(file_id)
- file_path = f"./temp/{file_id.file_unique_id}"
+ file_extension = ""
+
+ if file.file_path is not None:
+ file_extension = f".{file.file_path.split('.')[-1]}"
+
+ file_path = f"./temp/{file_id.file_unique_id}{file_extension}"
await file.download_to_drive(file_path)
+ file_comment = update.message.caption
+
+ if file_path is not None:
+ try:
+ url = upload_file(file_path, file_comment)
+ await update.message.reply_text(f'Saved! Here is your URL: [{url}]({url})', ParseMode.MARKDOWN,
+ reply_to_message_id=update.message.message_id)
+ except Exception as e:
+ traceback.print_tb(e.__traceback__)
+ await update.message.reply_text(f"Error occurred while uploading your file: {str(e)}")
- await update.message.reply_text('Saved!')
def run():
print("Starting the bot...")
@@ -22,5 +71,6 @@ def run():
app.add_handler(MessageHandler(filters.PHOTO, download_file))
app.run_polling()
+
if __name__ == "__main__":
- run() \ No newline at end of file
+ run()