-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspaceship_serverinfo.py
executable file
·208 lines (174 loc) · 6.11 KB
/
spaceship_serverinfo.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/python3
import sys
# https://github.com/Yepoleb/python-a2s
import a2s
import time
import socket
# list of servers. no shit
serverlist = [
"tf.sappho.io:27015",
"elo1.sappho.io:27115",
"elo2.sappho.io:27215",
"dm.sappho.io:27315",
"dm.sappho.io:27415",
"dm.sappho.io:28315",
"dm.sappho.io:28415",
"tf.sappho.io:27515",
"tf.sappho.io:27615",
"bb1.sappho.io:27715",
"pub.sappho.io:27915",
"tf.sappho.io:29015",
"tf.sappho.io:29999",
"leaf.sappho.io:27015",
]
# oh man this is so ugly but i'd rather do this in python than php
# we just readfile with php and this just becomes a table
# because i don't know php and i dont want to fuck with it any more than i have to
template_string = (
"""
<div class="centerTable">
<h3 style="margin: 5px">There are <span style="color:#ff85d5">{}</span> players across <span style="color:#ff85d5">{}</span> servers!</h3>
<div class="serverDiv" style="">
<table class="servers">
<tbody>
<tr>
<th>Game</th>
<th>Players</th>
<th>Connect Link</th>
<th>Server Name</th>
<th class="mapHeading">Current Map</th>
</tr>
{}
</tbody>
</table>
</div>
</div>
""")
final_string = ''
totalplayers = 0
totalservers = 0
# also ugly don't look at me
full = 'server_full'
notfull = 'server_notfull'
empty = 'server_empty'
error = 'server_error'
table_string = ''
for server in serverlist:
totalservers += 1
# get our url
serverurl = server.split(':')[0]
# get our port and cast to an int because the a2s lib needs the port as an int
serverport = int(server.split(':')[1])
# recombine them into a proper tuple so that a2s recognizes it
a2s_tuple = (serverurl, serverport)
# these get overriden if we get a successful query, otherwise these display when an error occurs
playercount = 0
maxplayers = 0
hostname = "Not set??? Report this to the site owner please"
mapname = "N/A"
islocked = False;
threwerror = True;
# try querying 5 times with a 3s timeout for servers that may be map changing.
# hostname is where we display our error msg if we get one.
foldername = ""
# "tHiS iSn'T pYtHoNiC" shut up
for tries in range(10):
try:
query = a2s.info(a2s_tuple, timeout=1.0)
playercount = query.player_count
maxplayers = query.max_players
hostname = query.server_name
mapname = query.map_name
foldername = query.folder
islocked = query.password_protected
threwerror = False;
print('Got info for {}!'.format(str(server)))
break
except socket.timeout:
print('Timeout for {}. Trying again... (try {})'.format(str(server), tries+1))
# there's no reason to retry if we get any of these errors.
except ConnectionRefusedError:
hostname = "Connection refused while querying."
break
except socket.gaierror:
hostname = "No server exists at this address."
break
except OSError:
hostname = "An OSError occurred."
break
except:
hostname = "An unknown error occurred!"
break
else:
hostname = "Query timeout."
#print(hostname)
if "tf" == foldername:
gameico = "https://steamcdn-a.akamaihd.net/apps/tf2/blog/images/favicon.ico"
gameurl = "https://www.teamfortress.com/"
elif "open_fortress" in foldername:
gameico = "https://openfortress.fun/images/favicon.ico"
gameurl = "https://openfortress.fun"
elif "tf2classic" in foldername:
gameico = "https://tf2classic.com/favicon-32x32.png"
gameurl = "https://tf2classic.com"
else:
gameico = "https://sappho.io/rsrc/galaxyemoji_new_round.png"
gameurl = "https://sappho.io/spaceship"
gamename_column = '<td class="gametype"><a href="{}"><img src="{}"></a></td>'.format(gameurl, gameico)
# don't count or display locked servers
if islocked:
continue
# count num of players on the whole network
totalplayers += playercount
# PLAYER COUNT
if threwerror:
span_playerstype = error
elif playercount >= maxplayers:
span_playerstype = full
elif playercount > 0:
span_playerstype = notfull
else:
span_playerstype = empty
playernum_column = '<td class="playernum"> <span class="{}">{}/{}</span> </td>'.format(span_playerstype, playercount, maxplayers)
# STEAM CONNECT URL AND SERVER URL
url_column = '<td class="link"> <a href="steam://connect/{}:{}">{}</a> </td>'.format(serverurl, serverport, server)
# SERVER HOSTNAME (or error if one was thrown)
if "The Pub" in hostname:
hostname_column = '<td class="rainbow-name">{}</td>'.format(hostname)
else:
hostname_column = '<td class="name">{}</td>'.format(hostname)
# SERVER MAP
map_column = '<td class="map">{}</td>'.format(mapname)
# servers that err'd out should be less noticable on the server page
# td_error sets their opacity to... i think its 50%?
changeopacity = ''
if threwerror:
changeopacity = ' td_error'
# add it to our string
table_string += (
"""
<tr class="server_tablerow{}">
{}
{}
{}
{}
{}
</tr>
""").format(changeopacity,
gamename_column,
playernum_column,
url_column,
hostname_column,
map_column)
time.sleep(0.1)
final_string = template_string.format(totalplayers, totalservers, table_string)
OUTPUT = '/var/www/sappho.io/rsrc/servertable.txt'
# this is so it gets flushed instantly because otherwise,
# you can have a race condition where
# script runs -> user connects to site before it's done -> they see a blank page
f = open(OUTPUT, "wb", 0)
# needs to be binary because... python? no clue
f.write(final_string.encode("utf-8"))
f.close()
#print(final_string)
sys.exit(0)