Skip to content

Commit

Permalink
https://github.com/C2DH/journal-of-digital-history-backend/issues/206
Browse files Browse the repository at this point in the history
  • Loading branch information
eliselavy committed Oct 3, 2023
1 parent c3713ec commit b3412e8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
45 changes: 45 additions & 0 deletions encode_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import re
import base64
from urllib.parse import quote

def encode_notebook_url(url):
# URL-encode the string
url_encoded = quote(url, safe='')

# Base64 encode the URL-encoded string
base64_encoded = base64.b64encode(url_encoded.encode('utf-8')).decode('utf-8')

# Replace '+' with '/' to match the desired result
result = base64_encoded.replace('+', '/')

return result

def process_github_url(value):
github_regex = r'https?://(github\.com|raw\.githubusercontent\.com)/([A-Za-z0-9-_.]+)/([A-Za-z0-9-_.]+)/(blob/)?(.*)'
match = re.match(github_regex, value)

if match:
domain, username, repo, _, filepath = match.groups()
proxy_value = f'/proxy-githubusercontent/{username}/{repo}/{filepath}'
result = {
'value': value,
'domain': domain,
'proxyValue': proxy_value,
'origin': 'github'
}
else:
print('Origin is not fully supported, things can go wrong... value:', value)
result = {'value': value, 'origin': 'unknown'}

return result

def main():
notebook_url = "https://github.com/C2DH/template_repo_JDH/blob/main/author_guideline_template.ipynb"

result = process_github_url(notebook_url)
encode = encode_notebook_url(result['proxyValue'])
# Use the result as needed, for example:
print(encode)

if __name__ == '__main__':
main()
2 changes: 2 additions & 0 deletions preflight.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ def main(
output_md="preflight_report.md",
):
workspace = os.getenv("GITHUB_WORKSPACE", "")
print(f"::debug::workspace:{workspace}")
notebook_filepath = os.path.join(workspace, notebook)
print(f"::debug::notebook_filepath:{notebook_filepath}")
if not os.path.exists(notebook_filepath):
print(f"::error::Path {notebook_filepath} does not exist")
sys.exit(1)
Expand Down

0 comments on commit b3412e8

Please sign in to comment.