Spaces:
Sleeping
Sleeping
| import requests | |
| import json | |
| class Converter: | |
| def __init__(self, app_id, app_key, options): | |
| self.options = options | |
| self.app_id = app_id | |
| self.app_key = app_key | |
| self.headers = { | |
| "app_id": app_id, | |
| "app_key": app_key | |
| } | |
| def convert(self, file_path): | |
| r = requests.post("https://api.mathpix.com/v3/pdf", | |
| headers=self.headers, | |
| data={ | |
| "options_json": json.dumps(self.options) | |
| }, | |
| files={ | |
| "file": open(file_path, "rb") | |
| } | |
| ) | |
| return r.json() | |
| def check_status(self, pdf_id): | |
| r = requests.get(f"https://api.mathpix.com/v3/pdf/{pdf_id}", | |
| headers=self.headers | |
| ) | |
| return r.json() | |
| def retrieve_converted_file(self, pdf_id): | |
| print(self.check_status(pdf_id)) | |
| while self.check_status(pdf_id)['status'] != "completed": | |
| pass | |
| url = "https://api.mathpix.com/v3/pdf/" + pdf_id + ".md" | |
| response = requests.get(url, headers=self.headers) | |
| with open(pdf_id + ".md", "w") as f: | |
| lines = response.text.splitlines() | |
| f.write("\n".join(lines[1:])) | |
| return response.text | |