chiark / gitweb /
ocean scraper can fetch charts
[ypp-sc-tools.db-test.git] / yarrg / yppedia-ocean-scraper
index 401de9bd6925fe60b904aa5f09b2791f99fc76a7..5f9c5f007b46fda4265ccc7492dc2b3d58e079f0 100755 (executable)
@@ -53,15 +53,20 @@ def debug(k,v):
 
 def fetch():
        global soup
-       url = ('http://yppedia.puzzlepirates.com/%s_Ocean' %
-               urllib.quote(ocean,''))
+       if opts.chart:
+               url_base = 'index.php?title=Template:Map:%s_Ocean&action=edit'
+       else:
+               url_base = '%s_Ocean'
+       url = ('http://yppedia.puzzlepirates.com/' +
+                       (url_base % urllib.quote(ocean,'')))
        debug('fetching',url)
        dataf = urllib2.urlopen(url)
        debug('fetched',dataf)
-       soup = BeautifulSoup(dataf)
+       soup = BeautifulSoup(dataf,
+               convertEntities=BeautifulSoup.HTML_ENTITIES)
 
 
-title_arch_re = regexp.compile('(\\S+) Archipelago \\((\\S+)\\)$')
+title_arch_re = regexp.compile('(\\S.*\\S) Archipelago \\((\\S+)\\)$')
 title_any_re = regexp.compile('(\\S.*\\S) \((\\S+)\\)$')
 href_img_re = regexp.compile('\\.png$')
 
@@ -78,7 +83,14 @@ def title_arch_ok(t):
        if o is None: return False
        return o == ocean
 
-def parse():
+def parse_chart():
+       ta = soup.find('textarea')
+       debug('ta',ta)
+       rc = ta.renderContents()
+       debug('rc',rc)
+       return rc
+
+def parse_ocean():
        firstarch = soup.find('a', attrs = {'title': title_arch_ok})
        debug('fa',firstarch)
 
@@ -107,8 +119,8 @@ def parse():
                debug('links',links)
                if not links: continue
                (a,o) = title_arch_info(links[0]['title'])
+               debug('arch-ocean', (a,o))
                assert(o == ocean)
-               debug('arch', a)
                assert(a not in arches)
                isles = []
                for link in links[1:]:
@@ -136,8 +148,11 @@ def main():
        global opts
 
        pa = OptionParser(
-               '''usage: .../yppedia-ocean-scraper [--debug] OCEAN''')
+'''usage: .../yppedia-ocean-scraper [--debug] [--chart] OCEAN''')
        ao = pa.add_option
+
+       ao('--chart', action='store_true', dest='chart',
+               help='print chart source rather than arch/island info')
        ao('--debug', action='count', dest='debug', default=0,
                help='enable debugging output')
 
@@ -148,7 +163,10 @@ def main():
        ocean = args[0]
 
        fetch()
-       parse()
-       output()
+       if opts.chart:
+               print parse_chart()
+       else:
+               parse_ocean()
+               output()
 
 main()