Extract the avatar downloading code to a standalone function

This commit is contained in:
Trever Fischer
2012-06-10 11:03:22 -04:00
parent f5dc82f6fc
commit 603ebe6435
2 changed files with 33 additions and 27 deletions

View File

@@ -1,4 +1,10 @@
import badges.api
from PIL import Image
import os
from django.core.cache import cache
from urllib2 import urlopen
from httplib import HTTPException
from cStringIO import StringIO
def update_badges(user):
playtime = user.minecraftprofile.totalPlaytime();
@@ -8,3 +14,28 @@ def update_badges(user):
badges.api.award(user, "7d_playtime")
if playtime.days >= 30:
badges.api.award(user, "30d_playtime")
def download_avatar(username, size):
avatar = cache.get('minecraft-avatar-%s-%s'%(username, size))
size = int(size)
if avatar is None:
try:
skinStream = urlopen("http://minecraft.net/skin/%s.png"%(username.replace(" ", "_")))
except (IOError, HTTPException), e:
skinStream = open(os.path.dirname(__file__)+"/static/skin.png")
imgStream = StringIO(skinStream.read())
img = Image.open(imgStream)
face = img.crop((8, 8, 16,16))
face.load()
overlay = img.crop((40, 8, 48, 16))
overlay.load()
try:
img = Image.composite(overlay, face, overlay)
except ValueError, e:
img = face
img = img.resize((size, size), Image.NEAREST)
buf = StringIO()
img.save(buf, "PNG")
avatar = buf.getvalue()
cache.set('minecraft-avatar-%s-%s'%(username, size), avatar, 86400)
return avatar

View File

@@ -1,40 +1,15 @@
from PIL import Image
from django.http import HttpResponse
from urllib2 import urlopen
from cStringIO import StringIO
from django.core.cache import cache
from django.views.decorators.cache import cache_control
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.template.loader import select_template
from httplib import HTTPException
from minecraft import download_avatar
import models
import os
@cache_control(public=True, private=False, no_cache=False, no_transform=False, must_revalidate=False, proxy_revalidate=False, max_age=86400)
def avatar(request, username, size=64):
avatar = cache.get('minecraft-avatar-%s-%s'%(username, size))
size = int(size)
if avatar is None:
try:
skinStream = urlopen("http://minecraft.net/skin/%s.png"%(username.replace(" ", "_")))
except (IOError, HTTPException), e:
skinStream = open(os.path.dirname(__file__)+"/static/skin.png")
imgStream = StringIO(skinStream.read())
img = Image.open(imgStream)
face = img.crop((8, 8, 16,16))
face.load()
overlay = img.crop((40, 8, 48, 16))
overlay.load()
try:
img = Image.composite(overlay, face, overlay)
except ValueError, e:
img = face
img = img.resize((size, size), Image.NEAREST)
buf = StringIO()
img.save(buf, "PNG")
avatar = buf.getvalue()
cache.set('minecraft-avatar-%s-%s'%(username, size), avatar, 86400)
avatar = download_avatar(username, size)
return HttpResponse(avatar, content_type="image/png")
def rules(request, server, port):