Skip to content
Snippets Groups Projects

Hedgedoc import

Merged Andreas Domanowski requested to merge hedgedoc-import into main
Files
6
@@ -4,17 +4,13 @@ Save all Markdown documents in your CodiMD history to a local directory.
@@ -4,17 +4,13 @@ Save all Markdown documents in your CodiMD history to a local directory.
"""
"""
import json
import json
import sys
from pathlib import Path
from pathlib import Path
from urllib.error import HTTPError
from urllib.error import HTTPError
from urllib.parse import quote
from urllib.parse import quote
from urllib.request import Request, urlopen
from urllib.request import Request, urlopen
 
import sys
if not sys.platform.startswith("win"):
from common import get_sessionid, print_block_heading
from getpass import getpass
else:
# on Windows, Ctrl-V doesn't work with getpass()
getpass = input
def slurp(url, session_id):
def slurp(url, session_id):
@@ -28,21 +24,26 @@ def prepare_target_dir(pathname):
@@ -28,21 +24,26 @@ def prepare_target_dir(pathname):
"""Create the directory to dump documents to, but refuse to override an existing one."""
"""Create the directory to dump documents to, but refuse to override an existing one."""
target_dir = Path(pathname)
target_dir = Path(pathname)
if target_dir.exists():
if target_dir.exists():
raise SystemExit(f"error: the target directory {target_dir} already exists")
raise SystemExit(f"ERROR: the target directory {target_dir} already exists. Delete it, then re-execute this "
 
f"script")
target_dir.mkdir()
target_dir.mkdir()
return target_dir
return target_dir
def main(instance_url, session_id, export_to):
def export_from_codimd(instance_url, session_id, export_to):
"""Retrieve CodiMD document history and try to download each document."""
"""Retrieve CodiMD document history and try to download each document."""
 
print_block_heading(f"Trying to fetch history ({instance_url})")
try:
try:
data = json.loads(slurp(f"{instance_url}/history", session_id))
data = json.loads(slurp(f"{instance_url}/history", session_id))
except OSError as error:
except OSError as error:
raise SystemExit(f"error: couldn't access the /history endpoint: {error}")
raise SystemExit(f"error: couldn't access the /history endpoint: {error}")
except json.JSONDecodeError as error:
except json.JSONDecodeError as error:
raise SystemExit(f"error: received malformed JSON: {error}")
raise SystemExit(f"error: received malformed JSON: {error}")
 
print_block_heading(f"Preparing target directory ({export_to})")
target_dir = prepare_target_dir(export_to)
target_dir = prepare_target_dir(export_to)
num_ok = num_fail = 0
num_ok = num_fail = 0
 
print_block_heading(f"Accessing history and trying to fetch each document")
 
print("Hold on, this may take a while...")
for row in data["history"]:
for row in data["history"]:
document_id = row["id"]
document_id = row["id"]
document_url = f"{instance_url}/{quote(document_id)}"
document_url = f"{instance_url}/{quote(document_id)}"
@@ -50,6 +51,8 @@ def main(instance_url, session_id, export_to):
@@ -50,6 +51,8 @@ def main(instance_url, session_id, export_to):
contents = slurp(f"{document_url}/download", session_id)
contents = slurp(f"{document_url}/download", session_id)
with open(Path(target_dir, f"{document_id}.md"), mode="wb") as stream:
with open(Path(target_dir, f"{document_id}.md"), mode="wb") as stream:
stream.write(contents)
stream.write(contents)
 
with open(Path(target_dir, f"history.json"), mode="w") as stream:
 
json.dump(data, stream)
num_ok += 1
num_ok += 1
except HTTPError as error:
except HTTPError as error:
# history might reference deleted or otherwise inaccessible notes
# history might reference deleted or otherwise inaccessible notes
@@ -59,13 +62,5 @@ def main(instance_url, session_id, export_to):
@@ -59,13 +62,5 @@ def main(instance_url, session_id, export_to):
print(f"Done: {num_ok} notes successfully downloaded, {num_fail} not accessible.")
print(f"Done: {num_ok} notes successfully downloaded, {num_fail} not accessible.")
def get_sessionid():
"""Ask the user for the session id, if it's not configured as an envvar."""
sid = getpass("Please provide your CodiMD session id (connect.sid cookie): ")
if sid.startswith("s%3A"):
return sid
raise SystemExit(f"error: the supplied session id seems to be malformed")
if __name__ == "__main__":
if __name__ == "__main__":
main("https://md.inf.tu-dresden.de", get_sessionid(), "codimd-documents")
export_from_codimd("https://md.inf.tu-dresden.de", get_sessionid("CodiMD", "connect.sid"), "codimd-documents")
Loading