File size: 1,445 Bytes
c82f7c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52

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