Example #1
0
def ooyala_thubmnail(embed_code, resolution='320x240', indicies="0-25", cache=True):
    """ Grab one of the custom thumbnail images - ooyala may return bigger
    images than requested at a higher compression rate so they should be also
    force-sized in your CSS. Indicies will return a number of thumbs, ones
    in the middle tend to be "better" suited to display """
    thumb = None
    try:

        if cache:
            video = OoyalaItem.objects.get(embed_code=embed_code)
            thumb = video.thumbnail

        if not thumb:
            thumbs = OoyalaThumbnail(embed_code=embed_code, resolution=resolution, indicies=indicies).process()
            thumbs_data = thumbs.getElementsByTagName('thumbnail')
            idx = len(thumbs_data)/2
            thumb = thumbs_data[idx].firstChild.nodeValue

        if cache:
            video.thumbnail = thumb
            video.save()

        return thumb
    except (IndexError, AttributeError):
        return NO_THUMB
Example #2
0
def enlarge_thumbnail(oitem, desired_size='278x175'):
    """
    Basic issue: we cache a thumbnail but don't record the size.
    So a small thumbnail can be cached sometimes.
    """
    desired_x = int(desired_size.split('x')[0])

    small_url = oitem.thumbnail
    if not small_url:
        print 'no thumb in place, TODO: get a random new one'
        return None
    img_data = urllib.urlopen(small_url).read()
    f = StringIO(img_data)
    size = Image.open(f).size
    if size[0] > desired_x:
        print 'already have a bigger image: %s > %s' % (size, desired_size)
        if '#' not in oitem.thumbnail:
            oitem.thumbnail += FIXED_THUMB_SYMBOL
            oitem.save()
        return None
    res = '%sx%s' % size

    small_thumbs = OoyalaThumbnail(embed_code=oitem.embed_code, resolution=res, indicies='0-25').process()
    if isinstance(small_thumbs, str):
        print 'OoyalaThumbnail error:', small_thumbs
        return None
    if oitem.thumbnail not in small_thumbs.toprettyxml():
        print 'existing thumb not found in xml'
        return None
    res = re.findall(r'index="(\d+)".*\n.*%s' % oitem.thumbnail, small_thumbs.toprettyxml())
    if res and res[0] and res[0].isdigit:
        idx = int(res[0])
    else:
        print 'regex not matched:', repr(res)
        if 'promo' in oitem.thumbnail:
            print 'promo image', oitem.thumbnail
        return None

    thumbs_data = small_thumbs.getElementsByTagName('thumbnail')


    big_thumbs = OoyalaThumbnail(embed_code=oitem.embed_code, resolution=desired_size, indicies='0-25').process()
    thumbs_data = big_thumbs.getElementsByTagName('thumbnail')
    big_url = thumbs_data[idx].firstChild.nodeValue
    # mark enlarged thumbs with a hash
    oitem.thumbnail = big_url + FIXED_THUMB_SYMBOL
    oitem.save()
    return big_url