Skip to content

Instantly share code, notes, and snippets.

@mvmocanu
Created January 10, 2013 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvmocanu/4502129 to your computer and use it in GitHub Desktop.
Save mvmocanu/4502129 to your computer and use it in GitHub Desktop.
Youtube video ID from youtube urls or from youtube embeded code (the old one or the new one)
# Finds the youtube video id from youtube urls or from
# youtube embeded code (the old one and the new one)
import re
exp = re.compile(r"""
(
youtu.be/|youtube(-nocookie)?.com/
(
watch\?(.*&)?v=|(embed|v)/
)
)
(?P<video_id>[^\?&"\'>]+)
""", re.X)
def find_it(s):
m = re.search(exp, s)
if m:
print m.group('video_id')
find_it('http://www.youtube.com/watch?v=Qi_ltajxQKE')
# Qi_ltajxQKE
find_it('http://youtu.be/ZFqlHhCNBOI')
# ZFqlHhCNBOI
find_it('<iframe src="http://www.youtube.com/embed/ZFqlHhCNBOI">')
# ZFqlHhCNBOI
object_str = (
'<object width="560" height="315"><param name="movie" '
'value="https://www.youtube-nocookie.com/v/Qi_ltajxQKE?version=3&amp;hl=en_US">'
'</param><param name="allowFullScreen" value="true"></param>'
'<param name="allowscriptaccess" value="always"></param>'
'<embed src="https://www.youtube-nocookie.com/v/Qi_ltajxQKE?version=3&amp;hl=en_US"'
' type="application/x-shockwave-flash" width="560" height="315" '
'allowscriptaccess="always" allowfullscreen="true"></embed></object>'
)
find_it(object_str)
# Qi_ltajxQKE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment