Skip to content

Instantly share code, notes, and snippets.

@rooty
Last active December 19, 2015 01:59
Show Gist options
  • Save rooty/5879810 to your computer and use it in GitHub Desktop.
Save rooty/5879810 to your computer and use it in GitHub Desktop.
youtube id parser without regexp
def 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