diff 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
line wrap: on
line diff
--- a/scrape-gm.py	Sun Dec 14 18:55:39 2008 +1030
+++ b/scrape-gm.py	Fri Oct 16 23:41:42 2009 +1030
@@ -54,6 +54,9 @@
         self.maxplayers = maxplayers
 
     def __init__(self, pcols, scols):
+        # For a server search..
+        # pcols[1] = Server description
+        # For a player search..
         # pcols[1] = Player name
         # pcols[2] = Server description
         # scols[0] = Players in server / max players
@@ -69,7 +72,11 @@
         self.ipportre = re.compile("([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+):([0-9]+)")
         self.sdesc = re.compile(" +[0-9]+\. +(.*)")
 
-        m = self.sdesc.match(pcols[2])
+        if len(pcols) == 3:
+            m = self.sdesc.match(pcols[2])
+        else:
+            m = self.sdesc.match(pcols[1])
+
         if (m == None):
             raise SyntaxError
         self.description = m.group(1)
@@ -93,10 +100,13 @@
 
     def __str__(self):
         plist = ""
-        for p in self.players:
-            plist = plist + " " + str(p)
-        
-        return "%s: %s (%s:%d) | Map: %s | Players: %d/%d : %s (%s old)" % \
+        if len(self.players) > 0:
+            plist = ": "
+            for p in self.players:
+                plist = plist + " " + str(p)
+            plist = plist + " "
+
+        return "%s: %s (%s:%d) | Map: %s | Players: %d/%d %s(%s old)" % \
                (self.gametype, self.description, self.ip, self.port, self.mapname,
                 self.numplayers, self.maxplayers, plist,
                 self.updateage)
@@ -140,9 +150,9 @@
         for i in range(len(playerrows[1:])):
             pcols = playerrows[i].findAll('td')
             scols = serverrows[i].findAll('td')
-            if (len(pcols) != 3):
+            if len(pcols) != 3 and len(pcols) != 2:
                 if debug:
-                    print "pcols has length %d, expected 3" % len(pcols)
+                    print "pcols has length %d, expected 2 or 3" % len(pcols)
                 continue
         
             pcols = map(lambda c : Server.FixTags(str(c)), pcols)
@@ -154,7 +164,9 @@
                 s = Server(pcols, scols)
                 servers[stuple] = s
             
-            servers[stuple].addplayer(pcols[1])
+            # Only add players if it's a player query
+            if len(pcols) == 3:
+                servers[stuple].addplayer(pcols[1])
 
         return servers
     Scrape = staticmethod(Scrape)
@@ -162,7 +174,9 @@
     def addplayer(self, pname):
         self.players.append(pname)
     
-    
+
+isipadrre = re.compile('[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+')  
+
 if True:
     maxhits = 10
     if (len(sys.argv) < 2):
@@ -172,7 +186,10 @@
     
     try:
         #f = open("gm.html")
-        f = urllib.urlopen("http://www.game-monitor.com/search.php?location=AU&search=" + urllib.quote(sys.argv[1]) + "&type=player&location=AU")
+        if isipadrre.match(sys.argv[1]) == None:
+            f = urllib.urlopen("http://www.game-monitor.com/search.php?location=AU&search=" + urllib.quote(sys.argv[1]) + "&type=player&location=AU")
+        else:
+            f = urllib.urlopen("http://www.game-monitor.com/search.php?location=AU&search=" + urllib.quote(sys.argv[1]))
     except IOError, e:
         print "Unable to fetch page - " + str(e)
         sys.exit(0)