X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.db-live.git;a=blobdiff_plain;f=yarrg%2Fyppedia-ocean-scraper;h=476c1cd9b381bb0d5d5ec1979cc4928bff9fa9f4;hp=9105f1963998d96943c08237ad9a36f84deb5d85;hb=87b0d92e050701753d558e49aaae1d433b546170;hpb=7078fe9eae28c76913e5c14946f9dbf94cfb3458 diff --git a/yarrg/yppedia-ocean-scraper b/yarrg/yppedia-ocean-scraper index 9105f19..476c1cd 100755 --- a/yarrg/yppedia-ocean-scraper +++ b/yarrg/yppedia-ocean-scraper @@ -42,6 +42,23 @@ import re as regexp from optparse import OptionParser from BeautifulSoup import BeautifulSoup + +# For fuck's sake! +import codecs +import locale +def fix_stdout(): + sys.stdout = codecs.EncodedFile(sys.stdout, locale.getpreferredencoding()) + def null_decode(input, errors='strict'): + return input, len(input) + sys.stdout.decode = null_decode +# From +# http://ewx.livejournal.com/457086.html?thread=3016574 +# http://ewx.livejournal.com/457086.html?thread=3016574 +# lightly modified. +# See also Debian #415968. +fix_stdout() + + ocean = None soup = None opts = None @@ -53,8 +70,12 @@ 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) @@ -78,9 +99,20 @@ def title_arch_ok(t): if o is None: return False return o == ocean -def parse(): - firstarch = soup.find('a', attrs = {'title': title_arch_ok}) - debug('fa',firstarch) +def parse_chart(): + ta = soup.find('textarea') + debug('ta',ta) + s = ta.string + debug('s',s) + s = regexp.sub(r'\<\;', '<', s) + s = regexp.sub(r'\>\;', '>', s) + s = regexp.sub(r'\"\;', '"', s) + s = regexp.sub(r'\&\;', '&', s) + debug('s',s) + return s + +def parse_ocean(): + content = soup.find('div', attrs = {'id': 'content'}) def findall_title_arch_ok(t): return t.findAll('a', attrs = {'title': title_arch_ok}) @@ -89,7 +121,7 @@ def parse(): if u.name != 'table': return False return len(findall_title_arch_ok(u)) > 1 - archestable = firstarch.findParent('table', attrs={'border':'1'}) + archestable = content.findChild('table', attrs={'border':'1'}) debug('at',archestable) archsoups = [] @@ -136,8 +168,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 +183,10 @@ def main(): ocean = args[0] fetch() - parse() - output() + if opts.chart: + print parse_chart() + else: + parse_ocean() + output() main()