chiark / gitweb /
routesearch: make absolute/perleague into 2-element arrays rather than macros
[ypp-sc-tools.db-live.git] / yarrg / yppedia-ocean-scraper
index a85403504aae52764a1a6d96f7202f6444c005ed..68efa38f6781980c63308a55b55b4334195873d5 100755 (executable)
@@ -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)