Mercurial > ~darius > hgwebdir.cgi > scrape-gm
comparison scrape-gm.py @ 16:eeee17d2072c default tip
Modify to search for a given server (IP:port).
Player names are not returned in this case.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Fri, 16 Oct 2009 23:41:42 +1030 |
parents | 789cf10ce4c9 |
children |
comparison
equal
deleted
inserted
replaced
15:789cf10ce4c9 | 16:eeee17d2072c |
---|---|
52 self.players = [] | 52 self.players = [] |
53 self.numplayers = numplayers | 53 self.numplayers = numplayers |
54 self.maxplayers = maxplayers | 54 self.maxplayers = maxplayers |
55 | 55 |
56 def __init__(self, pcols, scols): | 56 def __init__(self, pcols, scols): |
57 # For a server search.. | |
58 # pcols[1] = Server description | |
59 # For a player search.. | |
57 # pcols[1] = Player name | 60 # pcols[1] = Player name |
58 # pcols[2] = Server description | 61 # pcols[2] = Server description |
59 # scols[0] = Players in server / max players | 62 # scols[0] = Players in server / max players |
60 # scols[1] = Server IP & port | 63 # scols[1] = Server IP & port |
61 # scols[2] = Map name | 64 # scols[2] = Map name |
67 | 70 |
68 self.pcountre = re.compile("([0-9]+)/([0-9]+)") | 71 self.pcountre = re.compile("([0-9]+)/([0-9]+)") |
69 self.ipportre = re.compile("([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+):([0-9]+)") | 72 self.ipportre = re.compile("([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+):([0-9]+)") |
70 self.sdesc = re.compile(" +[0-9]+\. +(.*)") | 73 self.sdesc = re.compile(" +[0-9]+\. +(.*)") |
71 | 74 |
72 m = self.sdesc.match(pcols[2]) | 75 if len(pcols) == 3: |
76 m = self.sdesc.match(pcols[2]) | |
77 else: | |
78 m = self.sdesc.match(pcols[1]) | |
79 | |
73 if (m == None): | 80 if (m == None): |
74 raise SyntaxError | 81 raise SyntaxError |
75 self.description = m.group(1) | 82 self.description = m.group(1) |
76 | 83 |
77 m = self.ipportre.match(scols[1]) | 84 m = self.ipportre.match(scols[1]) |
91 self.maxplayers = int(m.group(2)) | 98 self.maxplayers = int(m.group(2)) |
92 self.players = [] | 99 self.players = [] |
93 | 100 |
94 def __str__(self): | 101 def __str__(self): |
95 plist = "" | 102 plist = "" |
96 for p in self.players: | 103 if len(self.players) > 0: |
97 plist = plist + " " + str(p) | 104 plist = ": " |
98 | 105 for p in self.players: |
99 return "%s: %s (%s:%d) | Map: %s | Players: %d/%d : %s (%s old)" % \ | 106 plist = plist + " " + str(p) |
107 plist = plist + " " | |
108 | |
109 return "%s: %s (%s:%d) | Map: %s | Players: %d/%d %s(%s old)" % \ | |
100 (self.gametype, self.description, self.ip, self.port, self.mapname, | 110 (self.gametype, self.description, self.ip, self.port, self.mapname, |
101 self.numplayers, self.maxplayers, plist, | 111 self.numplayers, self.maxplayers, plist, |
102 self.updateage) | 112 self.updateage) |
103 | 113 |
104 def GetTuple(scols): | 114 def GetTuple(scols): |
138 | 148 |
139 servers = {} | 149 servers = {} |
140 for i in range(len(playerrows[1:])): | 150 for i in range(len(playerrows[1:])): |
141 pcols = playerrows[i].findAll('td') | 151 pcols = playerrows[i].findAll('td') |
142 scols = serverrows[i].findAll('td') | 152 scols = serverrows[i].findAll('td') |
143 if (len(pcols) != 3): | 153 if len(pcols) != 3 and len(pcols) != 2: |
144 if debug: | 154 if debug: |
145 print "pcols has length %d, expected 3" % len(pcols) | 155 print "pcols has length %d, expected 2 or 3" % len(pcols) |
146 continue | 156 continue |
147 | 157 |
148 pcols = map(lambda c : Server.FixTags(str(c)), pcols) | 158 pcols = map(lambda c : Server.FixTags(str(c)), pcols) |
149 scols = map(lambda c : Server.FixTags(str(c)), scols) | 159 scols = map(lambda c : Server.FixTags(str(c)), scols) |
150 | 160 |
152 | 162 |
153 if (stuple not in servers): | 163 if (stuple not in servers): |
154 s = Server(pcols, scols) | 164 s = Server(pcols, scols) |
155 servers[stuple] = s | 165 servers[stuple] = s |
156 | 166 |
157 servers[stuple].addplayer(pcols[1]) | 167 # Only add players if it's a player query |
168 if len(pcols) == 3: | |
169 servers[stuple].addplayer(pcols[1]) | |
158 | 170 |
159 return servers | 171 return servers |
160 Scrape = staticmethod(Scrape) | 172 Scrape = staticmethod(Scrape) |
161 | 173 |
162 def addplayer(self, pname): | 174 def addplayer(self, pname): |
163 self.players.append(pname) | 175 self.players.append(pname) |
164 | 176 |
165 | 177 |
178 isipadrre = re.compile('[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+') | |
179 | |
166 if True: | 180 if True: |
167 maxhits = 10 | 181 maxhits = 10 |
168 if (len(sys.argv) < 2): | 182 if (len(sys.argv) < 2): |
169 print "Bad usage" | 183 print "Bad usage" |
170 print sys.argv[0] + "search_string" | 184 print sys.argv[0] + "search_string" |
171 sys.exit(1) | 185 sys.exit(1) |
172 | 186 |
173 try: | 187 try: |
174 #f = open("gm.html") | 188 #f = open("gm.html") |
175 f = urllib.urlopen("http://www.game-monitor.com/search.php?location=AU&search=" + urllib.quote(sys.argv[1]) + "&type=player&location=AU") | 189 if isipadrre.match(sys.argv[1]) == None: |
190 f = urllib.urlopen("http://www.game-monitor.com/search.php?location=AU&search=" + urllib.quote(sys.argv[1]) + "&type=player&location=AU") | |
191 else: | |
192 f = urllib.urlopen("http://www.game-monitor.com/search.php?location=AU&search=" + urllib.quote(sys.argv[1])) | |
176 except IOError, e: | 193 except IOError, e: |
177 print "Unable to fetch page - " + str(e) | 194 print "Unable to fetch page - " + str(e) |
178 sys.exit(0) | 195 sys.exit(0) |
179 | 196 |
180 servers = Server.Scrape(f) | 197 servers = Server.Scrape(f) |