annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
1 #!/usr/bin/env python
ac32969d1bec Initial revision
darius
parents:
diff changeset
2
ac32969d1bec Initial revision
darius
parents:
diff changeset
3 ############################################################################
ac32969d1bec Initial revision
darius
parents:
diff changeset
4 # Screen scraper for game-monitor.com
ac32969d1bec Initial revision
darius
parents:
diff changeset
5 #
ac32969d1bec Initial revision
darius
parents:
diff changeset
6 # Prints out matched player names agreated by server
ac32969d1bec Initial revision
darius
parents:
diff changeset
7 #
ac32969d1bec Initial revision
darius
parents:
diff changeset
8 ############################################################################
ac32969d1bec Initial revision
darius
parents:
diff changeset
9 #
11
22a51e8c0a69 Update copyright date. Remove CVS ID.
darius@inchoate.localdomain
parents: 10
diff changeset
10 # Copyright (C) 2008 Daniel O'Connor. All rights reserved.
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
11 #
ac32969d1bec Initial revision
darius
parents:
diff changeset
12 # Redistribution and use in source and binary forms, with or without
ac32969d1bec Initial revision
darius
parents:
diff changeset
13 # modification, are permitted provided that the following conditions
ac32969d1bec Initial revision
darius
parents:
diff changeset
14 # are met:
ac32969d1bec Initial revision
darius
parents:
diff changeset
15 # 1. Redistributions of source code must retain the above copyright
ac32969d1bec Initial revision
darius
parents:
diff changeset
16 # notice, this list of conditions and the following disclaimer.
ac32969d1bec Initial revision
darius
parents:
diff changeset
17 # 2. Redistributions in binary form must reproduce the above copyright
ac32969d1bec Initial revision
darius
parents:
diff changeset
18 # notice, this list of conditions and the following disclaimer in the
ac32969d1bec Initial revision
darius
parents:
diff changeset
19 # documentation and/or other materials provided with the distribution.
ac32969d1bec Initial revision
darius
parents:
diff changeset
20 #
ac32969d1bec Initial revision
darius
parents:
diff changeset
21 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ac32969d1bec Initial revision
darius
parents:
diff changeset
22 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ac32969d1bec Initial revision
darius
parents:
diff changeset
23 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ac32969d1bec Initial revision
darius
parents:
diff changeset
24 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
ac32969d1bec Initial revision
darius
parents:
diff changeset
25 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ac32969d1bec Initial revision
darius
parents:
diff changeset
26 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
ac32969d1bec Initial revision
darius
parents:
diff changeset
27 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ac32969d1bec Initial revision
darius
parents:
diff changeset
28 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
ac32969d1bec Initial revision
darius
parents:
diff changeset
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
ac32969d1bec Initial revision
darius
parents:
diff changeset
30 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
ac32969d1bec Initial revision
darius
parents:
diff changeset
31 # SUCH DAMAGE.
ac32969d1bec Initial revision
darius
parents:
diff changeset
32 #
ac32969d1bec Initial revision
darius
parents:
diff changeset
33 ############################################################################
ac32969d1bec Initial revision
darius
parents:
diff changeset
34
ac32969d1bec Initial revision
darius
parents:
diff changeset
35 import re, time, datetime, urllib, sys, BeautifulSoup
ac32969d1bec Initial revision
darius
parents:
diff changeset
36
ac32969d1bec Initial revision
darius
parents:
diff changeset
37 class Server:
ac32969d1bec Initial revision
darius
parents:
diff changeset
38 alltags = re.compile('<[^>]*>')
ac32969d1bec Initial revision
darius
parents:
diff changeset
39 vwhttags = re.compile('<(br|hr)>')
ac32969d1bec Initial revision
darius
parents:
diff changeset
40 hwhttags = re.compile('\&nbsp;')
12
ae9e833e4447 Get game type.
darius@inchoate.localdomain
parents: 11
diff changeset
41 typetag = re.compile('<td><a href="/GameSearch/([^/]+)/.*</td>')
ae9e833e4447 Get game type.
darius@inchoate.localdomain
parents: 11
diff changeset
42
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
43 def __init__(self, description = "", ip = "", port = 0, mapname = "",
ac32969d1bec Initial revision
darius
parents:
diff changeset
44 updateage = 0, numplayers = 0, maxplayers = 0, players = []):
ac32969d1bec Initial revision
darius
parents:
diff changeset
45 self.description = description
ac32969d1bec Initial revision
darius
parents:
diff changeset
46 self.ip = ip
ac32969d1bec Initial revision
darius
parents:
diff changeset
47 self.port = port
ac32969d1bec Initial revision
darius
parents:
diff changeset
48 self.mapname = mapname
ac32969d1bec Initial revision
darius
parents:
diff changeset
49 self.updateage = int(updateage)
ac32969d1bec Initial revision
darius
parents:
diff changeset
50 self.players = []
ac32969d1bec Initial revision
darius
parents:
diff changeset
51 self.numplayers = numplayers
ac32969d1bec Initial revision
darius
parents:
diff changeset
52 self.maxplayers = maxplayers
ac32969d1bec Initial revision
darius
parents:
diff changeset
53
ac32969d1bec Initial revision
darius
parents:
diff changeset
54 def __init__(self, pcols, scols):
ac32969d1bec Initial revision
darius
parents:
diff changeset
55 # pcols[2] = Player name
ac32969d1bec Initial revision
darius
parents:
diff changeset
56 # pcols[3] = Server description
ac32969d1bec Initial revision
darius
parents:
diff changeset
57 # scols[0] = Players in server / max players
ac32969d1bec Initial revision
darius
parents:
diff changeset
58 # scols[2] = Server IP
ac32969d1bec Initial revision
darius
parents:
diff changeset
59 # scols[3] = Server port
ac32969d1bec Initial revision
darius
parents:
diff changeset
60 # scols[4] = Map name
12
ae9e833e4447 Get game type.
darius@inchoate.localdomain
parents: 11
diff changeset
61 # scols[5] = Game type
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
62 # scols[10] = Update age
ac32969d1bec Initial revision
darius
parents:
diff changeset
63 self.tuplere = re.compile("\[?([0-9]+)/([0-9]+)\]?")
ac32969d1bec Initial revision
darius
parents:
diff changeset
64 self.description = pcols[3]
ac32969d1bec Initial revision
darius
parents:
diff changeset
65 self.ip = scols[2]
ac32969d1bec Initial revision
darius
parents:
diff changeset
66 self.port = int(scols[3])
ac32969d1bec Initial revision
darius
parents:
diff changeset
67 self.mapname = scols[4]
12
ae9e833e4447 Get game type.
darius@inchoate.localdomain
parents: 11
diff changeset
68 self.gametype = scols[5]
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
69 self.updateage = scols[10]
ac32969d1bec Initial revision
darius
parents:
diff changeset
70 m = self.tuplere.match(scols[0])
ac32969d1bec Initial revision
darius
parents:
diff changeset
71 if (m == None):
ac32969d1bec Initial revision
darius
parents:
diff changeset
72 raise SyntaxError
ac32969d1bec Initial revision
darius
parents:
diff changeset
73
ac32969d1bec Initial revision
darius
parents:
diff changeset
74 self.numplayers = int(m.group(1))
ac32969d1bec Initial revision
darius
parents:
diff changeset
75 self.maxplayers = int(m.group(2))
ac32969d1bec Initial revision
darius
parents:
diff changeset
76 self.players = []
ac32969d1bec Initial revision
darius
parents:
diff changeset
77
ac32969d1bec Initial revision
darius
parents:
diff changeset
78 def __str__(self):
ac32969d1bec Initial revision
darius
parents:
diff changeset
79 plist = ""
ac32969d1bec Initial revision
darius
parents:
diff changeset
80 for p in self.players:
ac32969d1bec Initial revision
darius
parents:
diff changeset
81 plist = plist + " " + str(p)
ac32969d1bec Initial revision
darius
parents:
diff changeset
82
12
ae9e833e4447 Get game type.
darius@inchoate.localdomain
parents: 11
diff changeset
83 return "%s: %s (%s:%d) | Map: %s | Players: %d/%d : %s (%s old)" % \
ae9e833e4447 Get game type.
darius@inchoate.localdomain
parents: 11
diff changeset
84 (self.gametype, self.description, self.ip, self.port, self.mapname,
7
825302e32c35 Print out the server IP & port tuple as well as the name.
darius@inchoate.localdomain
parents: 6
diff changeset
85 self.numplayers, self.maxplayers, plist,
825302e32c35 Print out the server IP & port tuple as well as the name.
darius@inchoate.localdomain
parents: 6
diff changeset
86 self.updateage)
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
87
ac32969d1bec Initial revision
darius
parents:
diff changeset
88 def GetTuple(scols):
ac32969d1bec Initial revision
darius
parents:
diff changeset
89 return str(scols[2]) + ":" + str(scols[3])
ac32969d1bec Initial revision
darius
parents:
diff changeset
90 GetTuple = staticmethod(GetTuple)
ac32969d1bec Initial revision
darius
parents:
diff changeset
91
ac32969d1bec Initial revision
darius
parents:
diff changeset
92 def FixTags(s):
12
ae9e833e4447 Get game type.
darius@inchoate.localdomain
parents: 11
diff changeset
93 # Mangle game type
ae9e833e4447 Get game type.
darius@inchoate.localdomain
parents: 11
diff changeset
94 t = Server.typetag.match(s)
ae9e833e4447 Get game type.
darius@inchoate.localdomain
parents: 11
diff changeset
95 if t != None:
ae9e833e4447 Get game type.
darius@inchoate.localdomain
parents: 11
diff changeset
96 s = t.group(1)
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
97 s = re.sub(Server.vwhttags, '\n', s)
ac32969d1bec Initial revision
darius
parents:
diff changeset
98 s = re.sub(Server.hwhttags, '', s)
ac32969d1bec Initial revision
darius
parents:
diff changeset
99 s = str(BeautifulSoup.BeautifulStoneSoup( \
ac32969d1bec Initial revision
darius
parents:
diff changeset
100 s, convertEntities = BeautifulSoup.BeautifulStoneSoup.XML_ENTITIES))
ac32969d1bec Initial revision
darius
parents:
diff changeset
101 s = re.sub(Server.alltags, '', s)
ac32969d1bec Initial revision
darius
parents:
diff changeset
102 return(s)
ac32969d1bec Initial revision
darius
parents:
diff changeset
103 FixTags = staticmethod(FixTags)
ac32969d1bec Initial revision
darius
parents:
diff changeset
104
ac32969d1bec Initial revision
darius
parents:
diff changeset
105 def Scrape(handle):
ac32969d1bec Initial revision
darius
parents:
diff changeset
106 s = BeautifulSoup.BeautifulSoup(handle)
ac32969d1bec Initial revision
darius
parents:
diff changeset
107
10
0e18c714b69d Update for new page layout.
darius@inchoate.localdomain
parents: 8
diff changeset
108 playertbl = s.find("table", "results")
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
109 if (playertbl == None):
6
5058c2695109 Properly report if we can't parse the results.
darius
parents: 5
diff changeset
110 #print "Unable to find results"
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
111 return None
ac32969d1bec Initial revision
darius
parents:
diff changeset
112
12
ae9e833e4447 Get game type.
darius@inchoate.localdomain
parents: 11
diff changeset
113 servertbl = playertbl.findNext("table")
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
114
ac32969d1bec Initial revision
darius
parents:
diff changeset
115 playerrows = playertbl.findAll("tr")
ac32969d1bec Initial revision
darius
parents:
diff changeset
116 serverrows = servertbl.findAll("tr")
ac32969d1bec Initial revision
darius
parents:
diff changeset
117
ac32969d1bec Initial revision
darius
parents:
diff changeset
118 if (len(playerrows) != len(serverrows)):
ac32969d1bec Initial revision
darius
parents:
diff changeset
119 print "Internal error 41223"
ac32969d1bec Initial revision
darius
parents:
diff changeset
120 return
ac32969d1bec Initial revision
darius
parents:
diff changeset
121
ac32969d1bec Initial revision
darius
parents:
diff changeset
122 servers = {}
ac32969d1bec Initial revision
darius
parents:
diff changeset
123 for i in range(len(playerrows[1:])):
ac32969d1bec Initial revision
darius
parents:
diff changeset
124 pcols = playerrows[i].findAll('td')
ac32969d1bec Initial revision
darius
parents:
diff changeset
125 scols = serverrows[i].findAll('td')
ac32969d1bec Initial revision
darius
parents:
diff changeset
126 if (len(pcols) != 4):
ac32969d1bec Initial revision
darius
parents:
diff changeset
127 continue
ac32969d1bec Initial revision
darius
parents:
diff changeset
128
ac32969d1bec Initial revision
darius
parents:
diff changeset
129 pcols = map(lambda c : Server.FixTags(str(c)), pcols)
ac32969d1bec Initial revision
darius
parents:
diff changeset
130 scols = map(lambda c : Server.FixTags(str(c)), scols)
ac32969d1bec Initial revision
darius
parents:
diff changeset
131
ac32969d1bec Initial revision
darius
parents:
diff changeset
132 stuple = Server.GetTuple(scols)
ac32969d1bec Initial revision
darius
parents:
diff changeset
133
ac32969d1bec Initial revision
darius
parents:
diff changeset
134 if (stuple not in servers):
ac32969d1bec Initial revision
darius
parents:
diff changeset
135 s = Server(pcols, scols)
ac32969d1bec Initial revision
darius
parents:
diff changeset
136 servers[stuple] = s
ac32969d1bec Initial revision
darius
parents:
diff changeset
137
ac32969d1bec Initial revision
darius
parents:
diff changeset
138 servers[stuple].addplayer(pcols[2])
ac32969d1bec Initial revision
darius
parents:
diff changeset
139
ac32969d1bec Initial revision
darius
parents:
diff changeset
140 return servers
ac32969d1bec Initial revision
darius
parents:
diff changeset
141 Scrape = staticmethod(Scrape)
ac32969d1bec Initial revision
darius
parents:
diff changeset
142
ac32969d1bec Initial revision
darius
parents:
diff changeset
143 def addplayer(self, pname):
ac32969d1bec Initial revision
darius
parents:
diff changeset
144 self.players.append(pname)
ac32969d1bec Initial revision
darius
parents:
diff changeset
145
ac32969d1bec Initial revision
darius
parents:
diff changeset
146
ac32969d1bec Initial revision
darius
parents:
diff changeset
147 if (1):
ac32969d1bec Initial revision
darius
parents:
diff changeset
148 maxhits = 10
ac32969d1bec Initial revision
darius
parents:
diff changeset
149 if (len(sys.argv) < 2):
ac32969d1bec Initial revision
darius
parents:
diff changeset
150 print "Bad usage"
ac32969d1bec Initial revision
darius
parents:
diff changeset
151 print sys.argv[0] + "search_string"
ac32969d1bec Initial revision
darius
parents:
diff changeset
152 sys.exit(1)
ac32969d1bec Initial revision
darius
parents:
diff changeset
153
ac32969d1bec Initial revision
darius
parents:
diff changeset
154 try:
ac32969d1bec Initial revision
darius
parents:
diff changeset
155 #f = open("gm.html")
8
fbaf34d7bab7 Only list Australian servers.
darius@inchoate.localdomain
parents: 7
diff changeset
156 f = urllib.urlopen("http://www.game-monitor.com/search.php?location=AU&search=" + urllib.quote(sys.argv[1]) + "&type=player&location=AU")
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
157 except IOError, e:
ac32969d1bec Initial revision
darius
parents:
diff changeset
158 print "Unable to fetch page - " + str(e)
ac32969d1bec Initial revision
darius
parents:
diff changeset
159 sys.exit(0)
ac32969d1bec Initial revision
darius
parents:
diff changeset
160
ac32969d1bec Initial revision
darius
parents:
diff changeset
161 servers = Server.Scrape(f)
ac32969d1bec Initial revision
darius
parents:
diff changeset
162 del f
6
5058c2695109 Properly report if we can't parse the results.
darius
parents: 5
diff changeset
163 if (servers == None):
5058c2695109 Properly report if we can't parse the results.
darius
parents: 5
diff changeset
164 print "No results available, please check manually"
5058c2695109 Properly report if we can't parse the results.
darius
parents: 5
diff changeset
165 elif (len(servers) == 0):
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
166 print "No players found"
ac32969d1bec Initial revision
darius
parents:
diff changeset
167 else:
12
ae9e833e4447 Get game type.
darius@inchoate.localdomain
parents: 11
diff changeset
168 tmp = []
ae9e833e4447 Get game type.
darius@inchoate.localdomain
parents: 11
diff changeset
169 for i in servers:
ae9e833e4447 Get game type.
darius@inchoate.localdomain
parents: 11
diff changeset
170 tmp.append(servers[i])
ae9e833e4447 Get game type.
darius@inchoate.localdomain
parents: 11
diff changeset
171 tmp.sort()
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
172 i = 0
12
ae9e833e4447 Get game type.
darius@inchoate.localdomain
parents: 11
diff changeset
173 for s in tmp:
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
174 i = i + 1
12
ae9e833e4447 Get game type.
darius@inchoate.localdomain
parents: 11
diff changeset
175 print s
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
176 if (i >= maxhits):
ac32969d1bec Initial revision
darius
parents:
diff changeset
177 print "*** Stopping after " + str(maxhits) + " hits"
ac32969d1bec Initial revision
darius
parents:
diff changeset
178 break