chiark / gitweb /
Initial version of scraper for oceans' arches and islands
[ypp-sc-tools.web-live.git] / yarrg / yppedia-ocean-scraper
1 #!/usr/bin/python
2
3 import signal
4 signal.signal(signal.SIGINT, signal.SIG_DFL)
5
6 import os
7 import urllib
8 import urllib2
9 import re as regexp
10 #from optparse import OptionParser
11
12 from BeautifulSoup import BeautifulSoup
13
14 ocean = 'Opal'
15
16 url = 'http://yppedia.puzzlepirates.com/%s_Ocean' % urllib.quote(ocean,'')
17 dataf = urllib2.urlopen(url)
18
19 soup = BeautifulSoup(dataf)
20
21 title_arch_re = regexp.compile('\\S+ Archipelago \\((\\S+)\\)$')
22 title_any_re = regexp.compile('(\\S.*\\S) \((\\S+)\\)$')
23 href_img_re = regexp.compile('\\.png$')
24
25 def title_arch_arch(t):
26         print 'checking ',t
27         if t is None: return None
28         m = title_arch_re.match(t)
29         if not m: return None
30         return m.group(1)
31
32 def title_arch_ok(t):
33         a = title_arch_arch(t)
34         if a is None: return False
35         return a == ocean
36
37 firstarch = soup.find('a', attrs = {'title': title_arch_ok})
38 print 'fa',`firstarch`
39
40 archestable = firstarch.findParent(
41         lambda u: u.name == 'table' and
42                 len(u.findAll('a', attrs = {'title': title_arch_ok})) > 1
43         )
44
45 print 'at',`archestable`
46
47 arches = archestable.findAll('a', attrs = {'title': title_arch_ok})
48 print 'ac', `arches`
49
50 arches = map((lambda u: u.findParent(
51         lambda v: len(v.findAll(text= regexp.compile('.*Large'))) > 0
52         )), arches)
53 print 'ac2', `arches`
54
55 for arch in arches:
56         links = arch.findAll('a', href=True)
57         print 'links', `links`
58         a = title_arch_arch(links[0]['title'])
59         assert(a)
60         print a
61         for link in links[1:]:
62                 print 'link', `link`
63                 if href_img_re.search(link['href']): continue
64                 m = title_any_re.match(link['title'])
65                 assert(m.group(2) == ocean)
66                 print m.group(1)