chiark / gitweb /
yarrg database: when scraping yppedia charts, allow links to islands whose pages...
[ypp-sc-tools.db-test.git] / yarrg / yppedia-ocean-scraper
index 476c1cd9b381bb0d5d5ec1979cc4928bff9fa9f4..5e5e0901f563b097b6554b891e04cf184f89b45a 100755 (executable)
@@ -2,22 +2,22 @@
 
 # helper program for getting information from yppedia
 
-# This is part of ypp-sc-tools, a set of third-party tools for assisting
-# players of Yohoho Puzzle Pirates.
+# This is part of the YARRG website.  YARRG is a tool and website
+# for assisting players of Yohoho Puzzle Pirates.
 #
 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
 #
 # This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
+# GNU Affero General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License
+# You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
@@ -37,8 +37,8 @@ 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
 
@@ -59,6 +59,20 @@ def fix_stdout():
 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
@@ -74,16 +88,18 @@ def fetch():
                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)
+       url_base = url_base % urllib.quote(ocean,'')
+       if opts.localhtml is None:
+               url = ('http://yppedia.puzzlepirates.com/' + url_base)
+               debug('fetching',url)
+               dataf = urllib.urlopen(url)
+               debug('fetched',dataf)
+       else:
+               dataf = file(opts.localhtml + '/' + url_base, 'r')
        soup = BeautifulSoup(dataf)
 
-
 title_arch_re = regexp.compile('(\\S.*\\S) Archipelago \\((\\S+)\\)$')
-title_any_re = regexp.compile('(\\S.*\\S) \((\\S+)\\)$')
+title_any_re = regexp.compile('(\\S.*\\S) \((\\S+)\\)(?: \(page does not exist\))?$')
 href_img_re = regexp.compile('\\.png$')
 
 def title_arch_info(t):
@@ -175,6 +191,9 @@ def main():
                help='print chart source rather than arch/island info')
        ao('--debug', action='count', dest='debug', default=0,
                help='enable debugging output')
+       ao('--local-html-dir', action='store', dest='localhtml',
+               help='get yppedia pages from local directory LOCALHTML'+
+                       ' instead of via HTTP')
 
        (opts,args) = pa.parse_args()
        if len(args) != 1: