Login

Get the youtube video thumbnail url template filter

Author:
fitoria
Posted:
October 16, 2010
Language:
Python
Version:
Not specified
Score:
2 (after 2 ratings)

It gets the thumbnail picture url from a youtube video url.

Example <img src="{{vide.url|youthumbnail:'s'}}">

It supports 2 sizes small(s) and large (l)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
from urlparse import parse_qs
from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

@stringfilter
def youthumbnail(value, args):
    '''returns youtube thumb url
    args s, l (small, large)'''
    qs = value.split('?')
    video_id = parse_qs(qs[1])['v'][0]

    if args == 's':
        return "http://img.youtube.com/vi/%s/2.jpg" % video_id
    elif args == 'l':
        return "http://img.youtube.com/vi/%s/0.jpg" % video_id
    else:
        return None

register.filter('youthumbnail', youthumbnail)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

maccesch (on May 31, 2011):

thanks for the snippet. you should also tag it django :)

#

Please login first before commenting.