comparison scrape-gm.py @ 12:ae9e833e4447

Get game type. Print game type at the beginning & sort output.
author darius@inchoate.localdomain
date Sat, 26 Jul 2008 20:47:08 +0930
parents 22a51e8c0a69
children e8550290e512
comparison
equal deleted inserted replaced
11:22a51e8c0a69 12:ae9e833e4447
36 36
37 class Server: 37 class Server:
38 alltags = re.compile('<[^>]*>') 38 alltags = re.compile('<[^>]*>')
39 vwhttags = re.compile('<(br|hr)>') 39 vwhttags = re.compile('<(br|hr)>')
40 hwhttags = re.compile('\&nbsp;') 40 hwhttags = re.compile('\&nbsp;')
41 41 typetag = re.compile('<td><a href="/GameSearch/([^/]+)/.*</td>')
42
42 def __init__(self, description = "", ip = "", port = 0, mapname = "", 43 def __init__(self, description = "", ip = "", port = 0, mapname = "",
43 updateage = 0, numplayers = 0, maxplayers = 0, players = []): 44 updateage = 0, numplayers = 0, maxplayers = 0, players = []):
44 self.description = description 45 self.description = description
45 self.ip = ip 46 self.ip = ip
46 self.port = port 47 self.port = port
55 # pcols[3] = Server description 56 # pcols[3] = Server description
56 # scols[0] = Players in server / max players 57 # scols[0] = Players in server / max players
57 # scols[2] = Server IP 58 # scols[2] = Server IP
58 # scols[3] = Server port 59 # scols[3] = Server port
59 # scols[4] = Map name 60 # scols[4] = Map name
61 # scols[5] = Game type
60 # scols[10] = Update age 62 # scols[10] = Update age
61 self.tuplere = re.compile("\[?([0-9]+)/([0-9]+)\]?") 63 self.tuplere = re.compile("\[?([0-9]+)/([0-9]+)\]?")
62 self.description = pcols[3] 64 self.description = pcols[3]
63 self.ip = scols[2] 65 self.ip = scols[2]
64 self.port = int(scols[3]) 66 self.port = int(scols[3])
65 self.mapname = scols[4] 67 self.mapname = scols[4]
68 self.gametype = scols[5]
66 self.updateage = scols[10] 69 self.updateage = scols[10]
67 m = self.tuplere.match(scols[0]) 70 m = self.tuplere.match(scols[0])
68 if (m == None): 71 if (m == None):
69 raise SyntaxError 72 raise SyntaxError
70 73
75 def __str__(self): 78 def __str__(self):
76 plist = "" 79 plist = ""
77 for p in self.players: 80 for p in self.players:
78 plist = plist + " " + str(p) 81 plist = plist + " " + str(p)
79 82
80 return "%s (%s:%d) | Map: %s | Players: %d/%d : %s (%s old)" % \ 83 return "%s: %s (%s:%d) | Map: %s | Players: %d/%d : %s (%s old)" % \
81 (self.description, self.ip, self.port, self.mapname, 84 (self.gametype, self.description, self.ip, self.port, self.mapname,
82 self.numplayers, self.maxplayers, plist, 85 self.numplayers, self.maxplayers, plist,
83 self.updateage) 86 self.updateage)
84 87
85 def GetTuple(scols): 88 def GetTuple(scols):
86 return str(scols[2]) + ":" + str(scols[3]) 89 return str(scols[2]) + ":" + str(scols[3])
87 GetTuple = staticmethod(GetTuple) 90 GetTuple = staticmethod(GetTuple)
88 91
89 def FixTags(s): 92 def FixTags(s):
93 # Mangle game type
94 t = Server.typetag.match(s)
95 if t != None:
96 s = t.group(1)
90 s = re.sub(Server.vwhttags, '\n', s) 97 s = re.sub(Server.vwhttags, '\n', s)
91 s = re.sub(Server.hwhttags, '', s) 98 s = re.sub(Server.hwhttags, '', s)
92 s = str(BeautifulSoup.BeautifulStoneSoup( \ 99 s = str(BeautifulSoup.BeautifulStoneSoup( \
93 s, convertEntities = BeautifulSoup.BeautifulStoneSoup.XML_ENTITIES)) 100 s, convertEntities = BeautifulSoup.BeautifulStoneSoup.XML_ENTITIES))
94 s = re.sub(Server.alltags, '', s) 101 s = re.sub(Server.alltags, '', s)
101 playertbl = s.find("table", "results") 108 playertbl = s.find("table", "results")
102 if (playertbl == None): 109 if (playertbl == None):
103 #print "Unable to find results" 110 #print "Unable to find results"
104 return None 111 return None
105 112
106 servertbl = playertbl.findNext("table", "results") 113 servertbl = playertbl.findNext("table")
107 114
108 playerrows = playertbl.findAll("tr") 115 playerrows = playertbl.findAll("tr")
109 serverrows = servertbl.findAll("tr") 116 serverrows = servertbl.findAll("tr")
110 117
111 if (len(playerrows) != len(serverrows)): 118 if (len(playerrows) != len(serverrows)):
156 if (servers == None): 163 if (servers == None):
157 print "No results available, please check manually" 164 print "No results available, please check manually"
158 elif (len(servers) == 0): 165 elif (len(servers) == 0):
159 print "No players found" 166 print "No players found"
160 else: 167 else:
168 tmp = []
169 for i in servers:
170 tmp.append(servers[i])
171 tmp.sort()
161 i = 0 172 i = 0
162 for s in servers: 173 for s in tmp:
163 i = i + 1 174 i = i + 1
164 print servers[s] 175 print s
165 if (i >= maxhits): 176 if (i >= maxhits):
166 print "*** Stopping after " + str(maxhits) + " hits" 177 print "*** Stopping after " + str(maxhits) + " hits"
167 break 178 break