Skip to content

Instantly share code, notes, and snippets.

@kurap
Forked from joanfont/youtube.py
Last active August 29, 2015 14:23
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 kurap/6b6fa979a012ad1743a1 to your computer and use it in GitHub Desktop.
Save kurap/6b6fa979a012ad1743a1 to your computer and use it in GitHub Desktop.
from django import template
import re
register = template.Library()
@register.simple_tag
def youtube_embed(video):
YOUTUBE_REGEX = r'(https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\.(com|be)/(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})'
def _get_id(video):
match = re.match(YOUTUBE_REGEX, video)
return match.group(6) if match else None
def _get_embed_url(video_id):
return '//youtube.com/embed/%s?showinfo=0&controls=2&showsearch=0&rel=0' % video_id
_id =_get_id(video)
if _id:
embed_url = _get_embed_url(_id)
return '<iframe frameborder="0" webkitallowfullscreen="true"'\
'width="720" height="360"'\
'mozallowfullscreen="true" allowfullscreen="true"'\
'src="%s"></iframe>' % embed_url
else:
return video
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment