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