Created
January 10, 2013 13:45
-
-
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)
This file contains 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
# 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&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&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