-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnetkoth.py
More file actions
134 lines (124 loc) · 5.99 KB
/
Copy pathnetkoth.py
File metadata and controls
134 lines (124 loc) · 5.99 KB
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
#!/usr/local/bin/python
"""
This is Irongeek's automated scoring code for the NetKotH game.
Create a netkothconfig.ini in the same directory as the script
to look something like:
______________________________________________________________
[General]
outfile = default.htm
sleeptime = 6
[Servers To Check]
linux = http://127.0.0.1/a.htm
windows = http://127.0.0.1/b.htm
wildcard = http://127.0.0.1/c.htm
______________________________________________________________
Server names can be changed (no ":" or "=" characters please),
and the score file will be generated by the script. I'm just now
learning Python, so I'm sure there are better ways to do some
of the tasks I do in this script. You can also make a
template.htm file, and tags that match the server name in all
upper case will be replaced with the score information for that
box.
"""
import urllib2
import urllib
import re
import ConfigParser
import time
#------Make some globals
config = ConfigParser.RawConfigParser()
scores = ConfigParser.RawConfigParser()
configfile =""
serverstocheck = ""
sleeptime = ""
outfile = ""
def getsettings():
print "Grabbing settings"
global configfile, serverstocheck, sleeptime, outfile
configfile = config.read("netkothconfig.ini")
serverstocheck = config.items("Servers To Check")
sleeptime = config.getint("General", "sleeptime")
outfile = config.get("General", "outfile")
def checkpagesandscore():
scoresfile = scores.read("netkothscores.txt")
for server in serverstocheck:
try:
print "About to check server " + server[0] + " " + server[1]
url = urllib2.urlopen(server[1],None,10)
#url = urllib2.urlopen(server[1])
html = url.read()
team = re.search('<team>(.*)</team>', html, re.IGNORECASE).group(1).strip().replace("=","").replace("<","").replace(">","")
print "Server " + server[0] + " owned by " + team
serverscoressection = server[0]+"Scores"
if not scores.has_option("TotalScores", team):
scores.set("TotalScores", team, 0)
currentscore = scores.getint( "TotalScores",team)
scores.set( "TotalScores", team, currentscore+1)
if not scores.has_option(serverscoressection, team):
scores.set(serverscoressection, team, 0)
currentscore = scores.getint( serverscoressection,team)
scores.set( serverscoressection, team, currentscore+1)
except IOError:
print server[0] + " " + server[1] + " may be down, skipping it"
except AttributeError:
print server[0] + " may not be owned yet"
with open("netkothscores.txt", 'wb') as scoresfile:
scores.write(scoresfile)
def makescoresections():
scoresfile = scores.read("netkothscores.txt")
if not scores.has_section("TotalScores"):
scores.add_section("TotalScores")
for server in serverstocheck:
serverscoressection = server[0]+"Scores"
if not scores.has_section(serverscoressection):
scores.add_section(serverscoressection)
def maketables(server):
print "Making score table for " + server[0]
try:
serverscoressection = server[0]+"Scores"
serverscores = scores.items(serverscoressection)
tableresults = "<div id=\"" + server[0] + "\">"
tableresults = tableresults + "<table border=\"2\">\n<tr>"
tableresults = tableresults + "<td colspan=\"2\"><center><b class=\"scoretabletitle\">" +(server[0]).title() + "</b><br>"
tableresults = tableresults + "<a href=\"" + server[1] + "\">" + server[1] +"</a>"
tableresults = tableresults + "</center></td>"
tableresults = tableresults + "</tr>\n"
serverscores.sort(key=lambda score: -int(score[1]))
toptagstart="<div class=\"topscore\">"
toptagend="</div>"
for team in serverscores:
tableresults = tableresults + "<tr><td>" + toptagstart + team[0].title() + toptagend + "</td><td>" + toptagstart + str(team[1]) + toptagend + "</td></tr>\n"
toptagstart="<div class=\"otherscore\">"
toptagend="</div>"
tableresults = tableresults + "</table></div>"
return tableresults
except:
print "No section for " + server[0]
#------Main begin
while 1:
#------Check files that may have changed since las loop
getsettings() #-------Grab core config values, you have the option to edit config file as the game runs
makescoresections() #In case score setions for a bax are not there
templatefilehandle = open("template.htm", 'r')
scorepagestring=templatefilehandle.read()
#------Look at all the pages to see who owns them.
checkpagesandscore()
#------Make Tables
for server in serverstocheck:
thistable = maketables(server)
serverlabeltag=("<" + server[0] + ">").upper()
print "Searching for " + serverlabeltag + " tag to replace in template.htm (case sensitive)"
scorepagestring = scorepagestring.replace(serverlabeltag,thistable)
#------Make Total Table
thistable = maketables(["Total",""])
serverlabeltag=("<TOTAL>").upper()
print "Searching for " + serverlabeltag + " to replace (case sensitive)"
scorepagestring = scorepagestring.replace(serverlabeltag,thistable)
#------Making the score page
print "Writing " + outfile
outfilehandle = open(outfile, 'w')
outfilehandle.write(scorepagestring)
outfilehandle.close()
print "Sleeping for " + str(sleeptime)
time.sleep(sleeptime)
#------Main end