diff --git a/common.py b/common.py index 95bb2700a922ebe8f735ecb2f67a93163738bdc0..dea5d9ddb3b99b706fe37e5722b724bda144dd30 100644 --- a/common.py +++ b/common.py @@ -1,4 +1,6 @@ +import json import sys +import urllib.request if not sys.platform.startswith("win"): from getpass import getpass @@ -21,3 +23,23 @@ def get_sessionid(service_name, cookie_key): if sid.startswith("s%3A"): return sid raise SystemExit(f"error: the supplied session id seems to be malformed") + + +def print_block_heading(message): + separator = "======================================================================================================" + print(separator) + print(message) + print(separator) + + +def check_accessibility(instance_url, session_id): + request_url = instance_url + '/me/' + headers = {"Cookie": f"connect.hedgeDoc.sid={session_id}"} + + req = urllib.request.Request(request_url, headers=headers) + with urllib.request.urlopen(req) as response: + response_json = json.load(response) + if response_json["status"] != "ok": + raise SystemExit(f"Could not access protected resources at {request_url}. Make sure that the specified " + f"cookie is correct. Aborting...") + print(f"Could access protected resources at {instance_url}. Proceeding...") diff --git a/export_md/codimd_export.py b/export_md/codimd_export.py index 6f8b158daa61ccd009363f7b34c1043b32003759..562ee452bcc5c13653a6696d5f454cd8fe9bd5ba 100755 --- a/export_md/codimd_export.py +++ b/export_md/codimd_export.py @@ -10,7 +10,7 @@ from urllib.parse import quote from urllib.request import Request, urlopen import sys -from common import get_sessionid +from common import get_sessionid, check_accessibility, print_block_heading def slurp(url, session_id): @@ -31,6 +31,7 @@ def prepare_target_dir(pathname): def export_from_codimd(instance_url, session_id, export_to): + check_accessibility(instance_url, session_id) """Retrieve CodiMD document history and try to download each document.""" try: data = json.loads(slurp(f"{instance_url}/history", session_id)) @@ -38,6 +39,7 @@ def export_from_codimd(instance_url, session_id, export_to): 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 for row in data["history"]: diff --git a/md-import-export.py b/md-import-export.py index 1e1423e30dff0f7227ceec3783edcd07c0fc72dc..ec6b7770f27eb7890dc950dc5d135c926b6f7f27 100644 --- a/md-import-export.py +++ b/md-import-export.py @@ -1,10 +1,14 @@ from export_md.codimd_export import export_from_codimd from import_md.hedgedoc_import import import_into_hedgedoc -from common import get_sessionid +from common import get_sessionid, print_block_heading if __name__ == "__main__": export_folder = "codimd-documents" export_archive = "archive.zip" + + print_block_heading("Beginning export from CodiMD...") export_from_codimd("http://localhost:3001", get_sessionid("CodiMD", "connect.sid"), export_folder) + + print_block_heading("Beginning import to HedgeDoc...") import_into_hedgedoc("http://hedgedoc:3000", get_sessionid("HedgeDoc", "connect.hedgeDoc.sid"), export_folder, export_archive)