Skip to content
Snippets Groups Projects

Hedgedoc import

Merged Andreas Domanowski requested to merge hedgedoc-import into main
Files
7
@@ -4,17 +4,13 @@ Save all Markdown documents in your CodiMD history to a local directory.
"""
import json
import sys
from pathlib import Path
from urllib.error import HTTPError
from urllib.parse import quote
from urllib.request import Request, urlopen
import sys
if not sys.platform.startswith("win"):
from getpass import getpass
else:
# on Windows, Ctrl-V doesn't work with getpass()
getpass = input
from common import get_sessionid, print_block_heading
def slurp(url, session_id):
@@ -28,21 +24,28 @@ def prepare_target_dir(pathname):
"""Create the directory to dump documents to, but refuse to override an existing one."""
target_dir = Path(pathname)
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()
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."""
print_block_heading(f"Trying to fetch history ({instance_url})")
try:
data = json.loads(slurp(f"{instance_url}/history", session_id))
except OSError as error:
raise SystemExit(f"error: couldn't access the /history endpoint: {error}")
except json.JSONDecodeError as 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)
num_ok = num_fail = 0
print_block_heading(f"Accessing history and trying to fetch each document")
with open(Path(target_dir, f"history.json"), mode="w") as stream:
json.dump(data, stream)
print("Hold on, this may take a while...")
for row in data["history"]:
document_id = row["id"]
document_url = f"{instance_url}/{quote(document_id)}"
@@ -59,13 +62,5 @@ def main(instance_url, session_id, export_to):
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__":
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