-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbanner.py
executable file
·131 lines (115 loc) · 4.47 KB
/
banner.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python2.7
import os;
import sys;
import bleach;
import StringIO;
import cgi;
import cgitb;
cgitb.enable();
import urllib2;
import xml.etree.ElementTree as ET;
from PIL import Image;
from PIL import ImageFont;
from PIL import ImageDraw;
print("Content-Type: image/png\r\n");
banners = {
'cannons': 'cannons.png',
'civilwar_scene': 'civil_war_scene.png',
'civilwar_scene2': 'picketts_charge.png',
'mnb_castle': 'wb_castle.png',
'mnb_cavalry': 'cavalry.png',
'mnb_knights': 'wb_knights.png',
'mnb_mounted': 'mounted.png',
'nw_borodino': 'borodino.png',
'nw_scene': 'nw_french.png'
};
images_directory = "./images/";
buff = StringIO.StringIO();
arguments = cgi.FieldStorage();
ip = bleach.clean(arguments["ip"].value);
port = bleach.clean(arguments["port"].value);
banner = banners[bleach.clean(arguments["banner"].value)];
img = Image.open(os.path.join(images_directory, banner)).convert('RGBA');
draw = ImageDraw.Draw(img);
font1 = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf', 14);
font2 = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 18);
link = "http://" + ip + ":" + port;
try:
f = urllib2.urlopen(link, None, 5);
except:
error = 'Error Retrieving Server Details';
w, h = draw.textsize(error, font2);
draw.text((249-(w/2),1),error,(0,0,0),font=font2);
draw.text((251-(w/2),1),error,(0,0,0),font=font2);
draw.text((249-(w/2),3),error,(0,0,0),font=font2);
draw.text((251-(w/2),3),error,(0,0,0),font=font2);
draw.text((250-(w/2),2),error,(255,0,0),font=font2);
img.save(buff, 'png');
print(buff.getvalue());
buff.close();
sys.exit(0);
stats = f.read();
e = ET.fromstring(stats);
serverName = bleach.clean(e.getiterator(tag='Name')[0].text);
module = bleach.clean(e.getiterator(tag='ModuleName')[0].text);
type = bleach.clean(e.getiterator(tag='MapTypeName')[0].text);
map = bleach.clean(e.getiterator(tag='MapName')[0].text);
players = bleach.clean(e.getiterator(tag='NumberOfActivePlayers')[0].text) + " / " + bleach.clean(e.getiterator(tag='MaxNumberOfPlayers')[0].text);
status = bleach.clean(e.getiterator(tag='HasPassword')[0].text);
if status == 'No':
lock = Image.open(os.path.join(images_directory, 'unlocked.png')).convert('RGBA');
else:
lock = Image.open(os.path.join(images_directory, 'locked.png')).convert('RGBA');
# Draw Header
game = 'Mount & Blade: Warband'
w, h = draw.textsize(game, font2);
draw.text((249-(w/2),1),game,(0,0,0),font=font2);
draw.text((251-(w/2),1),game,(0,0,0),font=font2);
draw.text((249-(w/2),3),game,(0,0,0),font=font2);
draw.text((251-(w/2),3),game,(0,0,0),font=font2);
draw.text((250-(w/2),2),game,(255,255,255),font=font2);
# Draw Eval Server
draw.text((8, 28),'Server Name:',(0,0,0),font=font1);
draw.text((9, 40),serverName,(0,0,0),font=font2);
draw.text((11, 40),serverName,(0,0,0),font=font2);
draw.text((9, 42),serverName,(0,0,0),font=font2);
draw.text((11, 42),serverName,(0,0,0),font=font2);
draw.text((10, 41),serverName,(255,255,255),font=font2);
# Draw Eval Module
draw.text((8, 66),'Module:',(0,0,0),font=font1);
draw.text((9, 78),module,(0,0,0),font=font2);
draw.text((11, 78),module,(0,0,0),font=font2);
draw.text((9, 80),module,(0,0,0),font=font2);
draw.text((11, 80),module,(0,0,0),font=font2);
draw.text((10, 79),module,(255,255,255),font=font2);
# Draw Game Type
draw.text((8, 104),'Game Mode:',(0,0,0),font=font1);
draw.text((9, 116),type,(0,0,0),font=font2);
draw.text((11,116),type,(0,0,0),font=font2);
draw.text((9,118),type,(0,0,0),font=font2);
draw.text((11,118),type,(0,0,0),font=font2);
draw.text((10,117),type,(255,255,255),font=font2);
# Draw Current Map
w, h = draw.textsize('Current Map:', font1);
draw.text((492-w, 28),'Current Map:',(0,0,0),font=font1);
w, h = draw.textsize(map, font2);
draw.text((489-w, 40),map,(0,0,0),font=font2);
draw.text((491-w, 40),map,(0,0,0),font=font2);
draw.text((489-w, 42),map,(0,0,0),font=font2);
draw.text((491-w, 42),map,(0,0,0),font=font2);
draw.text((490-w, 41),map,(255,255,255),font=font2);
# Draw Number of Players
w, h = draw.textsize('Current Players:', font1);
draw.text((492-w, 66),'Current Players:',(0,0,0),font=font1);
w, h = draw.textsize(players, font2);
draw.text((489-w, 78),players,(0,0,0),font=font2);
draw.text((491-w, 78),players,(0,0,0),font=font2);
draw.text((489-w, 80),players,(0,0,0),font=font2);
draw.text((491-w, 80),players,(0,0,0),font=font2);
draw.text((490-w, 79),players,(255,255,255),font=font2);
# Draw Lock/Unlocked Symbol
img.paste(lock, (474,4));
# Output final image
img.save(buff, 'png');
print(buff.getvalue());
buff.close();