summaryrefslogtreecommitdiff
path: root/file.py
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 /file.py
parent0e3f431a58a7dffb52566734e294704143d2654f (diff)
feat: reupload external links to hosting
Diffstat (limited to 'file.py')
-rw-r--r--file.py30
1 files changed, 30 insertions, 0 deletions
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}")