Convert PDF to reMarkable notebook, then send it to rM device
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
# <h1>Table of Contents<span class="tocSkip"></span></h1> | |
# <div class="toc"><ul class="toc-item"></ul></div> | |
# In[36]: | |
import os | |
import shutil | |
import argparse | |
# In[ ]: | |
parser = argparse.ArgumentParser(description='Backup reMarkable docs as PDFs.') | |
parser.add_argument("-i", dest="pdf", nargs=1, | |
help="input PDF file") | |
parser.add_argument('-s', dest='source', nargs="?", | |
const="10.11.99.1", default="10.11.99.1", | |
help='IP address of a reMarkable device') | |
args = parser.parse_args() | |
pdf = str(args.pdf[0]) | |
print(pdf) | |
pdf_name = os.path.basename(pdf)[:-4] | |
remarkable = args.source | |
if os.path.exists("_temp"): | |
raise OSError("'_temp' folder already exists. Please check.") | |
shutil.rmtree("_temp") | |
print(f" [ Converting PDF to rM notebook ]: {pdf_name}") | |
# use gnu-sed if f-something parse error occurs | |
# os.system('PATH="/opt/homebrew/opt/gnu-sed/libexec/gnubin:$PATH"') | |
os.system(f"./pdf2rmnotebook.sh {pdf}") # from https://github.com/JCN-9000/pdf2rmnotebook/blob/main/pdf2rmnotebook.sh | |
os.system(f"unzip Notebook.zip -d ./_temp") | |
uuid = [f for f in os.listdir("./_temp") if f.endswith("content")][0].split(".")[0] | |
with open(f"./_temp/{uuid}.metadata", "w") as w: | |
w.write('''{ | |
"deleted": false, | |
"lastModified": "1638094674918", | |
"lastOpened": "1638438026678", | |
"lastOpenedPage": 0, | |
"metadatamodified": false, | |
"modified": false, | |
"parent": "", | |
"pinned": false, | |
"synced": false, | |
"type": "DocumentType", | |
"version": 1, | |
"visibleName": "[[pdf_name]]" | |
} | |
'''.replace("[[pdf_name]]", pdf_name)) | |
print(f" [ Sending notebook ]: {uuid}") | |
os.system(f"scp -o PasswordAuthentication=no\ | |
-i ~/.ssh/id_rsa_remarkable\ | |
-o PubkeyAcceptedKeyTypes=+ssh-rsa\ | |
-o HostKeyAlgorithms=+ssh-rsa\ | |
-r ./_temp/* root@{remarkable}:~/.local/share/remarkable/xochitl/") | |
print(" [ Restarting xochitl ]") | |
os.system(f"ssh -o PasswordAuthentication=no\ | |
-i ~/.ssh/id_rsa_remarkable\ | |
-o PubkeyAcceptedKeyTypes=+ssh-rsa\ | |
-o HostKeyAlgorithms=+ssh-rsa\ | |
root@{remarkable} systemctl restart xochitl") | |
print(" [ Cleaning up temp files ]") | |
shutil.rmtree("_temp") | |
os.remove("Notebook.zip") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment