Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Sep 4, 2024
1 parent 2c3704b commit 8e839b2
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions tutubo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ def extract_channel_name(url: str) -> str:
This function supports the following patterns:
- :samp:`https://youtube.com/{channel_name}/*`
- :samp:`https://youtube.com/@{channel_name}/*`
- :samp:`https://youtube.com/c/{channel_name}/*`
- :samp:`https://youtube.com/channel/{channel_id}/*
- :samp:`https://youtube.com/c/@{channel_name}/*`
- :samp:`https://youtube.com/channel/@{channel_id}/*
- :samp:`https://youtube.com/u/{channel_name}/*`
- :samp:`https://youtube.com/user/{channel_id}/*
Expand All @@ -25,22 +28,14 @@ def extract_channel_name(url: str) -> str:
:returns:
YouTube channel name.
"""
patterns = [
r"(?:\/@([\w\d_\-]+)(\/.*)?)",
r"(?:\/(c)\/([%\d\w_\-]+)(\/.*)?)",
r"(?:\/(channel)\/([%\w\d_\-]+)(\/.*)?)",
r"(?:\/(u)\/([%\d\w_\-]+)(\/.*)?)",
r"(?:\/(user)\/([%\w\d_\-]+)(\/.*)?)"
]
for pattern in patterns:
regex = re.compile(pattern)
function_match = regex.search(url)
if function_match:
if "/@" in url:
return f'/@{function_match.group(1)}'
uri_style = function_match.group(1)
uri_identifier = function_match.group(2)
return f'/{uri_style}/{uri_identifier}'
pattern = r"(?:https?:\/\/)?(?:www\.)?youtube\.com\/(?:(user|channel|c)(?:\/))?\@?([%\d\w_\-]+)"
regex = re.compile(pattern)
function_match = regex.search(url)
if function_match:
uri_style = function_match.group(1)
uri_style = uri_style if uri_style else "c"
uri_identifier = function_match.group(2)
return f'/{uri_style}/{uri_identifier}'

raise extract.RegexMatchError(
caller="channel_name", pattern="patterns"
Expand Down Expand Up @@ -240,6 +235,10 @@ def length(self):
60 * 60 * int(h)
return 0

@property
def is_live(self) -> bool:
return self.length == 0

@property
def thumbnail_url(self):
return f"https://img.youtube.com/vi/{self.video_id}/default.jpg"
Expand Down

0 comments on commit 8e839b2

Please sign in to comment.