3 # helper program for getting information from yppedia
5 # This is part of ypp-sc-tools, a set of third-party tools for assisting
6 # players of Yohoho Puzzle Pirates.
8 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
24 # are used without permission. This program is not endorsed or
25 # sponsored by Three Rings.
28 yppedia-ocean-scraper is part of ypp-sc-tools Copyright (C) 2009 Ian Jackson
29 This program comes with ABSOLUTELY NO WARRANTY; this is free software,
30 and you are welcome to redistribute it under certain conditions. For
31 details, read the top of the yppedia-ocean-scraper file.
35 signal.signal(signal.SIGINT, signal.SIG_DFL)
42 from optparse import OptionParser
43 from BeautifulSoup import BeautifulSoup
50 sys.stdout = codecs.EncodedFile(sys.stdout, locale.getpreferredencoding())
51 def null_decode(input, errors='strict'):
52 return input, len(input)
53 sys.stdout.decode = null_decode
55 # http://ewx.livejournal.com/457086.html?thread=3016574
56 # http://ewx.livejournal.com/457086.html?thread=3016574
58 # See also Debian #415968.
63 class YarrgURLopener(urllib.FancyURLopener):
64 base_version= urllib.URLopener().version
65 proc= subprocess.Popen(
66 ["./database-info-fetch", "useragentstringmap",
67 base_version, "manual islands/topology fetch"],
70 stdout=subprocess.PIPE,
72 version = proc.communicate()[0].rstrip('\n');
73 assert(proc.returncode is not None and proc.returncode == 0)
74 urllib._urlopener = YarrgURLopener()
83 print >>sys.stderr, k,`v`
88 url_base = 'index.php?title=Template:Map:%s_Ocean&action=edit'
91 url = ('http://yppedia.puzzlepirates.com/' +
92 (url_base % urllib.quote(ocean,'')))
94 dataf = urllib.urlopen(url)
95 debug('fetched',dataf)
96 soup = BeautifulSoup(dataf)
99 title_arch_re = regexp.compile('(\\S.*\\S) Archipelago \\((\\S+)\\)$')
100 title_any_re = regexp.compile('(\\S.*\\S) \((\\S+)\\)$')
101 href_img_re = regexp.compile('\\.png$')
103 def title_arch_info(t):
104 # returns (arch,ocean)
106 if t is None: return (None,None)
107 m = title_arch_re.match(t)
108 if not m: return (None,None)
111 def title_arch_ok(t):
112 (a,o) = title_arch_info(t)
113 if o is None: return False
117 ta = soup.find('textarea')
121 s = regexp.sub(r'\<\;', '<', s)
122 s = regexp.sub(r'\>\;', '>', s)
123 s = regexp.sub(r'\"\;', '"', s)
124 s = regexp.sub(r'\&\;', '&', s)
129 content = soup.find('div', attrs = {'id': 'content'})
131 def findall_title_arch_ok(t):
132 return t.findAll('a', attrs = {'title': title_arch_ok})
134 def is_archestable(u):
135 if u.name != 'table': return False
136 return len(findall_title_arch_ok(u)) > 1
138 archestable = content.findChild('table', attrs={'border':'1'})
139 debug('at',archestable)
142 for row in archestable.findAll('tr',recursive=False):
143 archsoups += row.findAll('td',recursive=False)
144 debug('ac',archsoups)
147 return len(v.findAll(text = regexp.compile('.*Large'))) > 0
149 return u.findParent(is_island)
151 for arch in archsoups:
152 links = arch.findAll('a', href=True)
154 if not links: continue
155 (a,o) = title_arch_info(links[0]['title'])
156 debug('arch-ocean', (a,o))
158 assert(a not in arches)
160 for link in links[1:]:
162 if href_img_re.search(link['href']): continue
163 m = title_any_re.match(link['title'])
164 assert(m.group(2) == ocean)
166 debug('island', island)
177 for island in arches[a]:
185 '''usage: .../yppedia-ocean-scraper [--debug] [--chart] OCEAN''')
188 ao('--chart', action='store_true', dest='chart',
189 help='print chart source rather than arch/island info')
190 ao('--debug', action='count', dest='debug', default=0,
191 help='enable debugging output')
193 (opts,args) = pa.parse_args()
195 print >>sys.stderr, copyright_info
196 pa.error('need an ocean argument')