|
import json |
|
import time |
|
|
|
import requests as rq |
|
|
|
base_url = "https://v2.doc2x.noedgeai.com" |
|
|
|
|
|
def pre_upload(secret: str): |
|
url = f"{base_url}/api/v2/parse/preupload" |
|
headers = { |
|
"Authorization": f"Bearer {secret}" |
|
} |
|
res = rq.post(url, headers=headers) |
|
if res.status_code == 200: |
|
data = res.json() |
|
if data["code"] == "success": |
|
return data["data"] |
|
else: |
|
raise Exception(f"get preupload url failed: {data}") |
|
else: |
|
raise Exception(f"get preupload url failed: {res.text}") |
|
|
|
|
|
def put_file(path: str, url: str): |
|
with open(path, "rb") as f: |
|
res = rq.put(url, data=f) |
|
if res.status_code != 200: |
|
raise Exception(f"put file failed: {res.text}") |
|
|
|
|
|
def get_status(uid: str, secret: str): |
|
url = f"{base_url}/api/v2/parse/status?uid={uid}" |
|
headers = { |
|
"Authorization": f"Bearer {secret}" |
|
} |
|
res = rq.get(url, headers=headers) |
|
if res.status_code == 200: |
|
data = res.json() |
|
if data["code"] == "success": |
|
return data["data"] |
|
else: |
|
raise Exception(f"get status failed: {data}") |
|
else: |
|
raise Exception(f"get status failed: {res.text}") |
|
|
|
|
|
def get_md(uid: str, secret: str, trigger: bool = False): |
|
headers = { |
|
"Authorization": f"Bearer {secret}", |
|
"Content-Type": "application/json", |
|
} |
|
|
|
data = { |
|
"uid": uid, |
|
"to": "md", |
|
"formula_mode": "dollar", |
|
"filename": "origin", |
|
} |
|
|
|
if trigger: |
|
url = f"{base_url}/api/v2/convert/parse" |
|
res = rq.post(url, headers=headers, data=json.dumps(data)) |
|
else: |
|
url = f"{base_url}/api/v2/convert/parse/result?uid={uid}" |
|
res = rq.get(url, headers=headers) |
|
|
|
if res.status_code == 200: |
|
data = res.json() |
|
if data["code"] == "success": |
|
return data["data"] |
|
else: |
|
raise Exception(f"get status failed: {data}") |
|
else: |
|
raise Exception(f"get status failed: {res.text}") |
|
|