12 from BeautifulSoup import BeautifulSoup
19 duties = ('Swordfighting/Bilging/Sailing/Rigging/Navigating'+
20 '/Battle Navigation/Gunning/Carpentry/Rumble/Treasure Haul'+
21 '/Drinking/Spades/Hearts/Treasure Drop/Poker/Distilling'+
22 '/Alchemistry/Shipwrightery/Blacksmithing/Foraging').split('/')
24 standings = ('Able/Distinguished/Respected/Master/Renowned'+
25 '/Grand-Master/Legendary/Ultimate').split('/')
28 cache_corename = urllib.quote_plus(url)
29 cache_basename = "#%s#" % cache_corename
30 try: f = file(cache_basename, 'r')
31 except (OSError,IOError), oe:
32 if oe.errno != errno.ENOENT: raise
35 s = os.fstat(f.fileno())
36 if now > s.st_mtime + max_age:
43 stream = urllib2.urlopen(url)
45 cache_ourname = "#%s~%d#" % (cache_corename, os.getpid())
46 f = file(cache_ourname, 'w')
49 os.rename(cache_ourname, cache_basename)
52 def yoweb_fetch(kind, tail):
53 url = 'http://%s.puzzlepirates.com/yoweb/%s%s' % (ocean, kind, tail)
57 # Public data members:
58 # pi.skills = { 'Treasure Haul': 'Able' ... }
59 # pi.msgs = [ 'message describing problem with scrape' ]
63 def _logsoup(self, soup, m):
64 self._log(m + '; in ' + `soup`)
66 def __init__(self, pirate):
67 html = yoweb_fetch('pirate.wm?target=', pirate)
68 soup = BeautifulSoup(html,
69 # convertEntities=BeautifulSoup.HTML_ENTITIES
71 imgs = soup.findAll('img',
72 src=regexp.compile('/yoweb/images/stat.*'))
74 u'\s*\S*/([-A-Za-z]+)\s*$|\s*\S*/\S*\s*\(ocean\-wide\ \;([-A-Za-z]+)\)\s*$'
83 try: duty = img['alt']
84 except KeyError: continue
86 if not duty in duties:
87 self._logsoup(img, 'unknown duty: "%s"' % duty)
89 key = img.findParent('td')
91 self._logsoup(img, 'duty at root! "%s"' % duty)
93 valelem = key.findNextSibling('td')
95 self._logsoup(key, 'duty missing sibling "%s"'
98 valstr = ''.join(valelem.findAll(text=True))
99 match = re.match(valstr)
101 self._logsoup(key, 'duty "%s" unparseable'+
102 ' standing "%s"' % (duty, valstr))
104 standing = match.group(match.lastindex)
105 skills[duty].append(standing)
112 self.log('duty "%s" multiple standings %s' %
116 self.log('duty "%s" no standing found' % duty)
119 for i in range(0, len(standings)):
120 if standing == standings[i]:
121 self.skills[duty] = i
122 if not duty in self.skills:
123 self.log('duty "%s" unknown standing "%s"' %
127 if not duty in self.skills:
128 all_skills_ok = False
136 os.chdir(os.getenv('HOME'))
137 cache_dir = '.yoweb-scrape-cache'
140 except (OSError,IOError), oe:
141 if oe.errno != errno.ENOENT: raise
145 for path in os.listdir('.'):
146 if not path.startswith('#'): continue
148 if '~' in path: max_time = 10
151 if now > s.st_mtime + max_time:
153 except (OSError,IOError), oe:
154 if oe.errno != errno.ENOENT: raise
159 test = PirateInfo('Anaplian')