-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
gofile.py
44 lines (39 loc) · 1.32 KB
/
gofile.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
import json
import shlex
import requests
import subprocess
def uploadFile(file, token=None, folderId=None):
server = requests.get("https://api.gofile.io/getServer").json()["data"]["server"]
cmd = 'curl '
cmd += f'-F "file=@{file}" '
if token:
cmd += f'-F "token={token}" '
if folderId:
cmd += f'-F "folderId={folderId}" '
cmd += f"'https://{server}.gofile.io/uploadFile'"
upload_cmd = shlex.split(cmd)
try:
out = subprocess.check_output(upload_cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
raise Exception(e)
os.remove(file)
out = out.decode("UTF-8").strip()
print(out)
if out:
out = out.split("\n")[-1]
try:
response = json.loads(out)
except:
raise Exception("API Error (Not Vaild JSON Data Received)")
if not response:
raise Exception("API Error (No JSON Data Received)")
else:
raise Exception("API Error (No Data Received)")
if response["status"] == "ok":
data = response["data"]
data["directLink"] = f"https://{server}.gofile.io/download/{data['fileId']}/{data['fileName']}"
return data
elif "error-" in response["status"]:
error = response["status"].split("-")[1]
raise Exception(error)