|
34 | 34 |
|
35 | 35 | HTTP_SERVICE_UNAVAILABLE = 503
|
36 | 36 |
|
| 37 | +REMARKABLE_USB_URL = "http://10.11.99.1" |
| 38 | + |
37 | 39 | logger = Logger()
|
38 | 40 |
|
39 | 41 |
|
@@ -156,7 +158,9 @@ def follow_redirects(url):
|
156 | 158 | return url, jar
|
157 | 159 |
|
158 | 160 |
|
159 |
| -def upload_to_remarkable(filepath, remarkable_dir="/", rmapi_path="rmapi"): |
| 161 | +def upload_to_remarkable_rmapi( |
| 162 | + filepath, remarkable_dir="/", rmapi_path="rmapi" |
| 163 | +): |
160 | 164 | logger.info("Starting upload to reMarkable")
|
161 | 165 |
|
162 | 166 | # Create the reMarkable dir if it doesn't exist
|
@@ -188,6 +192,63 @@ def upload_to_remarkable(filepath, remarkable_dir="/", rmapi_path="rmapi"):
|
188 | 192 | logger.info("Upload successful.")
|
189 | 193 |
|
190 | 194 |
|
| 195 | +def list_usb_dir(guid): |
| 196 | + url = REMARKABLE_USB_URL + "/documents/" |
| 197 | + if guid: |
| 198 | + url += guid + "/" |
| 199 | + response = requests.post(url) |
| 200 | + return response.json() |
| 201 | + |
| 202 | + |
| 203 | +def crawl_usb_dirs(remarkable_dir): |
| 204 | + parts = [p for p in remarkable_dir.split("/") if p] |
| 205 | + guid = None |
| 206 | + while len(parts) > 0: |
| 207 | + part = parts.pop(0) |
| 208 | + data = list_usb_dir(guid) |
| 209 | + guid = None |
| 210 | + for item in data: |
| 211 | + if item["VissibleName"] == part: |
| 212 | + guid = item["ID"] |
| 213 | + if not guid: |
| 214 | + raise RemarkableError( |
| 215 | + "Directory %s does not exist on reMarkable" % remarkable_dir |
| 216 | + ) |
| 217 | + # necessary because the last listed dir is the one uploaded to |
| 218 | + list_usb_dir(guid) |
| 219 | + |
| 220 | + |
| 221 | +def upload_to_remarkable_usb(filepath, remarkable_dir="/"): |
| 222 | + # https://remarkable.guide/tech/usb-web-interface.html |
| 223 | + logger.info("Starting upload to reMarkable") |
| 224 | + |
| 225 | + # Check if the remarkable dir exists (no creation through usb api) |
| 226 | + crawl_usb_dirs(remarkable_dir) |
| 227 | + |
| 228 | + headers = { |
| 229 | + "Origin": REMARKABLE_USB_URL, |
| 230 | + "Accept": "*/*", |
| 231 | + "Referer": REMARKABLE_USB_URL + "/", |
| 232 | + "Connection": "keep-alive", |
| 233 | + } |
| 234 | + response = requests.post( |
| 235 | + REMARKABLE_USB_URL + "/upload", |
| 236 | + files={ |
| 237 | + "file": ( |
| 238 | + os.path.basename(filepath), |
| 239 | + open(filepath, "rb"), |
| 240 | + "application/pdf", |
| 241 | + ) |
| 242 | + }, |
| 243 | + headers=headers, |
| 244 | + ) |
| 245 | + if not response.ok: |
| 246 | + raise RemarkableError( |
| 247 | + "Uploading file %s to reMarkable failed" % filepath |
| 248 | + ) |
| 249 | + logger.info("Upload successful.") |
| 250 | + |
| 251 | + |
191 | 252 | def is_url(string):
|
192 | 253 | """Check if the string is a valid URL
|
193 | 254 |
|
|
0 commit comments