Last active
January 20, 2021 14:06
-
-
Save maxpoletaev/370f3e3fa571e768b09ed9dcc86cd4e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Source: https://stackoverflow.com/a/7936523 | |
from urllib.parse import urlparse, parse_qs | |
def extract_video_id(value): | |
""" | |
Examples: | |
- http://youtu.be/SA2iWivDJiE | |
- http://www.youtube.com/watch?v=_oPAwA_Udwc&feature=feedu | |
- http://www.youtube.com/embed/SA2iWivDJiE | |
- http://www.youtube.com/v/SA2iWivDJiE?version=3&hl=en_US | |
""" | |
query = urlparse(value) | |
if query.hostname == "youtu.be": | |
return query.path[1:] | |
if query.hostname in ("www.youtube.com", "youtube.com"): | |
if query.path == "/watch": | |
p = parse_qs(query.query) | |
return p["v"][0] | |
if query.path[:7] == "/embed/": | |
return query.path.split("/")[2] | |
if query.path[:3] == "/v/": | |
return query.path.split("/")[2] | |
# fail? | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment