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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
stgroup
misc
CodiMD export script
Commits
4138d828
Commit
4138d828
authored
2 years ago
by
Andreas Domanowski
Browse files
Options
Downloads
Patches
Plain Diff
Print further instructions and information to stdout
parent
e3356194
No related branches found
No related tags found
1 merge request
!1
Hedgedoc import
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
common.py
+2
-2
2 additions, 2 deletions
common.py
export_md/codimd_export.py
+3
-1
3 additions, 1 deletion
export_md/codimd_export.py
import_md/hedgedoc_import.py
+18
-26
18 additions, 26 deletions
import_md/hedgedoc_import.py
with
23 additions
and
29 deletions
common.py
+
2
−
2
View file @
4138d828
...
...
@@ -32,9 +32,9 @@ def print_block_heading(message):
print
(
separator
)
def
check_accessibility
(
instance_url
,
session_id
):
def
check_accessibility
(
instance_url
,
session_id
,
cookie_key
):
request_url
=
instance_url
+
'
/me/
'
headers
=
{
"
Cookie
"
:
f
"
co
nnect.hedgeDoc.sid
=
{
session_id
}
"
}
headers
=
{
"
Cookie
"
:
f
"
{
co
okie_key
}
=
{
session_id
}
"
}
req
=
urllib
.
request
.
Request
(
request_url
,
headers
=
headers
)
with
urllib
.
request
.
urlopen
(
req
)
as
response
:
...
...
This diff is collapsed.
Click to expand it.
export_md/codimd_export.py
+
3
−
1
View file @
4138d828
...
...
@@ -31,8 +31,9 @@ def prepare_target_dir(pathname):
def
export_from_codimd
(
instance_url
,
session_id
,
export_to
):
check_accessibility
(
instance_url
,
session_id
)
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
:
data
=
json
.
loads
(
slurp
(
f
"
{
instance_url
}
/history
"
,
session_id
))
except
OSError
as
error
:
...
...
@@ -42,6 +43,7 @@ def export_from_codimd(instance_url, session_id, export_to):
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
"
)
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
+
18
−
26
View file @
4138d828
#!/usr/bin/env python3
import
json
from
urllib.error
import
HTTPError
from
common
import
get_sessionid
from
common
import
get_sessionid
,
check_accessibility
,
print_block_heading
from
pathlib
import
Path
import
urllib.parse
import
urllib.request
import
os
import
subprocess
from
zipfile
import
ZipFile
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...
"
)
def
import_single_document
(
instance_url
,
hedgedoc_free_url
,
content
,
session_id
):
sanitized_free_url
=
hedgedoc_free_url
.
replace
(
"
"
,
"
%20
"
)
request_url
=
instance_url
+
'
/new/
'
+
sanitized_free_url
...
...
@@ -33,17 +20,19 @@ def import_single_document(instance_url, hedgedoc_free_url, content, session_id)
return
response
.
url
def
check_
archiv
e_exists
(
archive_
file
):
if
not
os
.
path
.
exists
(
archive_
file
):
def
check_
fil
e_exists
(
file
):
if
not
os
.
path
.
exists
(
file
):
raise
SystemExit
(
f
"
ERROR: File
{
archive_
file
}
does not exist. Export your data from CodiMD and re-execute this script again!
"
f
"
ERROR: File
{
file
}
does not exist. Export your data from CodiMD and re-execute this script again!
"
f
"
Aborting...
"
)
print
(
f
"
Required file
{
file
}
exists. Proceeding...
"
)
def
import_into_hedgedoc
(
instance_url
,
session_id
,
export_folder
,
archive_file
):
print
(
"
Beginning import_md
"
)
check_archive_exists
(
archive_file
)
check_accessibility
(
instance_url
,
session_id
)
print_block_heading
(
f
"
Checking existence of archive file (
{
archive_file
}
) exists and authorization at
{
instance_url
}
"
)
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
)
...
...
@@ -56,11 +45,11 @@ 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
=
[]
iterate_over
_archive
(
archive_file
,
instance_url
,
lookup_map
,
session_id
,
urls_to_visit
)
process
_archive
_export
(
archive_file
,
instance_url
,
lookup_map
,
session_id
,
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
):
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
"
)
...
...
@@ -76,21 +65,22 @@ def iterate_over_archive(archive_file, instance_url, lookup_map, session_id, url
if
document_title
in
lookup_map
:
print
(
f
"
\t
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
"
\t
Trying to migrate this document and make it available under the already visited path
"
)
print
(
f
"
Trying to migrate this document and make it available under the already visited path
"
)
try
:
new_url
=
import_single_document
(
instance_url
,
lookup_map
[
document_title
],
document_content
,
session_id
)
urls_to_visit
.
append
(
new_url
)
print
(
f
"
\t
Migration was possible. New URL:
{
instance_url
}
/
{
lookup_map
[
document_title
]
}
"
)
print
(
f
"
Migration was possible. New URL:
{
instance_url
}
/
{
lookup_map
[
document_title
]
}
"
)
except
HTTPError
as
error
:
if
error
.
status
==
409
:
print
(
"
\t
ATTENTION: Could not migrate document with the same path. Uploading anyways and
"
print
(
"
ATTENTION: Could not migrate document with the same path. Uploading anyways and
"
"
creating a new, random path
"
)
new_url
=
import_single_document
(
instance_url
,
""
,
document_content
,
session_id
)
print
(
f
"
New URL after document migration without migrating the URL/subpath:
{
new_url
}
"
)
urls_to_visit
.
append
(
new_url
)
print
()
else
:
print
(
f
"
According to your history, you did not visit
\"
{
document_title
}
.md
\"
in the CodiMD
"
"
instance recently. Migrating the document and generating a new, random URL/path for it
"
)
...
...
@@ -99,9 +89,11 @@ def iterate_over_archive(archive_file, instance_url, lookup_map, session_id, url
print
(
f
"
New URL after document migration with new, random URL/subpath:
"
f
"
{
generated_url
}
"
)
urls_to_visit
.
append
(
generated_url
)
print
()
def
create_urls_to_visit_file
(
filename
,
urls_to_visit
):
print_block_heading
(
"
Creating file containg the 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
\"
"
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment