Login

youtubize template tag

Author:
TheJester
Posted:
May 3, 2007
Language:
Python
Version:
.96
Tags:
youtube embed urlize
Score:
9 (after 9 ratings)

This snippet is based on djangos urlize filter. It converts http:// links to youtube into youtube-embed statements, so that one can provide a simple link to a youtube video and this filter will embed it. I used it for a fun blog app.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@register.filter
def youtubize(value):
    """
    Converts http:// links to youtube into youtube-embed statements, so that
    one can provide a simple link to a youtube video and this filter will
    embed it.
    
    Based on the Django urlize filter.
    """
    text = value
    # Configuration for urlize() function
    LEADING_PUNCTUATION  = ['(', '<', '&lt;']
    TRAILING_PUNCTUATION = ['.', ',', ')', '>', '\n', '&gt;']
    word_split_re = re.compile(r'(\s+)')
    punctuation_re = re.compile('^(?P<lead>(?:%s)*)(?P<middle>.*?)(?P<trail>(?:%s)*)$' % \
            ('|'.join([re.escape(x) for x in LEADING_PUNCTUATION]),
            '|'.join([re.escape(x) for x in TRAILING_PUNCTUATION])))
    youtube_re = re.compile ('http://www.youtube.com/watch.v=(?P<videoid>(.+))')
    
    
    words = word_split_re.split(text)
    for i, word in enumerate(words):
        match = punctuation_re.match(word)
        if match:
            lead, middle, trail = match.groups()
            if middle.startswith('http://www.youtube.com/watch') or middle.startswith('http://youtube.com/watch'):
                video_match = youtube_re.match(middle)
                if video_match:
                    video_id = video_match.groups()[1]
                    middle = '''<object width="425" height="350">
      <param name="movie" value="http://www.youtube.com/v/%s"/>
      <param name="wmode" value="transparent"/>
      <embed src="http://www.youtube.com/v/%s" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"/>
    </object>''' % (video_id, video_id)

            if lead + middle + trail != word:
                words[i] = lead + middle + trail
    return ''.join(words)

More like this

  1. "Youtube watch link to embed" custom tag. by I159 3 years, 8 months ago
  2. YouTube Template Filter by jgeewax 6 years, 10 months ago
  3. ultralize by catellar 3 years, 9 months ago
  4. convert youtube url for iframe, js api by ivanff 2 years, 2 months ago
  5. Very simple email image embed by ssadler 6 years ago

Comments

ericflo (on May 3, 2007):

This is awesome! Great snippet!

#

Ryochan7 (on October 12, 2007):

This is an awesome filter. Thanks for putting it up.

#

ekin (on July 8, 2011):

Thank for this useful filter. I tried to embed some youtube video, but i couldn't do it. When i insert the link, i see the link again. It doesn't convert to embeded video.

I tried this on a blog which has tinymce within the entry. Thank you for your help.

#

Please login first before commenting.