chiark / gitweb /
5b2fe9841f00a98c9320448a4b7f316813333e78
[ypp-sc-tools.db-test.git] / yoweb-scrape
1 #!/usr/bin/python
2
3 import signal
4 signal.signal(signal.SIGINT, signal.SIG_DFL)
5
6 import os
7 import time
8 import urllib
9 import urllib2
10 import errno
11 import sys
12 import re as regexp
13 import random
14 from optparse import OptionParser
15
16 from BeautifulSoup import BeautifulSoup
17
18 opts = None
19
20
21 puzzles = ('Swordfighting/Bilging/Sailing/Rigging/Navigating'+
22         '/Battle Navigation/Gunning/Carpentry/Rumble/Treasure Haul'+
23         '/Drinking/Spades/Hearts/Treasure Drop/Poker/Distilling'+
24         '/Alchemistry/Shipwrightery/Blacksmithing/Foraging').split('/')
25
26 standingvals = ('Able/Distinguished/Respected/Master'+
27                 '/Renowned/Grand-Master/Legendary/Ultimate').split('/')
28
29 pirate_ref_re = regexp.compile('^/yoweb/pirate\\.wm')
30
31 max_pirate_namelen = 12
32
33
34 def debug(m):
35         if opts.debug:
36                 print m
37
38 class Fetcher:
39         def __init__(self, ocean, cachedir):
40                 debug('Fetcher init %s' % cachedir)
41                 self.ocean = ocean
42                 self.cachedir = cachedir
43                 try: os.mkdir(cachedir)
44                 except (OSError,IOError), oe:
45                         if oe.errno != errno.EEXIST: raise
46                 self._cache_scan(time.time())
47
48         def _default_ocean(self):
49                 if self.ocean is None:
50                         self.ocean = 'ice'
51
52         def _cache_scan(self, now):
53                 # returns list of ages, unsorted
54                 ages = []
55                 debug('Fetcher   scan_cache')
56                 for leaf in os.listdir(self.cachedir):
57                         if not leaf.startswith('#'): continue
58                         path = self.cachedir + '/' + leaf
59                         try: s = os.stat(path)
60                         except (OSError,IOError), oe:
61                                 if oe.errno != errno.ENOENT: raise
62                                 continue
63                         age = now - s.st_mtime
64                         if age > opts.expire_age:
65                                 debug('Fetcher    expire %d %s' % (age, path))
66                                 try: os.remove(path)
67                                 except (OSError,IOError), oe:
68                                         if oe.errno != errno.ENOENT: raise
69                                 continue
70                         ages.append(age)
71                 return ages
72
73         def _rate_limit_cache_clean(self, now):
74                 ages = self._cache_scan(now)
75                 ages.sort()
76                 debug('Fetcher   ages ' + `ages`)
77                 min_age = 1
78                 need_wait = 0
79                 for age in ages:
80                         if age < min_age and age < 300:
81                                 debug('Fetcher   morewait min=%d age=%d' %
82                                         (min_age, age))
83                                 need_wait = max(need_wait, min_age - age)
84                         min_age += 3
85                         min_age *= 1.25
86                 if need_wait > 0:
87                         debug('Fetcher   wait %d' % need_wait)
88                         time.sleep(need_wait)
89
90         def fetch(self, url, max_age):
91                 debug('Fetcher fetch %s' % url)
92                 cache_corename = urllib.quote_plus(url)
93                 cache_item = "%s/#%s#" % (self.cachedir, cache_corename)
94                 try: f = file(cache_item, 'r')
95                 except (OSError,IOError), oe:
96                         if oe.errno != errno.ENOENT: raise
97                         f = None
98                 now = time.time()
99                 max_age = max(opts.min_max_age, min(max_age, opts.expire_age))
100                 if f is not None:
101                         s = os.fstat(f.fileno())
102                         age = now - s.st_mtime
103                         if age > max_age:
104                                 debug('Fetcher  stale %d < %d'% (max_age, age))
105                                 f = None
106                 if f is not None:
107                         data = f.read()
108                         f.close()
109                         debug('Fetcher  cached %d > %d' % (max_age, age))
110                         return data
111
112                 debug('Fetcher  fetch')
113                 self._rate_limit_cache_clean(now)
114
115                 stream = urllib2.urlopen(url)
116                 data = stream.read()
117                 cache_tmp = "%s/#%s~%d#" % (
118                         self.cachedir, cache_corename, os.getpid())
119                 f = file(cache_tmp, 'w')
120                 f.write(data)
121                 f.close()
122                 os.rename(cache_tmp, cache_item)
123                 debug('Fetcher  stored')
124                 return data
125
126         def yoweb(self, kind, tail, max_age):
127                 self._default_ocean()
128                 url = 'http://%s.puzzlepirates.com/yoweb/%s%s' % (
129                         self.ocean, kind, tail)
130                 return self.fetch(url, max_age)
131
132 class SoupLog:
133         def __init__(self):
134                 self.msgs = [ ]
135         def msg(self, m):
136                 self.msgs.append(m)
137         def soupm(self, obj, m):
138                 self.msg(m + '; in ' + `obj`)
139         def needs_msgs(self, child_souplog):
140                 self.msgs += child_souplog.msgs
141                 child_souplog.msgs = [ ]
142
143 def soup_text(obj):
144         str = ''.join(obj.findAll(text=True))
145         return str.strip()
146
147 class SomethingSoupInfo(SoupLog):
148         def __init__(self, kind, tail, max_age):
149                 SoupLog.__init__(self)
150                 html = fetcher.yoweb(kind, tail, max_age)
151                 self._soup = BeautifulSoup(html,
152                         convertEntities=BeautifulSoup.HTML_ENTITIES
153                         )
154
155 class PirateInfo(SomethingSoupInfo):
156         # Public data members:
157         #  pi.standings = { 'Treasure Haul': 'Able' ... }
158         #  pi.name = name
159         #  pi.crew = (id, name)
160         #  pi.flag = (id, name)
161         #  pi.msgs = [ 'message describing problem with scrape' ]
162                 
163         def __init__(self, pirate, max_age=300):
164                 SomethingSoupInfo.__init__(self,
165                         'pirate.wm?target=', pirate, max_age)
166                 self.name = pirate
167                 self._find_standings()
168                 self.crew = self._find_crewflag('crew',
169                         '^/yoweb/crew/info\\.wm')
170                 self.flag = self._find_crewflag('flag',
171                         '^/yoweb/flag/info\\.wm')
172
173         def _find_standings(self):
174                 imgs = self._soup.findAll('img',
175                         src=regexp.compile('/yoweb/images/stat.*'))
176                 re = regexp.compile(
177 u'\\s*\\S*/([-A-Za-z]+)\\s*$|\\s*\\S*/\\S*\\s*\\(ocean\\-wide(?:\\s|\\xa0)+([-A-Za-z]+)\\)\\s*$'
178                         )
179                 standings = { }
180
181                 for skill in puzzles:
182                         standings[skill] = [ ]
183
184                 skl = SoupLog()
185
186                 for img in imgs:
187                         try: puzzle = img['alt']
188                         except KeyError: continue
189
190                         if not puzzle in puzzles:
191                                 skl.soupm(img, 'unknown puzzle: "%s"' % puzzle)
192                                 continue
193                         key = img.findParent('td')
194                         if key is None:
195                                 skl.soupm(img, 'puzzle at root! "%s"' % puzzle)
196                                 continue
197                         valelem = key.findNextSibling('td')
198                         if valelem is None:
199                                 skl.soupm(key, 'puzzle missing sibling "%s"'
200                                         % puzzle)
201                                 continue
202                         valstr = soup_text(valelem)
203                         match = re.match(valstr)
204                         if match is None:
205                                 skl.soupm(key, ('puzzle "%s" unparseable'+
206                                         ' standing "%s"') % (puzzle, valstr))
207                                 continue
208                         standing = match.group(match.lastindex)
209                         standings[puzzle].append(standing)
210
211                 self.standings = { }
212
213                 for puzzle in puzzles:
214                         sl = standings[puzzle]
215                         if len(sl) > 1:
216                                 skl.msg('puzzle "%s" multiple standings %s' %
217                                                 (puzzle, `sl`))
218                                 continue
219                         if not sl:
220                                 skl.msg('puzzle "%s" no standing found' % puzzle)
221                                 continue
222                         standing = sl[0]
223                         for i in range(0, len(standingvals)-1):
224                                 if standing == standingvals[i]:
225                                         self.standings[puzzle] = i
226                         if not puzzle in self.standings:
227                                 skl.msg('puzzle "%s" unknown standing "%s"' %
228                                         (puzzle, standing))
229
230                 all_standings_ok = True
231                 for puzzle in puzzles:
232                         if not puzzle in self.standings:
233                                 self.needs_msgs(skl)
234
235         def _find_crewflag(self, cf, yoweb_re):
236                 things = self._soup.findAll('a', href=regexp.compile(yoweb_re))
237                 if len(things) != 1:
238                         self.msg('zero or several %s id references found' % cf)
239                         return None
240                 thing = things[0]
241                 id_re = '\\b%sid\\=(\\w+)$' % cf
242                 id_haystack = thing['href']
243                 match = regexp.compile(id_re).search(id_haystack)
244                 if match is None:
245                         self.soupm(thing, ('incomprehensible %s id ref'+
246                                 ' (%s in %s)') % (cf, id_re, id_haystack))
247                         return None
248                 name = soup_text(thing)
249                 return (match.group(1), name)
250
251         def __str__(self):
252                 return `(self.crew, self.flag, self.standings, self.msgs)`
253
254 class CrewInfo(SomethingSoupInfo):
255         # Public data members:
256         #  ci.crew = [ ('Captain',        ['Pirate', ...]),
257         #              ('Senior Officer', [...]),
258         #               ... ]
259         #  pi.msgs = [ 'message describing problem with scrape' ]
260
261         def __init__(self, crewid, max_age=300):
262                 SomethingSoupInfo.__init__(self,
263                         'crew/info.wm?crewid=', crewid, max_age)
264                 self._find_crew()
265
266         def _find_crew(self):
267                 self.crew = []
268                 capts = self._soup.findAll('img',
269                         src='/yoweb/images/crew-captain.png')
270                 if len(capts) != 1:
271                         self.msg('crew members: no. of captain images != 1')
272                         return
273                 tbl = capts[0]
274                 while not tbl.find('a', href=pirate_ref_re):
275                         tbl = tbl.findParent('table')
276                         if not tbl:
277                                 self.msg('crew members: cannot find table')
278                                 return
279                 current_rank_crew = None
280                 crew_rank_re = regexp.compile('/yoweb/images/crew')
281                 for row in tbl.contents:
282                         # findAll(recurse=False)
283                         if isinstance(row, unicode):
284                                 continue
285
286                         is_rank = row.find('img', attrs={'src': crew_rank_re})
287                         if is_rank:
288                                 rank = soup_text(row)
289                                 current_rank_crew = []
290                                 self.crew.append((rank, current_rank_crew))
291                                 continue
292                         for cell in row.findAll('a', href=pirate_ref_re):
293                                 if current_rank_crew is None:
294                                         self.soupm(cell, 'crew members: crew'
295                                                 ' before rank')
296                                         continue
297                                 current_rank_crew.append(soup_text(cell))
298
299         def __str__(self):
300                 return `(self.crew, self.msgs)`
301
302 class StandingsTable:
303         def __init__(self, use_puzzles=None, col_width=6):
304                 if use_puzzles is None:
305                         if opts.ship_duty:
306                                 use_puzzles=[
307                                         'Navigating','Battle Navigation',
308                                         'Gunning',
309                                         ['Sailing','Rigging'],
310                                         'Bilging',
311                                         'Carpentry',
312                                         'Treasure Haul'
313                                 ]
314                         else:
315                                 use_puzzles=puzzles
316                 self._puzzles = use_puzzles
317                 self.s = ''
318                 self._cw = col_width-1
319
320         def _pline(self, pirate, puzstrs):
321                 self.s += ' %-*s' % (max(max_pirate_namelen, 14), pirate)
322                 for v in puzstrs:
323                         self.s += ' %-*.*s' % (self._cw,self._cw, v)
324                 self.s += '\n'
325
326         def _puzstr(self, pi, puzzle):
327                 if not isinstance(puzzle,list): puzzle = [puzzle]
328                 try: standing = max([pi.standings[p] for p in puzzle])
329                 except KeyError: return '?'
330                 if not standing: return ''
331                 s = ''
332                 if self._cw > 4:
333                         c1 = standingvals[standing][0]
334                         if standing < 3: c1 = c1.lower() # 3 = Master
335                         s += `standing`
336                 if self._cw > 5:
337                         s += ' '
338                 s += '*' * (standing / 2)
339                 s += '+' * (standing % 2)
340                 return s
341
342         def headings(self):
343                 def puzn_redact(name):
344                         if isinstance(name,list):
345                                 return '/'.join(
346                                         ["%.*s" % (self._cw/2, puzn_redact(n))
347                                          for n in name])
348                         spc = name.find(' ')
349                         if spc < 0: return name
350                         return name[0:min(4,spc)] + name[spc+1:]
351                 self._pline('', map(puzn_redact, self._puzzles))
352         def literalline(self, line):
353                 self.s += line + '\n'
354         def pirate(self, pi):
355                 puzstrs = [self._puzstr(pi,puz) for puz in self._puzzles]
356                 self._pline(pi.name, puzstrs)
357
358         def results(self):
359                 return self.s
360
361 def do_pirate(pirates, bu):
362         print '{'
363         for pirate in pirates:
364                 info = PirateInfo(pirate)
365                 print '%s: %s,' % (`pirate`, info)
366         print '}'
367
368 def prep_crew_of(args, bu, max_age=300):
369         if len(args) != 1: bu('crew-of takes one pirate name')
370         pi = PirateInfo(args[0], max_age)
371         return CrewInfo(pi.crew[0], max_age)
372
373 def do_crew_of(args, bu):
374         ci = prep_crew_of(args, bu)
375         print ci
376
377 def do_standings_crew_of(args, bu):
378         ci = prep_crew_of(args, bu, 60)
379         tab = StandingsTable()
380         tab.headings()
381         for (rank, members) in ci.crew:
382                 if not members: continue
383                 tab.literalline('%s:' % rank)
384                 for p in members:
385                         pi = PirateInfo(p, random.randint(900,1800))
386                         tab.pirate(pi)
387         print tab.results()
388
389 class PirateAboard:
390         # pa.v
391         # pa.last_time
392         # pa.last_event
393         # pa.gunner
394         # pa.last_chat_time
395         # pa.last_chat_chan
396         def __init__(pa, v, time, event):
397                 pa.v = v
398                 pa.last_time = time
399                 pa.last_event = event
400                 pa.last_chat_time = None
401                 pa.last_chat_chan = None
402                 pa.gunner = False
403
404 class ChatLogTracker:
405         def __init__(self, myself_pi, logfn):
406                 self._pl = {}   # self._pl['Pirate'] =
407                 self._vl = {}   #   self._vl['Vessel']['Pirate'] = PirateAboard
408                                 # self._vl['Vessel']['#lastaboard']
409                 self._v = None          # self._v =
410                 self._vessel = None     #       self._vl[self._vessel]
411                 self._date = None
412                 self._myself = myself_pi
413                 self.need_redisplay = False
414                 self._f = file(logfn)
415                 self._lbuf = ''
416                 self._progress = [0, os.fstat(self._f.fileno()).st_size]
417
418         def _refresh(self):
419                 self.need_redisplay = True
420
421         def _onboard_event(self,timestamp,pirate,event):
422                 try: pa = self._pl[pirate]
423                 except KeyError: pa = None
424                 if pa is not None and pa.v is self._v:
425                         pa.last_time = timestamp
426                         pa.last_event = event
427                 else:
428                         if pa is not None: del pa.v[pirate]
429                         pa = PirateAboard(self._v, timestamp, event)
430                         self._pl[pirate] = pa
431                         self._v[pirate] = pa
432                 self._v['#lastaboard'] = timestamp
433                 self._refresh()
434                 return pa
435
436         def _trash_vessel(self, v):
437                 for pn in v:
438                         if pn.startswith('#'): continue
439                         del self._pl[pn]
440                 self._refresh()
441
442         def expire_garbage(self, timestamp):
443                 for (vn,v) in list(self._vl.iteritems()):
444                         la = v['#lastaboard']
445                         if timestamp - la > opts.ship_reboard_clearout:
446                                 self._debug_line_disposition(timestamp,'',
447                                         'stale reset '+vn)
448                                 self._trash_vessel(v)
449                                 del self._vl[vn]
450
451         def clear_vessel(self, timestamp):
452                 if self._v is not None:
453                         self._trash_vessel(self._v)
454                 self._v = {'#lastaboard': timestamp}
455                 self._vl[self._vessel] = self._v
456
457         def _debug_line_disposition(self,timestamp,l,m):
458                 debug('CLT %13s %-30s %s' % (timestamp,m,l))
459
460         def chatline(self,l):
461                 rm = lambda re: regexp.match(re,l)
462                 d = lambda m: self._debug_line_disposition(timestamp,l,m)
463                 timestamp = None
464
465                 m = rm('=+ (\\d+)/(\\d+)/(\\d+) =+$')
466                 if m:
467                         self._date = m.groups()
468                         return d('date '+`self._date`)
469
470                 if self._date is None:
471                         return d('date unset')
472
473                 m = rm('\\[(\d\d):(\d\d):(\d\d)\\] ')
474                 if not m:
475                         return d('no timestamp')
476
477                 time_tuple = [int(x) for x in self._date + m.groups()]
478                 time_tuple += (-1,-1,-1)
479                 timestamp = time.mktime(time_tuple)
480                 l = l[l.find(' ')+1:]
481
482                 def ob_x(who,event):
483                         return self._onboard_event(timestamp, who, event)
484                 def ob1(did): ob_x(m.group(1), did); return d(did)
485                 def oba(did): return ob1('%s %s' % (did, m.group(2)))
486
487                 m = rm('Going aboard the (\\S.*\\S)\\.\\.\\.$')
488                 if m:
489                         pn = self._myself.name
490                         self._vessel = m.group(1)
491                         dm = 'boarding'
492
493                         try:             self._v = self._vl[self._vessel]
494                         except KeyError: self._v = None; dm += ' new'
495                         
496                         if self._v is not None:  la = self._v['#lastaboard']
497                         else:                    la = 0; dm += ' ?la'
498
499                         if timestamp - la > opts.ship_reboard_clearout:
500                                 self.clear_vessel(timestamp)
501                                 dm += ' stale'
502
503                         ob_x(pn, 'we boarded')
504                         self.expire_garbage(timestamp)
505                         return d(dm)
506
507                 if self._v is None:
508                         return d('no vessel')
509
510                 m = rm('(\\w+) has come aboard\\.$')
511                 if m: return ob1('boarded');
512
513                 m = rm('You have ordered (\\w+) to do some (\\S.*\\S)\\.$')
514                 if m:
515                         (who,what) = m.groups()
516                         pa = ob_x(who,'ordered '+what)
517                         if what == 'Gunning':
518                                 pa.gunner = True
519                         return d('duty order')
520
521                 m = rm('(\\w+) abandoned a (\\S.*\\S) station\\.$')
522                 if m: oba('stopped'); return d('stopped')
523
524                 def chat(what):
525                         who = m.group(1)
526                         try: pa = self._pl[who]
527                         except KeyError: return d('chat mystery')
528                         if pa.v is self._v:
529                                 pa.last_chat_time = timestamp
530                                 pa.last_chat_chan = what
531                                 self._refresh()
532                                 return d(what+' chat')
533
534                 m = rm('(\\w+) (?:issued an order|ordered everyone) "')
535                 if m: return ob1('general order');
536
537                 m = rm('(\\w+) says, "')
538                 if m: return chat('public')
539
540                 m = rm('(\\w+) tells ye, "')
541                 if m: return chat('private')
542
543                 m = rm('(\\w+) flag officer chats, "')
544                 if m: return chat('flag officer')
545
546                 m = rm('(\\w+) officer chats, "')
547                 if m: return chat('officer')
548
549                 m = rm('Game over\\.  Winners: ([A-Za-z, ]+)\\.$')
550                 if m:
551                         pl = m.group(1).split(', ')
552                         if not self._myself.name in pl:
553                                 return d('lost boarding battle')
554                         for pn in pl:
555                                 if ' ' in pn: continue
556                                 ob_x(pn,'won boarding battle')
557                         return d('won boarding battle')
558
559                 m = rm('(\\w+) is eliminated\\!')
560                 if m: return ob1('eliminated in fray');
561
562                 m = rm('(\\w+) has left the vessel\.')
563                 if m:
564                         who = m.group(1)
565                         ob_x(who, 'disembarked')
566                         del self._v[who]
567                         del self._pl[who]
568                         return d('disembarked')
569
570                 return d('not matched')
571
572         def _str_vessel(self, vn, v):
573                 s = ' vessel %s\n' % vn
574                 s += ' '*20 + "%-*s   %13s\n" % (
575                                 max_pirate_namelen, '#lastaboard',
576                                 v['#lastaboard'])
577                 for pn in sorted(v.keys()):
578                         if pn.startswith('#'): continue
579                         pa = v[pn]
580                         assert pa.v == v
581                         assert self._pl[pn] == pa
582                         s += ' '*20 + "%s %-*s %13s %-30s %13s %s\n" % (
583                                 (' ','G')[pa.gunner],
584                                 max_pirate_namelen, pn,
585                                 pa.last_time, pa.last_event,
586                                 pa.last_chat_time, pa.last_chat_chan)
587                 return s
588
589         def __str__(self):
590                 s = '''<ChatLogTracker
591  myself %s
592  vessel %s
593 '''                     % (self._myself.name, self._vessel)
594                 assert ((self._v is None and self._vessel is None) or
595                         (self._v is self._vl[self._vessel]))
596                 if self._vessel is not None:
597                         s += self._str_vessel(self._vessel, self._v)
598                 for vn in sorted(self._vl.keys()):
599                         if vn == self._vessel: continue
600                         s += self._str_vessel(vn, self._vl[vn])
601                 for p in self._pl:
602                         pa = self._pl[p]
603                         assert pa.v[p] is pa
604                         assert pa.v in self._vl.values()
605                 s += '>\n'
606                 return s
607
608         def catchup(self, progress=None):
609                 while True:
610                         more = self._f.readline()
611                         if not more: break
612
613                         self._progress[0] += len(more)
614                         if progress: progress.progress(*self._progress)
615
616                         self._lbuf += more
617                         if self._lbuf.endswith('\n'):
618                                 self.chatline(self._lbuf.rstrip())
619                                 self._lbuf = ''
620                 if progress: progress.caughtup()
621
622 class ProgressPrintPercentage:
623         def __init__(self, f): self._f = f
624         def progress(self,done,total):
625                 self._f.write("scan chat logs %3d%%\r" % ((done*100) / total))
626                 self._f.flush()
627         def caughtup(self):
628                 self._f.write('                   \r')
629                 self._f.flush()
630
631 def do_track_chat_log(args, bu):
632         if len(args) != 1: bu('ship-aid takes only chat log filename')
633         logfn = args[0]
634         logfn_re = '(?:.*/)?([A-Z][a-z]+)_([a-z]+)_chat-log-\\w+$'
635         match = regexp.match(logfn_re, logfn)
636         if not match: bu('ship-aid chat log filename is not in default format')
637         (pirate, fetcher.ocean) = match.groups()
638
639         track = ChatLogTracker(pirate, logfn)
640         myself_pi = PirateInfo(pirate,3600)
641         track = ChatLogTracker(myself_pi, logfn)
642         track.catchup(ProgressPrintPercentage(sys.stdout))
643         while True:
644                 track.catchup()
645                 if track.need_redisplay:
646                         print track
647                         track.need_redisplay = False
648                 time.sleep(1)
649
650 def main():
651         global opts, fetcher
652
653         pa = OptionParser(
654 '''usage: .../yoweb-scrape [OPTION...] ACTION [ARGS...]
655 actions:
656  yoweb-scrape [--ocean OCEAN ...] pirate PIRATE
657  yoweb-scrape [--ocean OCEAN ...] crew-of PIRATE
658  yoweb-scrape [--ocean OCEAN ...] standings-crew-of PIRATE
659  yoweb-scrape [--ocean OCEAN ...] track-chat-log CHAT-LOG
660  yoweb-scrape [--ocean OCEAN ...] ship-aid CHAT-LOG
661 ''')
662         ao = pa.add_option
663         ao('-O','--ocean',dest='ocean', metavar='OCEAN', default=None,
664                 help='select ocean OCEAN')
665         ao('--cache-dir', dest='cache_dir', metavar='DIR',
666                 default='~/.yoweb-scrape-cache',
667                 help='cache yoweb pages in DIR')
668         ao('-D','--debug', action='store_true', dest='debug', default=False,
669                 help='enable debugging output')
670         ao('-q','--quiet', action='store_true', dest='quiet',
671                 help='suppress warning output')
672
673         ao('--ship-duty', action='store_true', dest='ship_duty',
674                 help='show ship duty station puzzles')
675
676         (opts,args) = pa.parse_args()
677         random.seed()
678
679         if len(args) < 1:
680                 pa.error('need a mode argument')
681
682         mode = args[0]
683         mode_fn_name = 'do_' + mode.replace('_','#').replace('-','_')
684         try: mode_fn = globals()[mode_fn_name]
685         except KeyError: pa.error('unknown mode "%s"' % mode)
686
687         # fixed parameters
688         opts.min_max_age = 60
689         opts.expire_age = 3600
690         opts.ship_reboard_clearout = 3600
691
692         if opts.cache_dir.startswith('~/'):
693                 opts.cache_dir = os.getenv('HOME') + opts.cache_dir[1:]
694
695         fetcher = Fetcher(opts.ocean, opts.cache_dir)
696
697         mode_fn(args[1:], pa.error)
698
699 main()