Mercurial > ~darius > hgwebdir.cgi > scrape-gm
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 |
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('\ ') | |
41 | |
42 def __init__(self, description = "", ip = "", port = 0, mapname = "", | |
43 updateage = 0, numplayers = 0, maxplayers = 0, players = []): | |
44 self.description = description | |
45 self.ip = ip | |
46 self.port = port | |
47 self.mapname = mapname | |
48 self.updateage = int(updateage) | |
49 self.players = [] | |
50 self.numplayers = numplayers | |
51 self.maxplayers = maxplayers | |
52 | |
53 def __init__(self, pcols, scols): | |
54 # pcols[2] = Player name | |
55 # pcols[3] = Server description | |
56 # scols[0] = Players in server / max players | |
57 # scols[2] = Server IP | |
58 # scols[3] = Server port | |
59 # scols[4] = Map name | |
60 # scols[10] = Update age | |
61 self.tuplere = re.compile("\[?([0-9]+)/([0-9]+)\]?") | |
62 self.description = pcols[3] | |
63 self.ip = scols[2] | |
64 self.port = int(scols[3]) | |
65 self.mapname = scols[4] | |
66 self.updateage = scols[10] | |
67 m = self.tuplere.match(scols[0]) | |
68 if (m == None): | |
69 raise SyntaxError | |
70 | |
71 self.numplayers = int(m.group(1)) | |
72 self.maxplayers = int(m.group(2)) | |
73 self.players = [] | |
74 | |
75 def __str__(self): | |
76 plist = "" | |
77 for p in self.players: | |
78 plist = plist + " " + str(p) | |
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 | 84 |
85 def GetTuple(scols): | |
86 return str(scols[2]) + ":" + str(scols[3]) | |
87 GetTuple = staticmethod(GetTuple) | |
88 | |
89 def FixTags(s): | |
90 s = re.sub(Server.vwhttags, '\n', s) | |
91 s = re.sub(Server.hwhttags, '', s) | |
92 s = str(BeautifulSoup.BeautifulStoneSoup( \ | |
93 s, convertEntities = BeautifulSoup.BeautifulStoneSoup.XML_ENTITIES)) | |
94 s = re.sub(Server.alltags, '', s) | |
95 return(s) | |
96 FixTags = staticmethod(FixTags) | |
97 | |
98 def Scrape(handle): | |
99 s = BeautifulSoup.BeautifulSoup(handle) | |
100 | |
10 | 101 playertbl = s.find("table", "results") |
1 | 102 if (playertbl == None): |
6 | 103 #print "Unable to find results" |
1 | 104 return None |
105 | |
10 | 106 servertbl = playertbl.findNext("table", "results") |
1 | 107 |
108 playerrows = playertbl.findAll("tr") | |
109 serverrows = servertbl.findAll("tr") | |
110 | |
111 if (len(playerrows) != len(serverrows)): | |
112 print "Internal error 41223" | |
113 return | |
114 | |
115 servers = {} | |
116 for i in range(len(playerrows[1:])): | |
117 pcols = playerrows[i].findAll('td') | |
118 scols = serverrows[i].findAll('td') | |
119 if (len(pcols) != 4): | |
120 continue | |
121 | |
122 pcols = map(lambda c : Server.FixTags(str(c)), pcols) | |
123 scols = map(lambda c : Server.FixTags(str(c)), scols) | |
124 | |
125 stuple = Server.GetTuple(scols) | |
126 | |
127 if (stuple not in servers): | |
128 s = Server(pcols, scols) | |
129 servers[stuple] = s | |
130 | |
131 servers[stuple].addplayer(pcols[2]) | |
132 | |
133 return servers | |
134 Scrape = staticmethod(Scrape) | |
135 | |
136 def addplayer(self, pname): | |
137 self.players.append(pname) | |
138 | |
139 | |
140 if (1): | |
141 maxhits = 10 | |
142 if (len(sys.argv) < 2): | |
143 print "Bad usage" | |
144 print sys.argv[0] + "search_string" | |
145 sys.exit(1) | |
146 | |
147 try: | |
148 #f = open("gm.html") | |
8 | 149 f = urllib.urlopen("http://www.game-monitor.com/search.php?location=AU&search=" + urllib.quote(sys.argv[1]) + "&type=player&location=AU") |
1 | 150 except IOError, e: |
151 print "Unable to fetch page - " + str(e) | |
152 sys.exit(0) | |
153 | |
154 servers = Server.Scrape(f) | |
155 del f | |
6 | 156 if (servers == None): |
157 print "No results available, please check manually" | |
158 elif (len(servers) == 0): | |
1 | 159 print "No players found" |
160 else: | |
161 i = 0 | |
162 for s in servers: | |
163 i = i + 1 | |
164 print servers[s] | |
165 if (i >= maxhits): | |
166 print "*** Stopping after " + str(maxhits) + " hits" | |
167 break |