Use minecraft avatars
This commit is contained in:
0
minecraft/templatetags/__init__.py
Normal file
0
minecraft/templatetags/__init__.py
Normal file
15
minecraft/templatetags/minecraft.py
Normal file
15
minecraft/templatetags/minecraft.py
Normal 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
5
minecraft/urls.py
Normal 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
19
minecraft/views.py
Normal 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")
|
Reference in New Issue
Block a user