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
52 print >>sys.stderr, k,`v`
57 url_base = 'index.php?title=Template:Map:%s_Ocean&action=edit'
60 url = ('http://yppedia.puzzlepirates.com/' +
61 (url_base % urllib.quote(ocean,'')))
63 dataf = urllib2.urlopen(url)
64 debug('fetched',dataf)
65 soup = BeautifulSoup(dataf,
66 convertEntities=BeautifulSoup.HTML_ENTITIES)
69 title_arch_re = regexp.compile('(\\S.*\\S) Archipelago \\((\\S+)\\)$')
70 title_any_re = regexp.compile('(\\S.*\\S) \((\\S+)\\)$')
71 href_img_re = regexp.compile('\\.png$')
73 def title_arch_info(t):
74 # returns (arch,ocean)
76 if t is None: return (None,None)
77 m = title_arch_re.match(t)
78 if not m: return (None,None)
82 (a,o) = title_arch_info(t)
83 if o is None: return False
87 ta = soup.find('textarea')
89 rc = ta.renderContents()
94 firstarch = soup.find('a', attrs = {'title': title_arch_ok})
97 def findall_title_arch_ok(t):
98 return t.findAll('a', attrs = {'title': title_arch_ok})
100 def is_archestable(u):
101 if u.name != 'table': return False
102 return len(findall_title_arch_ok(u)) > 1
104 archestable = firstarch.findParent('table', attrs={'border':'1'})
105 debug('at',archestable)
108 for row in archestable.findAll('tr',recursive=False):
109 archsoups += row.findAll('td',recursive=False)
110 debug('ac',archsoups)
113 return len(v.findAll(text = regexp.compile('.*Large'))) > 0
115 return u.findParent(is_island)
117 for arch in archsoups:
118 links = arch.findAll('a', href=True)
120 if not links: continue
121 (a,o) = title_arch_info(links[0]['title'])
122 debug('arch-ocean', (a,o))
124 assert(a not in arches)
126 for link in links[1:]:
128 if href_img_re.search(link['href']): continue
129 m = title_any_re.match(link['title'])
130 assert(m.group(2) == ocean)
132 debug('island', island)
143 for island in arches[a]:
151 '''usage: .../yppedia-ocean-scraper [--debug] [--chart] OCEAN''')
154 ao('--chart', action='store_true', dest='chart',
155 help='print chart source rather than arch/island info')
156 ao('--debug', action='count', dest='debug', default=0,
157 help='enable debugging output')
159 (opts,args) = pa.parse_args()
161 print >>sys.stderr, copyright_info
162 pa.error('need an ocean argument')