-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAvatar.py
86 lines (61 loc) · 2.2 KB
/
Avatar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from PIL import Image
from flask import Flask, send_file, request, abort
from flask_sqlalchemy import SQLAlchemy
from StringIO import StringIO
import requests, os
app = Flask(__name__)
AVAILABLE_SIZES = [60, 88, 95, 120, 300, 600]
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@127.0.0.1/timeline'
database = SQLAlchemy(app)
def downloadImage(image, size = 120):
path = 'http://icer.ink/mobcdn.clubpenguin.com/game/items/images/paper/image/{}/{}.png'.format(size, image)
print 'Downloading...', path
dimg = requests.get(path)
image_p = 'Avatar/paper/{}/{}.png'.format(size, image)
p = '/'.join(image_p.split('/')[:-1])
if not os.path.exists(p): os.makedirs(p)
sprite = Image.open(StringIO(dimg.content))
sprite.save(image_p)
return sprite
def initializeImage(items, size = 120):
path = 'Avatar/paper/{}/{}.png'.format(size, '{}')
sprites = list()
for i in items:
if i == 0:
sprites.append(Image.new('RGBA', (size, size), (0, 0, 0, 0)))
continue
if not os.path.exists(path.format(i)):
sprites.append(downloadImage(i, size))
else:
sprite = Image.open(path.format(i))
sprites.append(sprite)
return sprites
def buildAvatar(images):
Avatar = images[0]
for i in images[1:]:
Avatar.paste(i, (0, 0), i)
AvatarIO = StringIO()
Avatar.save(AvatarIO, 'PNG', quality = 100)
AvatarIO.seek(0)
return AvatarIO
@app.route('/<path:m>/crossdomain.xml', methods = ['GET'])
@app.route('/crossdomain.xml', methods = ['GET'])
def handleCrossdomain(m = None):
return '<cross-domain-policy><allow-access-from domain="*"/></cross-domain-policy>'
@app.route("/<swid>/cp", methods = ['GET'])
def getAvatar(swid):
size = 120
try:
if request.args.has_key('size'):
size = int(request.args.get('size'))
if not size in AVAILABLE_SIZES:
size = 120
except:
pass
details = database.engine.execute("SELECT Photo, Color, Head, Face, Neck, Body, Hand, Feet, Pin FROM avatars a, penguins p WHERE p.id = a.penguin_id and p.swid = %s", swid)
details = details.first()
if details is None:
return abort(404)
items = initializeImage(list(map(int, details)), size)
return send_file(buildAvatar(items), mimetype='image/png')
app.run(host = 'cdn.avatar.clubpenguin.com', port = 80, debug = True)