Skip to content

Commit

Permalink
fix(coffeeani): Url encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
coffeebank committed Nov 2, 2023
1 parent 48ad54f commit 5c23445
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion coffeeani/coffeeani_utils/sources/anilist.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def anilist_get_external_links(media_result):
external_links = ""
for i in range(0, len(media_result["externalLinks"])):
ext_link = media_result["externalLinks"][i]
external_links += f"[{ext_link['site']}]({ext_link['url']}), "
external_links += f"[{ext_link['site']}]({format_url_encode(ext_link['url'], format_all=True, safe=':/')}), "
if i + 1 == len(media_result["externalLinks"]):
external_links = external_links[:-2]
if len(external_links) > 0:
Expand Down
13 changes: 11 additions & 2 deletions coffeeani/coffeeani_utils/utils/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,18 @@ def format_translate(text: str, source_lang: str="a", target_lang: str="en"):
translate = f"https://www.deepl.com/translator#{source_lang}/{target_lang}/{encoded_text}"
return translate

def format_url_encode(text: str):
def format_url_encode(text: str, format_all: bool = False, safe: str = "/"):
"""
By default, doesn't encode East Asian characters, since they're already condensed.
To encode everything, use `format_all=True`
To encode a url, use `safe=":/"`
"""
if text is None:
return None
if format_all is True:
return urllib.parse.quote(text, safe=safe)
east_asian_chars = (
'\u4E00-\u9FFF' # Common Hanzi/Kanji characters
'|\u3400-\u4DBF' # Extension A for rare Hanzi/Kanji characters
Expand All @@ -58,7 +67,7 @@ def format_url_encode(text: str):
'|\uD7B0-\uD7FF' # Hangul Jamo Extended-B
)
# Use regex sub to replace all non-East Asian characters with their quoted versions
return re.sub(f'[^{east_asian_chars}]', lambda m: urllib.parse.quote(m.group(0)), text)
return re.sub(f'[^{east_asian_chars}]', lambda m: urllib.parse.quote(m.group(0), safe=safe), text)

def get_array_first_key(arr):
if arr:
Expand Down

0 comments on commit 5c23445

Please sign in to comment.