summaryrefslogtreecommitdiff
path: root/file.py
blob: 1109bcbe324ebf2daac35853b4de722423ea3371 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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}")