From 4166a64e9642afc9b90ff72973bfcc4b18a7dfd2 Mon Sep 17 00:00:00 2001
From: Andreas Domanowski <andreas@domanowski.net>
Date: Wed, 1 Mar 2023 14:31:37 +0100
Subject: [PATCH] Move import and export scripts to explicit locations

---
 .../codimd_export.py                          |  2 +-
 .../hedgedoc_import.py                        | 46 ++++---------------
 md-import-export.py                           |  8 ++--
 3 files changed, 13 insertions(+), 43 deletions(-)
 rename codimd_export.py => export_md/codimd_export.py (97%)
 rename hedgedoc_import.py => import_md/hedgedoc_import.py (73%)

diff --git a/codimd_export.py b/export_md/codimd_export.py
similarity index 97%
rename from codimd_export.py
rename to export_md/codimd_export.py
index dbb3c25..6f8b158 100755
--- a/codimd_export.py
+++ b/export_md/codimd_export.py
@@ -59,4 +59,4 @@ def export_from_codimd(instance_url, session_id, export_to):
 
 
 if __name__ == "__main__":
-    export_from_codimd("https://md.inf.tu-dresden.de", get_sessionid("connect.sid"), "codimd-documents")
+    export_from_codimd("https://md.inf.tu-dresden.de", get_sessionid("CodiMD", "connect.sid"), "codimd-documents")
diff --git a/hedgedoc_import.py b/import_md/hedgedoc_import.py
similarity index 73%
rename from hedgedoc_import.py
rename to import_md/hedgedoc_import.py
index f8dff84..b99b9fc 100644
--- a/hedgedoc_import.py
+++ b/import_md/hedgedoc_import.py
@@ -41,11 +41,9 @@ def check_archive_exists(archive_file):
 
 
 def import_into_hedgedoc(instance_url, session_id, export_folder, archive_file):
-    print("Beginning import")
+    print("Beginning import_md")
     check_archive_exists(archive_file)
     check_accessibility(instance_url, session_id)
-    # let user select browser
-    browser = select_browser()
     # get exported history map
     with open(os.path.join(export_folder, "history.json")) as map_file:
         history_dictionary = json.load(map_file)
@@ -59,8 +57,7 @@ def import_into_hedgedoc(instance_url, session_id, export_folder, archive_file):
     urls_to_visit = []
 
     iterate_over_archive(archive_file, instance_url, lookup_map, session_id, urls_to_visit)
-    visit_urls_in_browser(browser, urls_to_visit)
-    create_urls_to_visit_file("hedgedoc_documents_to_visit.url", urls_to_visit)
+    create_urls_to_visit_file("../hedgedoc_documents_to_visit.url", urls_to_visit)
 
 
 def iterate_over_archive(archive_file, instance_url, lookup_map, session_id, urls_to_visit):
@@ -104,43 +101,16 @@ def iterate_over_archive(archive_file, instance_url, lookup_map, session_id, url
                 urls_to_visit.append(generated_url)
 
 
-def visit_urls_in_browser(browser, urls_to_visit):
-    print("Your specified browser now needs to visit every newly created document to ensure that it's available in"
-          "your history in HedgeDoc")
-    subprocess.run([browser] + urls_to_visit)
-
-
 def create_urls_to_visit_file(filename, urls_to_visit):
+    print(f"A new file {filename} will be created. It contains all URLs you need to visit in order to make the"
+          f"migrated documents appear in your HedgeDoc history.")
+    print("This can be automated by running the scripts in the directory \"history_scripts\"")
+    print("BE AWARE: Opening a lot of tabs might be quite resource-intensive.")
     with open(filename, 'w') as f:
         for url in urls_to_visit:
             f.write(url + "\n")
-        print(
-            "In preparation for the case that this did not work, all URLs are saved in the file \"hedgedoc_documents_to_visit.url\"")
-
-
-def select_browser():
-    print("Once you've uploaded all your documents, they unfortunately do not appear in your HedgeDoc history.")
-    print("To make sure that they are available to you, this script automatically visits all your newly uploaded "
-          "documents in your browser.")
-    print("Therefore, you need to specify your browser. It needs to be on your path with the same name as "
-          "specified here")
-    print("ATTENTION - this needs to be a browser where you have an active and logged-in HedgeDoc session")
-    supported_browsers = ["firefox", "opera", "safari", "google-chrome", "chromium"]
-    user_input = ''
-    input_message = "Specify a browser which holds an active and logged-in HedgeDoc session:\n"
-
-    for i, browser_suggestion in enumerate(supported_browsers):
-        input_message += f'{i + 1}) {browser_suggestion}\n'
-
-    # input_message += 'Your choice: '
-
-    while user_input not in map(str, range(1, len(supported_browsers) + 1)):
-        user_input = input(input_message)
-
-    print('You chose: ' + supported_browsers[int(user_input) - 1])
-    return supported_browsers[int(user_input) - 1]
 
 
 if __name__ == "__main__":
-    import_into_hedgedoc("https://md.inf.tu-dresden.de/notes", get_sessionid("connect.hedgeDoc.sid"),
-                         "codimd-documents", "archive.zip")
+    import_into_hedgedoc("https://md.inf.tu-dresden.de/notes", get_sessionid("HedgeDoc", "connect.hedgeDoc.sid"),
+                         "codimd-documents", "../archive.zip")
diff --git a/md-import-export.py b/md-import-export.py
index 233a546..1e1423e 100644
--- a/md-import-export.py
+++ b/md-import-export.py
@@ -1,10 +1,10 @@
-from codimd_export import export_from_codimd
+from export_md.codimd_export import export_from_codimd
+from import_md.hedgedoc_import import import_into_hedgedoc
 from common import get_sessionid
-from hedgedoc_import import import_into_hedgedoc
 
 if __name__ == "__main__":
     export_folder = "codimd-documents"
     export_archive = "archive.zip"
-    export_from_codimd("https://md.inf.tu-dresden.de/", get_sessionid("CodiMD", "connect.sid"), export_folder)
-    import_into_hedgedoc("https://md.inf.tu-dresden.de/notes", get_sessionid("HedgeDoc", "connect.hedgeDoc.sid"),
+    export_from_codimd("http://localhost:3001", get_sessionid("CodiMD", "connect.sid"), export_folder)
+    import_into_hedgedoc("http://hedgedoc:3000", get_sessionid("HedgeDoc", "connect.hedgeDoc.sid"),
                          export_folder, export_archive)
-- 
GitLab