-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://github.com/C2DH/journal-of-digital-history-backend/issues/206
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters