From bd4b7a66d161a8d1a89472f22d4857cd25de50dc Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sun, 22 Jun 2025 01:24:49 +0500 Subject: feat: reupload external links to hosting --- file.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 file.py (limited to 'file.py') 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}") -- cgit v1.2.3