Skip to content

Commit

Permalink
add fix_thumbnails script
Browse files Browse the repository at this point in the history
  • Loading branch information
TomV committed Jan 23, 2012
1 parent 4a9c8ad commit 6b5935e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
70 changes: 70 additions & 0 deletions ooyala/fix_thumbs.py
@@ -0,0 +1,70 @@
import re
import tempfile
import urllib

try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO


from PIL import Image

from ooyala.models import OoyalaItem
from ooyala.library import OoyalaThumbnail



FIXED_THUMB_SYMBOL = '#'

def enlarge_thumbnail(oitem, desired_size='278x175'):
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

if __name__ == '__main__':
# only do thumbs not marked with a hash
for oitem in OoyalaItem.live.exclude(thumbnail__contains=FIXED_THUMB_SYMBOL):
small_url = oitem.thumbnail
print small_url
res = enlarge_thumbnail(oitem)
3 changes: 3 additions & 0 deletions ooyala/models.py
Expand Up @@ -3,6 +3,7 @@
from django.utils.safestring import mark_safe
from django.db import models
from ooyala.managers import OItemManager
from ooyala.fix_thumbs import enlarge_thumbnail

class OoyalaItem(models.Model):
""" Holds an ooyala item from a Backlot Query request - essentially
Expand Down Expand Up @@ -109,6 +110,8 @@ def get_data(tagname):
created = True
ooyala_item = OoyalaItem(**item_data)
ooyala_item.save()

enlarge_thumbnail(ooyala_item)

if created:
print "Created new item %s " % ooyala_item.embed_code
Expand Down

0 comments on commit 6b5935e

Please sign in to comment.