annotate scrape-gm.py @ 9:3ef64337d86b

- Listen on #tea & #teabf - Only match !fp at the beginning of the line - Allow users to specify a search string but default to [tea] if none is specified.
author darius@inchoate.localdomain
date Fri, 29 Feb 2008 10:20:28 +1030
parents fbaf34d7bab7
children 0e18c714b69d
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 #
6
5058c2695109 Properly report if we can't parse the results.
darius
parents: 5
diff changeset
8 # $Id: scrape-gm.py,v 1.3 2007/11/18 08:54:07 darius Exp $
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
9 ############################################################################
ac32969d1bec Initial revision
darius
parents:
diff changeset
10 #
ac32969d1bec Initial revision
darius
parents:
diff changeset
11 # Copyright (C) 2007 Daniel O'Connor. All rights reserved.
ac32969d1bec Initial revision
darius
parents:
diff changeset
12 #
ac32969d1bec Initial revision
darius
parents:
diff changeset
13 # Redistribution and use in source and binary forms, with or without
ac32969d1bec Initial revision
darius
parents:
diff changeset
14 # modification, are permitted provided that the following conditions
ac32969d1bec Initial revision
darius
parents:
diff changeset
15 # are met:
ac32969d1bec Initial revision
darius
parents:
diff changeset
16 # 1. Redistributions of source code must retain the above copyright
ac32969d1bec Initial revision
darius
parents:
diff changeset
17 # notice, this list of conditions and the following disclaimer.
ac32969d1bec Initial revision
darius
parents:
diff changeset
18 # 2. Redistributions in binary form must reproduce the above copyright
ac32969d1bec Initial revision
darius
parents:
diff changeset
19 # notice, this list of conditions and the following disclaimer in the
ac32969d1bec Initial revision
darius
parents:
diff changeset
20 # documentation and/or other materials provided with the distribution.
ac32969d1bec Initial revision
darius
parents:
diff changeset
21 #
ac32969d1bec Initial revision
darius
parents:
diff changeset
22 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ac32969d1bec Initial revision
darius
parents:
diff changeset
23 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ac32969d1bec Initial revision
darius
parents:
diff changeset
24 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ac32969d1bec Initial revision
darius
parents:
diff changeset
25 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
ac32969d1bec Initial revision
darius
parents:
diff changeset
26 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ac32969d1bec Initial revision
darius
parents:
diff changeset
27 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
ac32969d1bec Initial revision
darius
parents:
diff changeset
28 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ac32969d1bec Initial revision
darius
parents:
diff changeset
29 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
ac32969d1bec Initial revision
darius
parents:
diff changeset
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
ac32969d1bec Initial revision
darius
parents:
diff changeset
31 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
ac32969d1bec Initial revision
darius
parents:
diff changeset
32 # SUCH DAMAGE.
ac32969d1bec Initial revision
darius
parents:
diff changeset
33 #
ac32969d1bec Initial revision
darius
parents:
diff changeset
34 ############################################################################
ac32969d1bec Initial revision
darius
parents:
diff changeset
35
ac32969d1bec Initial revision
darius
parents:
diff changeset
36 import re, time, datetime, urllib, sys, BeautifulSoup
ac32969d1bec Initial revision
darius
parents:
diff changeset
37
ac32969d1bec Initial revision
darius
parents:
diff changeset
38 class Server:
ac32969d1bec Initial revision
darius
parents:
diff changeset
39 alltags = re.compile('<[^>]*>')
ac32969d1bec Initial revision
darius
parents:
diff changeset
40 vwhttags = re.compile('<(br|hr)>')
ac32969d1bec Initial revision
darius
parents:
diff changeset
41 hwhttags = re.compile('\&nbsp;')
ac32969d1bec Initial revision
darius
parents:
diff changeset
42
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
ac32969d1bec Initial revision
darius
parents:
diff changeset
61 # scols[10] = Update age
ac32969d1bec Initial revision
darius
parents:
diff changeset
62 self.tuplere = re.compile("\[?([0-9]+)/([0-9]+)\]?")
ac32969d1bec Initial revision
darius
parents:
diff changeset
63 self.description = pcols[3]
ac32969d1bec Initial revision
darius
parents:
diff changeset
64 self.ip = scols[2]
ac32969d1bec Initial revision
darius
parents:
diff changeset
65 self.port = int(scols[3])
ac32969d1bec Initial revision
darius
parents:
diff changeset
66 self.mapname = scols[4]
ac32969d1bec Initial revision
darius
parents:
diff changeset
67 self.updateage = scols[10]
ac32969d1bec Initial revision
darius
parents:
diff changeset
68 m = self.tuplere.match(scols[0])
ac32969d1bec Initial revision
darius
parents:
diff changeset
69 if (m == None):
ac32969d1bec Initial revision
darius
parents:
diff changeset
70 raise SyntaxError
ac32969d1bec Initial revision
darius
parents:
diff changeset
71
ac32969d1bec Initial revision
darius
parents:
diff changeset
72 self.numplayers = int(m.group(1))
ac32969d1bec Initial revision
darius
parents:
diff changeset
73 self.maxplayers = int(m.group(2))
ac32969d1bec Initial revision
darius
parents:
diff changeset
74 self.players = []
ac32969d1bec Initial revision
darius
parents:
diff changeset
75
ac32969d1bec Initial revision
darius
parents:
diff changeset
76 def __str__(self):
ac32969d1bec Initial revision
darius
parents:
diff changeset
77 plist = ""
ac32969d1bec Initial revision
darius
parents:
diff changeset
78 for p in self.players:
ac32969d1bec Initial revision
darius
parents:
diff changeset
79 plist = plist + " " + str(p)
ac32969d1bec Initial revision
darius
parents:
diff changeset
80
7
825302e32c35 Print out the server IP & port tuple as well as the name.
darius@inchoate.localdomain
parents: 6
diff changeset
81 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
82 (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
83 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
84 self.updateage)
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
85
ac32969d1bec Initial revision
darius
parents:
diff changeset
86 def GetTuple(scols):
ac32969d1bec Initial revision
darius
parents:
diff changeset
87 return str(scols[2]) + ":" + str(scols[3])
ac32969d1bec Initial revision
darius
parents:
diff changeset
88 GetTuple = staticmethod(GetTuple)
ac32969d1bec Initial revision
darius
parents:
diff changeset
89
ac32969d1bec Initial revision
darius
parents:
diff changeset
90 def FixTags(s):
ac32969d1bec Initial revision
darius
parents:
diff changeset
91 s = re.sub(Server.vwhttags, '\n', s)
ac32969d1bec Initial revision
darius
parents:
diff changeset
92 s = re.sub(Server.hwhttags, '', s)
ac32969d1bec Initial revision
darius
parents:
diff changeset
93 s = str(BeautifulSoup.BeautifulStoneSoup( \
ac32969d1bec Initial revision
darius
parents:
diff changeset
94 s, convertEntities = BeautifulSoup.BeautifulStoneSoup.XML_ENTITIES))
ac32969d1bec Initial revision
darius
parents:
diff changeset
95 s = re.sub(Server.alltags, '', s)
ac32969d1bec Initial revision
darius
parents:
diff changeset
96 return(s)
ac32969d1bec Initial revision
darius
parents:
diff changeset
97 FixTags = staticmethod(FixTags)
ac32969d1bec Initial revision
darius
parents:
diff changeset
98
ac32969d1bec Initial revision
darius
parents:
diff changeset
99 def Scrape(handle):
ac32969d1bec Initial revision
darius
parents:
diff changeset
100 s = BeautifulSoup.BeautifulSoup(handle)
ac32969d1bec Initial revision
darius
parents:
diff changeset
101
ac32969d1bec Initial revision
darius
parents:
diff changeset
102 playertbl = s.find("table", "search_table")
ac32969d1bec Initial revision
darius
parents:
diff changeset
103 if (playertbl == None):
6
5058c2695109 Properly report if we can't parse the results.
darius
parents: 5
diff changeset
104 #print "Unable to find results"
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
105 return None
ac32969d1bec Initial revision
darius
parents:
diff changeset
106
ac32969d1bec Initial revision
darius
parents:
diff changeset
107 servertbl = playertbl.findNext("table", "search_table")
ac32969d1bec Initial revision
darius
parents:
diff changeset
108
ac32969d1bec Initial revision
darius
parents:
diff changeset
109 playerrows = playertbl.findAll("tr")
ac32969d1bec Initial revision
darius
parents:
diff changeset
110 serverrows = servertbl.findAll("tr")
ac32969d1bec Initial revision
darius
parents:
diff changeset
111
ac32969d1bec Initial revision
darius
parents:
diff changeset
112 if (len(playerrows) != len(serverrows)):
ac32969d1bec Initial revision
darius
parents:
diff changeset
113 print "Internal error 41223"
ac32969d1bec Initial revision
darius
parents:
diff changeset
114 return
ac32969d1bec Initial revision
darius
parents:
diff changeset
115
ac32969d1bec Initial revision
darius
parents:
diff changeset
116 servers = {}
ac32969d1bec Initial revision
darius
parents:
diff changeset
117 for i in range(len(playerrows[1:])):
ac32969d1bec Initial revision
darius
parents:
diff changeset
118 pcols = playerrows[i].findAll('td')
ac32969d1bec Initial revision
darius
parents:
diff changeset
119 scols = serverrows[i].findAll('td')
ac32969d1bec Initial revision
darius
parents:
diff changeset
120 if (len(pcols) != 4):
ac32969d1bec Initial revision
darius
parents:
diff changeset
121 continue
ac32969d1bec Initial revision
darius
parents:
diff changeset
122
ac32969d1bec Initial revision
darius
parents:
diff changeset
123 pcols = map(lambda c : Server.FixTags(str(c)), pcols)
ac32969d1bec Initial revision
darius
parents:
diff changeset
124 scols = map(lambda c : Server.FixTags(str(c)), scols)
ac32969d1bec Initial revision
darius
parents:
diff changeset
125
ac32969d1bec Initial revision
darius
parents:
diff changeset
126 stuple = Server.GetTuple(scols)
ac32969d1bec Initial revision
darius
parents:
diff changeset
127
ac32969d1bec Initial revision
darius
parents:
diff changeset
128 if (stuple not in servers):
ac32969d1bec Initial revision
darius
parents:
diff changeset
129 s = Server(pcols, scols)
ac32969d1bec Initial revision
darius
parents:
diff changeset
130 servers[stuple] = s
ac32969d1bec Initial revision
darius
parents:
diff changeset
131
ac32969d1bec Initial revision
darius
parents:
diff changeset
132 servers[stuple].addplayer(pcols[2])
ac32969d1bec Initial revision
darius
parents:
diff changeset
133
ac32969d1bec Initial revision
darius
parents:
diff changeset
134 return servers
ac32969d1bec Initial revision
darius
parents:
diff changeset
135 Scrape = staticmethod(Scrape)
ac32969d1bec Initial revision
darius
parents:
diff changeset
136
ac32969d1bec Initial revision
darius
parents:
diff changeset
137 def addplayer(self, pname):
ac32969d1bec Initial revision
darius
parents:
diff changeset
138 self.players.append(pname)
ac32969d1bec Initial revision
darius
parents:
diff changeset
139
ac32969d1bec Initial revision
darius
parents:
diff changeset
140
ac32969d1bec Initial revision
darius
parents:
diff changeset
141 if (1):
ac32969d1bec Initial revision
darius
parents:
diff changeset
142 maxhits = 10
ac32969d1bec Initial revision
darius
parents:
diff changeset
143 if (len(sys.argv) < 2):
ac32969d1bec Initial revision
darius
parents:
diff changeset
144 print "Bad usage"
ac32969d1bec Initial revision
darius
parents:
diff changeset
145 print sys.argv[0] + "search_string"
ac32969d1bec Initial revision
darius
parents:
diff changeset
146 sys.exit(1)
ac32969d1bec Initial revision
darius
parents:
diff changeset
147
ac32969d1bec Initial revision
darius
parents:
diff changeset
148 try:
ac32969d1bec Initial revision
darius
parents:
diff changeset
149 #f = open("gm.html")
8
fbaf34d7bab7 Only list Australian servers.
darius@inchoate.localdomain
parents: 7
diff changeset
150 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
151 except IOError, e:
ac32969d1bec Initial revision
darius
parents:
diff changeset
152 print "Unable to fetch page - " + str(e)
ac32969d1bec Initial revision
darius
parents:
diff changeset
153 sys.exit(0)
ac32969d1bec Initial revision
darius
parents:
diff changeset
154
ac32969d1bec Initial revision
darius
parents:
diff changeset
155 servers = Server.Scrape(f)
ac32969d1bec Initial revision
darius
parents:
diff changeset
156 del f
6
5058c2695109 Properly report if we can't parse the results.
darius
parents: 5
diff changeset
157 if (servers == None):
5058c2695109 Properly report if we can't parse the results.
darius
parents: 5
diff changeset
158 print "No results available, please check manually"
5058c2695109 Properly report if we can't parse the results.
darius
parents: 5
diff changeset
159 elif (len(servers) == 0):
1
ac32969d1bec Initial revision
darius
parents:
diff changeset
160 print "No players found"
ac32969d1bec Initial revision
darius
parents:
diff changeset
161 else:
ac32969d1bec Initial revision
darius
parents:
diff changeset
162 i = 0
ac32969d1bec Initial revision
darius
parents:
diff changeset
163 for s in servers:
ac32969d1bec Initial revision
darius
parents:
diff changeset
164 i = i + 1
ac32969d1bec Initial revision
darius
parents:
diff changeset
165 print servers[s]
ac32969d1bec Initial revision
darius
parents:
diff changeset
166 if (i >= maxhits):
ac32969d1bec Initial revision
darius
parents:
diff changeset
167 print "*** Stopping after " + str(maxhits) + " hits"
ac32969d1bec Initial revision
darius
parents:
diff changeset
168 break