Skip to content

Commit c8e12bc

Browse files
chore: use a url parsing library instead of regex
1 parent 6e34994 commit c8e12bc

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

modmail/addons/models.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import logging
4-
import re
4+
import urllib.parse
55
from enum import Enum
66
from typing import TYPE_CHECKING, Any, Dict, List, Literal, NoReturn, Optional, Set, Union
77

@@ -85,10 +85,10 @@ def __init__(self, zip_url: str, type: SourceTypeEnum):
8585
"""Initialize the AddonSource."""
8686
self.zip_url = zip_url
8787
if self.zip_url is not None:
88-
match = re.match(r"^(?:https?:\/\/)?(?P<url>(?P<domain>.*\..+?)\/(?P<path>.*))$", self.zip_url)
89-
self.zip_url = match.group("url")
90-
self.domain = match.group("domain")
91-
self.path = match.group("path")
88+
parsed_url = urllib.parse.urlparse(self.zip_url)
89+
self.zip_url = urllib.parse.urlunparse(parsed_url)
90+
self.domain = parsed_url.netloc
91+
self.path = parsed_url.path
9292
else:
9393
self.domain = None
9494
self.path = None
@@ -120,8 +120,7 @@ def from_repo(cls, user: str, repo: str, reflike: str = None, githost: Host = "g
120120
@classmethod
121121
def from_zip(cls, url: str) -> AddonSource:
122122
"""Create an AddonSource from a zip file."""
123-
match = re.match(r"^(?P<url>(?:https?:\/\/)?(?P<domain>.*\..+?)\/(?P<path>.*\.zip))$", url)
124-
source = cls(match.group("url"), SourceTypeEnum.ZIP)
123+
source = cls(url, SourceTypeEnum.ZIP)
125124
return source
126125

127126
def __repr__(self) -> str: # pragma: no cover

modmail/addons/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async def download_zip_from_source(source: AddonSource, session: ClientSession)
4444
if source.source_type not in (SourceTypeEnum.REPO, SourceTypeEnum.ZIP):
4545
raise TypeError("Unsupported source detected.")
4646

47-
async with session.get(f"https://{source.zip_url}") as resp:
47+
async with session.get(source.zip_url, timeout=20) as resp:
4848
if resp.status != 200:
4949
raise HTTPError(resp)
5050
raw_bytes = await resp.read()

0 commit comments

Comments
 (0)