10 from BeautifulSoup import BeautifulSoup
17 duties = ('Swordfighting/Bilging/Sailing/Rigging/Navigation'+
18 '/Battle Navigation/Carpentry/Rumble/Treasure Haul').split('/')
20 standings = ('Able/Distinguished/Respected/Master/Renowned'+
21 '/Grand-Master/Legendary/Ultimate').split('/')
24 cache_corename = urllib.quote_plus(url)
25 cache_basename = "#%s#" % cache_corename
26 try: f = file(cache_basename, 'r')
27 except (OSError,IOError), oe:
28 if oe.errno != errno.ENOENT: raise
31 s = os.fstat(f.fileno())
32 if now > s.st_mtime + max_age:
38 stream = urllib2.urlopen(url)
40 cache_ourname = "#%s~%d#" % (cache_corename, os.getpid())
41 f = file(cache_ourname, 'w')
44 os.rename(cache_ourname, cache_basename)
47 def yoweb_fetch(kind, tail):
48 url = 'http://%s.puzzlepirates.com/yoweb/%s%s' % (ocean, kind, tail)
52 def __init__(self, pirate):
53 html = yoweb_fetch('pirate.wm?target=', pirate)
54 soup = BeautifulSoup(html,
55 # convertEntities=BeautifulSoup.HTML_ENTITIES
57 imgs = soup.findAll('img')
59 u'\s*\S*/([-A-Za-z]+)\s*$|\s*\S*/\S*\s*\(ocean\-wide\ \;([-A-Za-z]+)\)\s*$'
65 try: duty = img['alt']
66 except KeyError: continue
68 if not duty in duties: continue
69 key = img.findParent('td')
70 if key is None: continue
71 valelem = key.findNextSibling('td')
72 if valelem is None: continue
73 valstr = ''.join(valelem.findAll(text=True))
74 print `duty`, `valstr`
75 match = re.match(valstr)
76 if match is None: continue
77 standing = match.group(1)
78 skills[duty] = standing
80 # print `duty`, `standing`
81 # if standing not in standings: continue
82 # for i in range(0, len(standings)):
83 # print `duty`, `standing`, i
84 # if standing == standings[i]:
85 # print `skills[duty]`
86 # skills[duty].append(i)
89 # for skill in duties:
96 os.chdir(os.getenv('HOME'))
97 cache_dir = '.yoweb-scrape-cache'
100 except (OSError,IOError), oe:
101 if oe.errno != errno.ENOENT: raise
105 for path in os.listdir('.'):
106 if not path.startswith('#'): continue
108 if '~' in path: max_time = 10
111 if now > s.st_mtime + max_time:
113 except (OSError,IOError), oe:
114 if oe.errno != errno.ENOENT: raise
119 test = PirateInfo('Anaplian')