Skip to content

Commit

Permalink
Fix extract_video_id_from_youtube_url to handle shorts
Browse files Browse the repository at this point in the history
  • Loading branch information
Yomguithereal committed Feb 29, 2024
1 parent 73dcab9 commit 2986293
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1867,7 +1867,7 @@ parse_youtube_url('https://www.youtube.com/taranisnews')

#### extract_video_id_from_youtube_url

Return a video id from the given Youtube url or `None` if we could not find one.
Return a video id from the given Youtube url or `None` if we could not find one. Note that this will also work with Youtube shorts.

```python
from ural.youtube import extract_video_id_from_youtube_url
Expand Down
4 changes: 3 additions & 1 deletion test/youtube_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,9 @@ def test_parse_youtube_url(self):

def test_extract_video_id_from_youtube_url(self):
for url, result, _ in PARSE_TESTS:
extract_result = result.id if isinstance(result, YoutubeVideo) else None
extract_result = (
result.id if isinstance(result, (YoutubeVideo, YoutubeShort)) else None
)

assert extract_video_id_from_youtube_url(url) == extract_result

Expand Down
1 change: 0 additions & 1 deletion ural/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,3 @@ def get_query_argument(url, key):
return q[1]

return None

5 changes: 2 additions & 3 deletions ural/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def parse_youtube_url(url, fix_common_mistakes=True):
def extract_video_id_from_youtube_url(url):
parsed = parse_youtube_url(url)

if parsed is None or not isinstance(parsed, YoutubeVideo):
if parsed is None or not isinstance(parsed, (YoutubeVideo, YoutubeShort)):
return

return parsed.id
Expand Down Expand Up @@ -433,7 +433,6 @@ def normalize_youtube_url(url):
return YOUTUBE_CHANNEL_NAME_URL_TEMPLATE % parsed.name

if isinstance(parsed, YoutubeShort):
if parsed.id is not None:
return YOUTUBE_SHORT_URL_TEMPLATE % parsed.id
return YOUTUBE_SHORT_URL_TEMPLATE % parsed.id

raise TypeError("normalize_youtube_url: impossible path reached")

0 comments on commit 2986293

Please sign in to comment.