Taylor Fox Dahlin commited on
Commit
b45ac80
·
unverified ·
1 Parent(s): 3157612

Fix channel url matching. (#1005)

Browse files

* Fixes channel url matching for channels without canonical names.

pytube/contrib/channel.py CHANGED
@@ -14,10 +14,10 @@ class Channel(Playlist):
14
  def __init__(self, url: str, proxies: Optional[Dict[str, str]] = None):
15
  super().__init__(url, proxies)
16
 
17
- self.channel_name = extract.channel_name(url)
18
 
19
  self.channel_url = (
20
- f"https://www.youtube.com/c/{self.channel_name}"
21
  )
22
  self.videos_url = self.channel_url + '/videos'
23
  self.playlists_url = self.channel_url + '/playlists'
 
14
  def __init__(self, url: str, proxies: Optional[Dict[str, str]] = None):
15
  super().__init__(url, proxies)
16
 
17
+ self.channel_uri = extract.channel_name(url)
18
 
19
  self.channel_url = (
20
+ f"https://www.youtube.com{self.channel_uri}"
21
  )
22
  self.videos_url = self.channel_url + '/videos'
23
  self.playlists_url = self.channel_url + '/playlists'
pytube/extract.py CHANGED
@@ -195,16 +195,17 @@ def channel_name(url: str) -> str:
195
  YouTube channel name.
196
  """
197
  patterns = [
198
- r"(?:\/c\/([\d\w_\-]+)(\/.*)?)",
199
- r"(?:\/channel\/([\w\d_\-]+)(\/.*)?)"
200
  ]
201
  for pattern in patterns:
202
  regex = re.compile(pattern)
203
  function_match = regex.search(url)
204
  if function_match:
205
  logger.debug("finished regex search, matched: %s", pattern)
206
- channel_id = function_match.group(1)
207
- return channel_id
 
208
 
209
  raise RegexMatchError(
210
  caller="channel_name", pattern="patterns"
 
195
  YouTube channel name.
196
  """
197
  patterns = [
198
+ r"(?:\/(c)\/([\d\w_\-]+)(\/.*)?)",
199
+ r"(?:\/(channel)\/([\w\d_\-]+)(\/.*)?)"
200
  ]
201
  for pattern in patterns:
202
  regex = re.compile(pattern)
203
  function_match = regex.search(url)
204
  if function_match:
205
  logger.debug("finished regex search, matched: %s", pattern)
206
+ uri_style = function_match.group(1)
207
+ uri_identifier = function_match.group(2)
208
+ return f'/{uri_style}/{uri_identifier}'
209
 
210
  raise RegexMatchError(
211
  caller="channel_name", pattern="patterns"
tests/contrib/test_channel.py CHANGED
@@ -20,7 +20,7 @@ def test_channel_name(request_get, channel_videos_html):
20
  request_get.return_value = channel_videos_html
21
 
22
  c = Channel('https://www.youtube.com/c/ProgrammingKnowledge/videos')
23
- assert c.channel_name == 'ProgrammingKnowledge'
24
 
25
 
26
  @mock.patch('pytube.request.get')
 
20
  request_get.return_value = channel_videos_html
21
 
22
  c = Channel('https://www.youtube.com/c/ProgrammingKnowledge/videos')
23
+ assert c.channel_uri == '/c/ProgrammingKnowledge'
24
 
25
 
26
  @mock.patch('pytube.request.get')