Use minecraft avatars

This commit is contained in:
Trever Fischer
2012-03-03 22:09:40 -05:00
parent 8687e034ac
commit 34567bf695
8 changed files with 44 additions and 4 deletions

View File

View File

@@ -0,0 +1,15 @@
from django.core.urlresolvers import reverse
from django import template
register = template.Library()
@register.tag
def avatar(parser, token):
return AvatarNode(token.split_contents()[1])
class AvatarNode(template.Node):
def __init__(self, uservar):
self.__user = template.Variable(uservar)
def render(self, context):
return '<img src="%s"/>'%(reverse('minecraft.views.avatar', kwargs={'username': self.__user.resolve(context)}),)

5
minecraft/urls.py Normal file
View File

@@ -0,0 +1,5 @@
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('minecraft',
url(r'^avatar/(?P<username>.*).png$', 'views.avatar'),
)

19
minecraft/views.py Normal file
View File

@@ -0,0 +1,19 @@
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
def avatar(request, username):
avatar = cache.get('minecraft-avatar-%s'%(username))
if avatar is None:
imgStream = StringIO(urlopen("http://minecraft.net/skin/%s.png"%(username)).read())
img = Image.open(imgStream)
img = img.crop((8, 8, 16,16))
img = img.resize((64, 64), Image.NEAREST)
buf = StringIO()
img.save(buf, "PNG")
avatar = buf.getvalue()
cache.set('minecraft-avatar-%s', avatar, 86400)
return HttpResponse(avatar, content_type="image/png")