Skip to content
Snippets Groups Projects
Commit b622ec75 authored by Andreas Domanowski's avatar Andreas Domanowski
Browse files

Add cli frontend

parent 0167600e
No related branches found
No related tags found
1 merge request!1Hedgedoc import
...@@ -8,6 +8,7 @@ from pathlib import Path ...@@ -8,6 +8,7 @@ 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
from common import get_sessionid from common import get_sessionid
...@@ -28,7 +29,7 @@ def prepare_target_dir(pathname): ...@@ -28,7 +29,7 @@ def prepare_target_dir(pathname):
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."""
try: try:
data = json.loads(slurp(f"{instance_url}/history", session_id)) data = json.loads(slurp(f"{instance_url}/history", session_id))
...@@ -45,6 +46,8 @@ def main(instance_url, session_id, export_to): ...@@ -45,6 +46,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"map.map"), 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
...@@ -55,4 +58,4 @@ def main(instance_url, session_id, export_to): ...@@ -55,4 +58,4 @@ def main(instance_url, session_id, export_to):
if __name__ == "__main__": if __name__ == "__main__":
main("https://md.inf.tu-dresden.de", get_sessionid("connect.sid"), "codimd-documents") export_from_codimd("https://md.inf.tu-dresden.de", get_sessionid("connect.sid"), "codimd-documents")
#!/usr/bin/env python3 #!/usr/bin/env python3
from common import get_sessionid
from pathlib import Path
import urllib.parse import urllib.parse
import urllib.request import urllib.request
import os import os
from pathlib import Path import subprocess
from zipfile import ZipFile
from common import get_sessionid
def import_document(instance_url, hedgedoc_free_url, content, session_id): def import_single_document(instance_url, hedgedoc_free_url, content, session_id):
sanitized_free_url = hedgedoc_free_url.replace(" ", "%20") sanitized_free_url = hedgedoc_free_url.replace(" ", "%20")
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
...@@ -22,18 +23,43 @@ def import_document(instance_url, hedgedoc_free_url, content, session_id): ...@@ -22,18 +23,43 @@ def import_document(instance_url, hedgedoc_free_url, content, session_id):
print("Go visit " + response.url + " with your browser in a logged-in session.") print("Go visit " + response.url + " with your browser in a logged-in session.")
def main(instance_url, session_id, import_from): def import_into_hedgedoc(instance_url, session_id, export_folder, archive_file):
folder = os.path.join(os.getcwd(), import_from); folder = os.path.join(os.getcwd(), export_folder);
print("Trying to upload all files with extension \".md\" in folder \"" + folder + "\""); print("Trying to upload all files with extension \".md\" in folder \"" + folder + "\"");
#with ZipFile('images.zip') as zf:
# for file in zf.namelist():
# if not file.endswith('.png'): # optional filtering by filetype
# continue
# with zf.open(file) as f:
# image = pygame.image.load(f, namehint=file)
for relative_filename in os.listdir(folder): for relative_filename in os.listdir(folder):
if relative_filename.endswith(".md"): if relative_filename.endswith(".md"):
addressable_filename = os.path.join(folder, relative_filename) addressable_filename = os.path.join(folder, relative_filename)
print("Trying to upload: " + addressable_filename) print("Trying to upload: " + addressable_filename)
markdown_content = Path(addressable_filename).read_text() markdown_content = Path(addressable_filename).read_text()
free_url = Path(relative_filename).stem free_url = Path(relative_filename).stem
import_document(instance_url, free_url, markdown_content, session_id) import_single_document(instance_url, free_url, markdown_content, session_id)
def select_browser():
options = ['chrome', 'firefox', 'opera']
user_input = ''
input_message = "Pick an option:\n"
for index, item in enumerate(options):
input_message += f'{index + 1}) {item}\n'
input_message += 'Your choice: '
while user_input not in map(str, range(1, len(options) + 1)):
user_input = input(input_message)
print('You picked: ' + options[int(user_input) - 1])
if __name__ == "__main__": if __name__ == "__main__":
main("https://md.inf.tu-dresden.de/notes", get_sessionid("connect.hedgeDoc.sid"), "codimd-documents") select_browser()
#subprocess.run(["firefox", "www.google.de"])
#import_into_hedgedoc("https://md.inf.tu-dresden.de/notes", get_sessionid("connect.hedgeDoc.sid"), "codimd-documents")
from codimd_export import export_from_codimd
from common import get_sessionid
from hedgedoc_import import import_into_hedgedoc
if __name__ == "__main__":
export_from_codimd("https://md.inf.tu-dresden.de", get_sessionid("connect.sid"), "codimd-documents")
import_into_hedgedoc("https://md.inf.tu-dresden.de/notes", get_sessionid("connect.hedgeDoc.sid"), "codimd-documents", "archive.zip")
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment