-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrmathlab.py
More file actions
147 lines (101 loc) · 4.03 KB
/
Copy pathrmathlab.py
File metadata and controls
147 lines (101 loc) · 4.03 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/python3
import os
import zipfile
import argparse
from pathlib import Path
from config import *
from remarkable import *
from mathpix import *
# Path of the Python script (Directory of this file)
DIR = os.path.dirname(os.path.abspath(__file__))
#print("1: ", DIR)
# Path from which you execute the Python script (The current working directory)
CURWORK_DIR = os.getcwd()
#print("2: ", CURWORK_DIR)
APP_NAME = "rmathlab"
HOME_DIR = Path.home()
APP_DIR = f"{HOME_DIR}/.{APP_NAME}"
RM_DIR_REMOTE = "/home/root/.local/share/remarkable/xochitl"
METADATA_SUBDIR = "tmp_metadata"
BASE_URL = "https://api.mathpix.com/v3" # "https://api.mathpix.com/v3/pdf"
parser = argparse.ArgumentParser(
prog = f"{APP_NAME}", description = "Send file from your Remarkable tablet to Mathpix and receive tex, mmd, docx, html, lines.json, lines.mmd.json (default: tex)",
)
parser.add_argument(
"filename", type=str, default="", help="The name of the file you want to send to Mathpix (mandatory argument)"
)
parser.add_argument(
"-l", "--local", action="store_true", help="Pick local file. Don't fetch from the Remarkable tablet."
)
parser.add_argument(
"-f", "--fileformat", type=str, default="tex", help="Output file format: tex, mmd, docx, html, lines.json, lines.mmd.json (default: tex)"
)
parser.add_argument(
"-out", "--outputfilename", type=str, default="", help="The name of the output file (default: same like filename)"
)
parser.add_argument(
"-cdir", "--appdir", type=str, default=f"{APP_DIR}", help=f"Path of the app directory. Containing the config file and temporary data. (default: {APP_DIR})"
)
parser.add_argument(
"-c", "--configfile", type=str, default="config.ini", help="Name of your configfile in your rm-mathrec directory (default: config.ini)"
)
parser.add_argument(
"-v", "--verbose", action="store_true", help="Print executed shell commands"
)
#parser.add_argument("name", type=str, nargs="?", default="remarkable", help="SSH hostname of reMarkable reachable with \"ssh [name]\" without password (default: remarkable)") # TODO
if __name__ == "__main__":
"""
# Read configs
"""
args = parser.parse_args()
filename = getattr(args, "filename")
outputfilename = getattr(args, "outputfilename")
appdir = getattr(args, "appdir")
configfilename = getattr(args, "configfile")
fileFormat = getattr(args, "fileformat") # tex, mmd, docx, html, lines.json, lines.mmd.json, default: tex
filename = os.path.splitext(filename)[0]
if outputfilename == "":
outputfilename = f"{filename}"
outputfilename = f"{CURWORK_DIR}/{outputfilename}"
try:
config = readConfig(appdir, configfilename)
#print(config)
fileLocal = args.local
"""
# Remarkable
"""
metadataDirLocal = f"{appdir}/{METADATA_SUBDIR}"
#metadataDirLocal = os.path.abspath(f"{METADATA_SUBDIR}")
if fileLocal == False:
#checkSSHConnection(config["SSH_NAME"]) # TODO
downloadMetadata(args, config["SSH_NAME"], RM_DIR_REMOTE, metadataDirLocal)
uuid = getUUIDByFilename(metadataDirLocal, f"{filename}")
if uuid == None:
print(f"Couldn't find filename: {filename}")
raise
#else:
# print(uuid)
downloadPdf = downloadPdfFromRM(uuid, f"{CURWORK_DIR}/{filename}")
"""
# Mathpix
"""
if fileLocal or downloadPdf:
headers = { "APP_ID": config["APP_ID"], "APP_KEY": config["APP_KEY"] }
#print(headers)
pdffilename = f"{CURWORK_DIR}/{os.path.splitext(filename)[0]}.pdf"
file = getFile(pdffilename)
pdf_id = sendFileToMathpix(headers, BASE_URL, file)
if fileFormat == "tex":
outputPath = f"{outputfilename}.zip" # .tex.zip
else:
outputPath = f"{outputfilename}.{fileFormat}"
if pdf_id and waitForMathpix(headers, BASE_URL, pdf_id):
downloadFileFromMathpix(headers, BASE_URL, pdf_id, fileFormat, outputPath)
if fileFormat == "tex":
with zipfile.ZipFile(outputPath, 'r') as zipRef:
name = os.path.splitext(outputPath)[0]
zipRef.extractall(name)
# Remove .zip folder
os.remove(outputPath)
except:
print("An unexpected error occured")