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=68efa38f6781980c63308a55b55b4334195873d5;hp=a85403504aae52764a1a6d96f7202f6444c005ed;hb=3579e35d2b8ed951db24cc4873c155fded7f4375;hpb=714a62245610bd19b4056878b808f3d6e59e88c6;ds=sidebyside diff --git a/yarrg/yppedia-ocean-scraper b/yarrg/yppedia-ocean-scraper index a854035..68efa38 100755 --- a/yarrg/yppedia-ocean-scraper +++ b/yarrg/yppedia-ocean-scraper @@ -37,11 +37,42 @@ signal.signal(signal.SIGINT, signal.SIG_DFL) import sys import os import urllib -import urllib2 import re as regexp +import subprocess 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() + + +# User agent: +class YarrgURLopener(urllib.FancyURLopener): + base_version= urllib.URLopener().version + proc= subprocess.Popen( + ["./database-info-fetch", "useragentstringmap", + base_version, "manual islands/topology fetch"], + shell=False, + stderr=None, + stdout=subprocess.PIPE, + ) + version = proc.communicate()[0].rstrip('\n'); + assert(proc.returncode is not None and proc.returncode == 0) +urllib._urlopener = YarrgURLopener() + ocean = None soup = None opts = None @@ -60,7 +91,7 @@ def fetch(): url = ('http://yppedia.puzzlepirates.com/' + (url_base % urllib.quote(ocean,''))) debug('fetching',url) - dataf = urllib2.urlopen(url) + dataf = urllib.urlopen(url) debug('fetched',dataf) soup = BeautifulSoup(dataf)