Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
CodiMD export script
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
stgroup
misc
CodiMD export script
Compare revisions
7f4d0cafaf0316d827fe17ca690ef79b65c478cb to c0ab6f2feeaf4db0abc26f96d684b8261c72a68e
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
stgroup/misc/codimd-export-script
Select target project
No results found
c0ab6f2feeaf4db0abc26f96d684b8261c72a68e
Select Git revision
Branches
main
1 result
Swap
Target
stgroup/misc/codimd-export-script
Select target project
stgroup/misc/codimd-export-script
1 result
7f4d0cafaf0316d827fe17ca690ef79b65c478cb
Select Git revision
Branches
main
1 result
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (3)
Add info statement to hang on while fetching documents
· 9667a17e
Andreas Domanowski
authored
2 years ago
9667a17e
Change URLs to our instances
· 0bf70bcc
Andreas Domanowski
authored
2 years ago
0bf70bcc
Remove accessibility check
· c0ab6f2f
Andreas Domanowski
authored
2 years ago
c0ab6f2f
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
common.py
+19
-11
19 additions, 11 deletions
common.py
export_md/codimd_export.py
+2
-2
2 additions, 2 deletions
export_md/codimd_export.py
import_md/hedgedoc_import.py
+6
-7
6 additions, 7 deletions
import_md/hedgedoc_import.py
md-import-export.py
+2
-2
2 additions, 2 deletions
md-import-export.py
with
29 additions
and
22 deletions
common.py
View file @
c0ab6f2f
import
json
import
sys
import
urllib.request
from
urllib.error
import
HTTPError
if
not
sys
.
platform
.
startswith
(
"
win
"
):
from
getpass
import
getpass
...
...
@@ -32,14 +33,21 @@ def print_block_heading(message):
print
(
separator
)
def
check_accessibility
(
instance_url
,
session_id
,
cookie_key
):
request_url
=
instance_url
+
'
/me/
'
headers
=
{
"
Cookie
"
:
f
"
{
cookie_key
}
=
{
session_id
}
"
}
#def check_accessibility(instance_url, session_id, cookie_key):
# request_url = instance_url + '/me/'
# headers = {"Cookie": f"{cookie_key}={session_id}"}
# try:
# 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_no_connection_error(request_url)
# print(f"Could access protected resources at {instance_url}. Proceeding...")
# except HTTPError as error:
# print(f"HTTP {error.status} {error.reason}")
# raise_no_connection_error(request_url)
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
"
:
def
raise_no_connection_error
(
request_url
):
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...
"
)
This diff is collapsed.
Click to expand it.
export_md/codimd_export.py
View file @
c0ab6f2f
...
...
@@ -10,7 +10,7 @@ from urllib.parse import quote
from
urllib.request
import
Request
,
urlopen
import
sys
from
common
import
get_sessionid
,
check_accessibility
,
print_block_heading
from
common
import
get_sessionid
,
print_block_heading
def
slurp
(
url
,
session_id
):
...
...
@@ -31,7 +31,6 @@ def prepare_target_dir(pathname):
def
export_from_codimd
(
instance_url
,
session_id
,
export_to
):
check_accessibility
(
instance_url
,
session_id
,
"
connect.sid
"
)
"""
Retrieve CodiMD document history and try to download each document.
"""
print_block_heading
(
f
"
Trying to fetch history (
{
instance_url
}
)
"
)
try
:
...
...
@@ -44,6 +43,7 @@ def export_from_codimd(instance_url, session_id, 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
"
)
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
)
}
"
...
...
This diff is collapsed.
Click to expand it.
import_md/hedgedoc_import.py
View file @
c0ab6f2f
#!/usr/bin/env python3
import
json
from
urllib.error
import
HTTPError
from
common
import
get_sessionid
,
check_accessibility
,
print_block_heading
from
common
import
get_sessionid
,
print_block_heading
from
pathlib
import
Path
import
urllib.parse
import
urllib.request
...
...
@@ -30,9 +30,8 @@ def check_file_exists(file):
def
import_into_hedgedoc
(
instance_url
,
session_id
,
export_folder
,
archive_file
):
print_block_heading
(
f
"
Checking existence of archive file (
{
archive_file
}
)
exists and authorization at
{
instance_url
}
"
)
f
"
Checking existence of archive file (
{
archive_file
}
)
"
)
check_file_exists
(
archive_file
)
check_accessibility
(
instance_url
,
session_id
,
"
connect.hedgeDoc.sid
"
)
# get exported history map
with
open
(
os
.
path
.
join
(
export_folder
,
"
history.json
"
))
as
map_file
:
history_dictionary
=
json
.
load
(
map_file
)
...
...
@@ -45,15 +44,15 @@ def import_into_hedgedoc(instance_url, session_id, export_folder, archive_file):
# URLs to visit to make the new document available in the history
urls_to_visit
=
[]
process_archive_export
(
archive_file
,
instance_url
,
lookup_map
,
session_id
,
urls_to_visit
)
create_urls_to_visit_file
(
"
history_scripts/hedgedoc_documents_to_visit.url
"
,
urls_to_visit
)
#
process_archive_export(archive_file, instance_url, lookup_map, session_id, urls_to_visit)
#
create_urls_to_visit_file("history_scripts/hedgedoc_documents_to_visit.url", urls_to_visit)
def
process_archive_export
(
archive_file
,
instance_url
,
lookup_map
,
session_id
,
urls_to_visit
):
# iterate over files in archive
with
ZipFile
(
archive_file
)
as
zf
:
print
(
"
Now scanning your provided archive file containing the documents you are the owner of
"
)
print
(
"
If you visited your own document via a, e.g.,
\"
codi-instance.tld/my_
own_path
\"
)
"
+
print
(
"
If you visited your own document via a, e.g.,
\"
codi-instance.tld/my_
specified_free_url
\"
"
+
"
this script tries to migrate it to the HedgeDoc instance at
\"
hedgedoc-instance.tld/my_own_path
\"
"
)
print
(
"
If this is not possible, a new random URL for the document will be created
"
)
for
file
in
zf
.
namelist
():
...
...
@@ -65,7 +64,7 @@ def process_archive_export(archive_file, instance_url, lookup_map, session_id, u
if
document_title
in
lookup_map
:
print
(
f
"
You visited your own document
\"
{
document_title
}
\"
.md
)
via the identifier/path
"
+
f
"
You visited your own document
\"
{
document_title
}
.md
\"
via the identifier/path
"
+
f
"
\"
{
lookup_map
[
document_title
]
}
\"
"
)
print
(
f
"
Trying to migrate this document and make it available under the already visited path
"
)
try
:
...
...
This diff is collapsed.
Click to expand it.
md-import-export.py
View file @
c0ab6f2f
...
...
@@ -7,8 +7,8 @@ if __name__ == "__main__":
export_archive
=
"
archive.zip
"
print_block_heading
(
"
Beginning export from CodiMD...
"
)
export_from_codimd
(
"
http://
localhost:3001
"
,
get_sessionid
(
"
CodiMD
"
,
"
connect.sid
"
),
export_folder
)
export_from_codimd
(
"
http://
md.inf.tu-dresden.de
"
,
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
"
),
import_into_hedgedoc
(
"
http://
md.inf.tu-dresden.de/notes
"
,
get_sessionid
(
"
HedgeDoc
"
,
"
connect.hedgeDoc.sid
"
),
export_folder
,
export_archive
)
This diff is collapsed.
Click to expand it.