diff options
| author | ilotterytea <iltsu@alright.party> | 2025-06-22 01:24:49 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-06-22 01:24:49 +0500 |
| commit | bd4b7a66d161a8d1a89472f22d4857cd25de50dc (patch) | |
| tree | 0d0e49b7578d8e76d31c2a7417dabb68d5fd1df8 /file.py | |
| parent | 0e3f431a58a7dffb52566734e294704143d2654f (diff) | |
feat: reupload external links to hosting
Diffstat (limited to 'file.py')
| -rw-r--r-- | file.py | 30 |
1 files changed, 30 insertions, 0 deletions
@@ -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}") |
