annotate scrape-gm.py @ 11:22a51e8c0a69

Update copyright date. Remove CVS ID.
author darius@inchoate.localdomain
date Fri, 29 Feb 2008 21:05:14 +1030
parents 0e18c714b69d
children ae9e833e4447
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;')
ac32969d1bec Initial revision
darius
parents:
diff changeset
41
ac32969d1bec Initial revision
darius
parents:
diff changeset
42 def __init__(self, description = "", ip = "", port = 0, mapname = "",
ac32969d1bec Initial revision
darius
parents:
diff changeset
43 updateage = 0, numplayers = 0, maxplayers = 0, players = []):
ac32969d1bec Initial revision
darius
parents:
diff changeset
44 self.description = description
ac32969d1bec Initial revision
darius
parents:
diff changeset
45 self.ip = ip
ac32969d1bec Initial revision
darius
parents:
diff changeset
46 self.port = port
ac32969d1bec Initial revision
darius
parents:
diff changeset
47 self.mapname = mapname
ac32969d1bec Initial revision
darius
parents:
diff changeset
48 self.updateage = int(updateage)
ac32969d1bec Initial revision
darius
parents:
diff changeset
49 self.players = []
ac32969d1bec Initial revision
darius
parents:
diff changeset
50 self.numplayers = numplayers
ac32969d1bec Initial revision
darius
parents:
diff changeset
51 self.maxplayers = maxplayers
ac32969d1bec Initial revision
darius
parents:
diff changeset
52
ac32969d1bec Initial revision
darius
parents:
diff changeset
53 def __init__(self, pcols, scols):
ac32969d1bec Initial revision
darius
parents:
diff changeset
54 # pcols[2] = Player name
ac32969d1bec Initial revision
darius
parents:
diff changeset
55 # pcols[3] = Server description
ac32969d1bec Initial revision
darius
parents:
diff changeset
56 # scols[0] = Players in server / max players
ac32969d1bec Initial revision
darius
parents:
diff changeset
57 # scols[2] = Server IP
ac32969d1bec Initial revision
darius
parents:
diff changeset
58 # scols[3] = Server port
ac32969d1bec Initial revision
darius
parents:
diff changeset
59 # scols[4] = Map name
ac32969d1bec Initial revision
darius
parents:
diff changeset
60 # scols[10] = Update age
ac32969d1bec Initial revision
darius
parents:
diff changeset
61 self.tuplere = re.compile("\[?([0-9]+)/([0-9]+)\]?")
ac32969d1bec Initial revision
darius
parents:
diff changeset
62 self.description = pcols[3]
ac32969d1bec Initial revision
darius
parents:
diff changeset
63 self.ip = scols[2]
ac32969d1bec Initial revision
darius
parents:
diff changeset
64 self.port = int(scols[3])
ac32969d1bec Initial revision
darius
parents:
diff changeset
65 self.mapname = scols[4]
ac32969d1bec Initial revision
darius
parents:
diff changeset
66 self.updateage = scols[10]
ac32969d1bec Initial revision
darius
parents:
diff changeset
67 m = self.tuplere.match(scols[0])
ac32969d1bec Initial revision
darius
parents:
diff changeset
68 if (m == None):
ac32969d1bec Initial revision
darius
parents:
diff changeset
69 raise SyntaxError
ac32969d1bec Initial revision
darius
parents:
diff changeset
70
ac32969d1bec Initial revision
darius
parents:
diff changeset
71 self.numplayers = int(m.group(1))
ac32969d1bec Initial revision
darius
parents:
diff changeset
72 self.maxplayers = int(m.group(2))
ac32969d1bec Initial revision
darius
parents:
diff changeset
73 self.players = []
ac32969d1bec Initial revision
darius
parents:
diff changeset
74
ac32969d1bec Initial revision
darius
parents:
diff changeset
75 def __str__(self):
ac32969d1bec Initial revision
darius
parents:
diff changeset
76 plist = ""
ac32969d1bec Initial revision
darius
parents:
diff changeset
77 for p in self.players:
ac32969d1bec Initial revision
darius
parents:
diff changeset
78 plist = plist + " " + str(p)
ac32969d1bec Initial revision
darius
parents:
diff changeset
79
7
825302e32c35 Print out the server IP & port tuple as well as the name.
darius@inchoate.localdomain
parents: 6
diff changeset
80 return "%s (%s:%d) | Map: %s | Players: %d/%d : %s (%s old)" % \
825302e32c35 Print out the server IP & port tuple as well as the name.
darius@inchoate.localdomain
parents: 6
diff changeset
81 (self.description, self.ip, self.port, self.mapname,
825302e32c35 Print out the server IP & port tuple as well as the name.
darius@inchoate.localdomain
parents: 6
diff changeset
82 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
83 self.updateage)
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
84
ac32969d1bec Initial revision
darius
parents:
diff changeset
85 def GetTuple(scols):
ac32969d1bec Initial revision
darius
parents:
diff changeset
86 return str(scols[2]) + ":" + str(scols[3])
ac32969d1bec Initial revision
darius
parents:
diff changeset
87 GetTuple = staticmethod(GetTuple)
ac32969d1bec Initial revision
darius
parents:
diff changeset
88
ac32969d1bec Initial revision
darius
parents:
diff changeset
89 def FixTags(s):
ac32969d1bec Initial revision
darius
parents:
diff changeset
90 s = re.sub(Server.vwhttags, '\n', s)
ac32969d1bec Initial revision
darius
parents:
diff changeset
91 s = re.sub(Server.hwhttags, '', s)
ac32969d1bec Initial revision
darius
parents:
diff changeset
92 s = str(BeautifulSoup.BeautifulStoneSoup( \
ac32969d1bec Initial revision
darius
parents:
diff changeset
93 s, convertEntities = BeautifulSoup.BeautifulStoneSoup.XML_ENTITIES))
ac32969d1bec Initial revision
darius
parents:
diff changeset
94 s = re.sub(Server.alltags, '', s)
ac32969d1bec Initial revision
darius
parents:
diff changeset
95 return(s)
ac32969d1bec Initial revision
darius
parents:
diff changeset
96 FixTags = staticmethod(FixTags)
ac32969d1bec Initial revision
darius
parents:
diff changeset
97
ac32969d1bec Initial revision
darius
parents:
diff changeset
98 def Scrape(handle):
ac32969d1bec Initial revision
darius
parents:
diff changeset
99 s = BeautifulSoup.BeautifulSoup(handle)
ac32969d1bec Initial revision
darius
parents:
diff changeset
100
10
0e18c714b69d Update for new page layout.
darius@inchoate.localdomain
parents: 8
diff changeset
101 playertbl = s.find("table", "results")
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
102 if (playertbl == None):
6
5058c2695109 Properly report if we can't parse the results.
darius
parents: 5
diff changeset
103 #print "Unable to find results"
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
104 return None
ac32969d1bec Initial revision
darius
parents:
diff changeset
105
10
0e18c714b69d Update for new page layout.
darius@inchoate.localdomain
parents: 8
diff changeset
106 servertbl = playertbl.findNext("table", "results")
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
107
ac32969d1bec Initial revision
darius
parents:
diff changeset
108 playerrows = playertbl.findAll("tr")
ac32969d1bec Initial revision
darius
parents:
diff changeset
109 serverrows = servertbl.findAll("tr")
ac32969d1bec Initial revision
darius
parents:
diff changeset
110
ac32969d1bec Initial revision
darius
parents:
diff changeset
111 if (len(playerrows) != len(serverrows)):
ac32969d1bec Initial revision
darius
parents:
diff changeset
112 print "Internal error 41223"
ac32969d1bec Initial revision
darius
parents:
diff changeset
113 return
ac32969d1bec Initial revision
darius
parents:
diff changeset
114
ac32969d1bec Initial revision
darius
parents:
diff changeset
115 servers = {}
ac32969d1bec Initial revision
darius
parents:
diff changeset
116 for i in range(len(playerrows[1:])):
ac32969d1bec Initial revision
darius
parents:
diff changeset
117 pcols = playerrows[i].findAll('td')
ac32969d1bec Initial revision
darius
parents:
diff changeset
118 scols = serverrows[i].findAll('td')
ac32969d1bec Initial revision
darius
parents:
diff changeset
119 if (len(pcols) != 4):
ac32969d1bec Initial revision
darius
parents:
diff changeset
120 continue
ac32969d1bec Initial revision
darius
parents:
diff changeset
121
ac32969d1bec Initial revision
darius
parents:
diff changeset
122 pcols = map(lambda c : Server.FixTags(str(c)), pcols)
ac32969d1bec Initial revision
darius
parents:
diff changeset
123 scols = map(lambda c : Server.FixTags(str(c)), scols)
ac32969d1bec Initial revision
darius
parents:
diff changeset
124
ac32969d1bec Initial revision
darius
parents:
diff changeset
125 stuple = Server.GetTuple(scols)
ac32969d1bec Initial revision
darius
parents:
diff changeset
126
ac32969d1bec Initial revision
darius
parents:
diff changeset
127 if (stuple not in servers):
ac32969d1bec Initial revision
darius
parents:
diff changeset
128 s = Server(pcols, scols)
ac32969d1bec Initial revision
darius
parents:
diff changeset
129 servers[stuple] = s
ac32969d1bec Initial revision
darius
parents:
diff changeset
130
ac32969d1bec Initial revision
darius
parents:
diff changeset
131 servers[stuple].addplayer(pcols[2])
ac32969d1bec Initial revision
darius
parents:
diff changeset
132
ac32969d1bec Initial revision
darius
parents:
diff changeset
133 return servers
ac32969d1bec Initial revision
darius
parents:
diff changeset
134 Scrape = staticmethod(Scrape)
ac32969d1bec Initial revision
darius
parents:
diff changeset
135
ac32969d1bec Initial revision
darius
parents:
diff changeset
136 def addplayer(self, pname):
ac32969d1bec Initial revision
darius
parents:
diff changeset
137 self.players.append(pname)
ac32969d1bec Initial revision
darius
parents:
diff changeset
138
ac32969d1bec Initial revision
darius
parents:
diff changeset
139
ac32969d1bec Initial revision
darius
parents:
diff changeset
140 if (1):
ac32969d1bec Initial revision
darius
parents:
diff changeset
141 maxhits = 10
ac32969d1bec Initial revision
darius
parents:
diff changeset
142 if (len(sys.argv) < 2):
ac32969d1bec Initial revision
darius
parents:
diff changeset
143 print "Bad usage"
ac32969d1bec Initial revision
darius
parents:
diff changeset
144 print sys.argv[0] + "search_string"
ac32969d1bec Initial revision
darius
parents:
diff changeset
145 sys.exit(1)
ac32969d1bec Initial revision
darius
parents:
diff changeset
146
ac32969d1bec Initial revision
darius
parents:
diff changeset
147 try:
ac32969d1bec Initial revision
darius
parents:
diff changeset
148 #f = open("gm.html")
8
fbaf34d7bab7 Only list Australian servers.
darius@inchoate.localdomain
parents: 7
diff changeset
149 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
150 except IOError, e:
ac32969d1bec Initial revision
darius
parents:
diff changeset
151 print "Unable to fetch page - " + str(e)
ac32969d1bec Initial revision
darius
parents:
diff changeset
152 sys.exit(0)
ac32969d1bec Initial revision
darius
parents:
diff changeset
153
ac32969d1bec Initial revision
darius
parents:
diff changeset
154 servers = Server.Scrape(f)
ac32969d1bec Initial revision
darius
parents:
diff changeset
155 del f
6
5058c2695109 Properly report if we can't parse the results.
darius
parents: 5
diff changeset
156 if (servers == None):
5058c2695109 Properly report if we can't parse the results.
darius
parents: 5
diff changeset
157 print "No results available, please check manually"
5058c2695109 Properly report if we can't parse the results.
darius
parents: 5
diff changeset
158 elif (len(servers) == 0):
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
159 print "No players found"
ac32969d1bec Initial revision
darius
parents:
diff changeset
160 else:
ac32969d1bec Initial revision
darius
parents:
diff changeset
161 i = 0
ac32969d1bec Initial revision
darius
parents:
diff changeset
162 for s in servers:
ac32969d1bec Initial revision
darius
parents:
diff changeset
163 i = i + 1
ac32969d1bec Initial revision
darius
parents:
diff changeset
164 print servers[s]
ac32969d1bec Initial revision
darius
parents:
diff changeset
165 if (i >= maxhits):
ac32969d1bec Initial revision
darius
parents:
diff changeset
166 print "*** Stopping after " + str(maxhits) + " hits"
ac32969d1bec Initial revision
darius
parents:
diff changeset
167 break