2 # This is part of ypp-sc-tools, a set of third-party tools for assisting
3 # players of Yohoho Puzzle Pirates.
5 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
21 # are used without permission. This program is not endorsed or
22 # sponsored by Three Rings.
25 yoweb-scrape is part of ypp-sc-tools Copyright (C) 2009 Ian Jackson
26 This program comes with ABSOLUTELY NO WARRANTY; this is free software,
27 and you are welcome to redistribute it under certain conditions.
28 For details, read the top of the yoweb-scrape file.
31 #---------- setup ----------
34 signal.signal(signal.SIGINT, signal.SIG_DFL)
47 from optparse import OptionParser
48 from StringIO import StringIO
50 from BeautifulSoup import BeautifulSoup
54 #---------- YPP parameters and arrays ----------
56 puzzles = ('Swordfighting/Bilging/Sailing/Rigging/Navigating'+
57 '/Battle Navigation/Gunning/Carpentry/Rumble/Treasure Haul'+
58 '/Drinking/Spades/Hearts/Treasure Drop/Poker/Distilling'+
59 '/Alchemistry/Shipwrightery/Blacksmithing/Foraging').split('/')
63 ['Sailing','Rigging'],
68 duty_puzzles = ([ 'Navigating', 'Battle Navigation' ] +
72 standingvals = ('Able/Proficient/Distinguished/Respected/Master'+
73 '/Renowned/Grand-Master/Legendary/Ultimate').split('/')
74 standing_limit = len(standingvals)
76 pirate_ref_re = regexp.compile('^/yoweb/pirate\\.wm')
78 max_pirate_namelen = 12
81 #---------- general utilities ----------
85 print >>opts.debug_file, m
89 opts.debug_file.flush()
95 def format_time_interval(ti):
96 if ti < 120: return '%d:%02d' % (ti / 60, ti % 60)
97 if ti < 7200: return '%2dm' % (ti / 60)
98 if ti < 86400: return '%dh' % (ti / 3600)
99 return '%dd' % (ti / 86400)
101 #---------- caching and rate-limiting data fetcher ----------
104 def __init__(self, ocean, cachedir):
105 debug('Fetcher init %s' % cachedir)
107 self.cachedir = cachedir
108 try: os.mkdir(cachedir)
109 except (OSError,IOError), oe:
110 if oe.errno != errno.EEXIST: raise
111 self._cache_scan(time.time())
113 def default_ocean(self, ocean='ice'):
114 if self.ocean is None:
117 def _cache_scan(self, now):
118 # returns list of ages, unsorted
120 debug('Fetcher scan_cache')
121 for leaf in os.listdir(self.cachedir):
122 if not leaf.startswith('#'): continue
123 path = self.cachedir + '/' + leaf
124 try: s = os.stat(path)
125 except (OSError,IOError), oe:
126 if oe.errno != errno.ENOENT: raise
128 age = now - s.st_mtime
129 if age > opts.expire_age:
130 debug('Fetcher expire %d %s' % (age, path))
132 except (OSError,IOError), oe:
133 if oe.errno != errno.ENOENT: raise
138 def need_wait(self, now, imaginary=[]):
139 ages = self._cache_scan(now)
142 debug('Fetcher ages ' + `ages`)
146 if age < min_age and age <= 5:
147 debug('Fetcher morewait min=%d age=%d' %
149 need_wait = max(need_wait, min_age - age)
153 need_wait += random.random() - 0.5
156 def _rate_limit_cache_clean(self, now):
157 need_wait = self.need_wait(now)
159 debug('Fetcher wait %d' % need_wait)
162 def fetch(self, url, max_age):
163 debug('Fetcher fetch %s' % url)
164 cache_corename = urllib.quote_plus(url)
165 cache_item = "%s/#%s#" % (self.cachedir, cache_corename)
166 try: f = file(cache_item, 'r')
167 except (OSError,IOError), oe:
168 if oe.errno != errno.ENOENT: raise
171 max_age = max(opts.min_max_age, min(max_age, opts.expire_age))
173 s = os.fstat(f.fileno())
174 age = now - s.st_mtime
176 debug('Fetcher stale %d < %d'% (max_age, age))
181 debug('Fetcher cached %d > %d' % (max_age, age))
184 debug('Fetcher fetch')
185 self._rate_limit_cache_clean(now)
187 stream = urllib2.urlopen(url)
189 cache_tmp = "%s/#%s~%d#" % (
190 self.cachedir, cache_corename, os.getpid())
191 f = file(cache_tmp, 'w')
194 os.rename(cache_tmp, cache_item)
195 debug('Fetcher stored')
198 def yoweb(self, kind, tail, max_age):
200 url = 'http://%s.puzzlepirates.com/yoweb/%s%s' % (
201 self.ocean, kind, tail)
202 return self.fetch(url, max_age)
204 #---------- logging assistance for troubled screenscrapers ----------
211 def soupm(self, obj, m):
212 self.msg(m + '; in ' + `obj`)
213 def needs_msgs(self, child_souplog):
214 self.msgs += child_souplog.msgs
215 child_souplog.msgs = [ ]
218 str = ''.join(obj.findAll(text=True))
221 class SomethingSoupInfo(SoupLog):
222 def __init__(self, kind, tail, max_age):
223 SoupLog.__init__(self)
224 html = fetcher.yoweb(kind, tail, max_age)
225 self._soup = BeautifulSoup(html,
226 convertEntities=BeautifulSoup.HTML_ENTITIES
229 #---------- scraper for pirate pages ----------
231 class PirateInfo(SomethingSoupInfo):
232 # Public data members:
233 # pi.standings = { 'Treasure Haul': 'Able' ... }
235 # pi.crew = (id, name)
236 # pi.flag = (id, name)
237 # pi.msgs = [ 'message describing problem with scrape' ]
239 def __init__(self, pirate, max_age=300):
240 SomethingSoupInfo.__init__(self,
241 'pirate.wm?target=', pirate, max_age)
243 self._find_standings()
244 self.crew = self._find_crewflag('crew',
245 '^/yoweb/crew/info\\.wm')
246 self.flag = self._find_crewflag('flag',
247 '^/yoweb/flag/info\\.wm')
249 def _find_standings(self):
250 imgs = self._soup.findAll('img',
251 src=regexp.compile('/yoweb/images/stat.*'))
253 u'\\s*\\S*/([-A-Za-z]+)\\s*$|\\s*\\S*/\\S*\\s*\\(ocean\\-wide(?:\\s|\\xa0)+([-A-Za-z]+)\\)\\s*$'
257 for skill in puzzles:
258 standings[skill] = [ ]
263 try: puzzle = img['alt']
264 except KeyError: continue
266 if not puzzle in puzzles:
267 skl.soupm(img, 'unknown puzzle: "%s"' % puzzle)
269 key = img.findParent('td')
271 skl.soupm(img, 'puzzle at root! "%s"' % puzzle)
273 valelem = key.findNextSibling('td')
275 skl.soupm(key, 'puzzle missing sibling "%s"'
278 valstr = soup_text(valelem)
279 match = re.match(valstr)
281 skl.soupm(key, ('puzzle "%s" unparseable'+
282 ' standing "%s"') % (puzzle, valstr))
284 standing = match.group(match.lastindex)
285 standings[puzzle].append(standing)
289 for puzzle in puzzles:
290 sl = standings[puzzle]
292 skl.msg('puzzle "%s" multiple standings %s' %
296 skl.msg('puzzle "%s" no standing found' % puzzle)
299 for i in range(0, standing_limit):
300 if standing == standingvals[i]:
301 self.standings[puzzle] = i
302 if not puzzle in self.standings:
303 skl.msg('puzzle "%s" unknown standing "%s"' %
306 all_standings_ok = True
307 for puzzle in puzzles:
308 if not puzzle in self.standings:
311 def _find_crewflag(self, cf, yoweb_re):
312 things = self._soup.findAll('a', href=regexp.compile(yoweb_re))
314 self.msg('zero or several %s id references found' % cf)
317 id_re = '\\b%sid\\=(\\w+)$' % cf
318 id_haystack = thing['href']
319 match = regexp.compile(id_re).search(id_haystack)
321 self.soupm(thing, ('incomprehensible %s id ref'+
322 ' (%s in %s)') % (cf, id_re, id_haystack))
324 name = soup_text(thing)
325 return (match.group(1), name)
328 return `(self.crew, self.flag, self.standings, self.msgs)`
330 #---------- scraper for crew pages ----------
332 class CrewInfo(SomethingSoupInfo):
333 # Public data members:
334 # ci.crew = [ ('Captain', ['Pirate', ...]),
335 # ('Senior Officer', [...]),
337 # pi.msgs = [ 'message describing problem with scrape' ]
339 def __init__(self, crewid, max_age=300):
340 SomethingSoupInfo.__init__(self,
341 'crew/info.wm?crewid=', crewid, max_age)
344 def _find_crew(self):
346 capts = self._soup.findAll('img',
347 src='/yoweb/images/crew-captain.png')
349 self.msg('crew members: no. of captain images != 1')
352 while not tbl.find('a', href=pirate_ref_re):
353 tbl = tbl.findParent('table')
355 self.msg('crew members: cannot find table')
357 current_rank_crew = None
358 crew_rank_re = regexp.compile('/yoweb/images/crew')
359 for row in tbl.contents:
360 # findAll(recurse=False)
361 if isinstance(row,basestring):
364 is_rank = row.find('img', attrs={'src': crew_rank_re})
366 rank = soup_text(row)
367 current_rank_crew = []
368 self.crew.append((rank, current_rank_crew))
370 for cell in row.findAll('a', href=pirate_ref_re):
371 if current_rank_crew is None:
372 self.soupm(cell, 'crew members: crew'
375 current_rank_crew.append(soup_text(cell))
378 return `(self.crew, self.msgs)`
380 #---------- pretty-printer for tables of pirate puzzle standings ----------
382 class StandingsTable:
383 def __init__(self, f, use_puzzles=None, col_width=6, gap_every=5):
384 if use_puzzles is None:
386 use_puzzles=duty_puzzles
389 self._puzzles = use_puzzles
391 self._cw = col_width-1
392 self._gap_every = gap_every
396 def _nl(self): self._o('\n')
398 def _pline(self, pirate, puzstrs, extra):
399 if (self._linecount > 0
400 and self._gap_every is not None
401 and not (self._linecount % self._gap_every)):
403 self._o(' %-*s' % (max(max_pirate_namelen, 14), pirate))
405 self._o(' %-*.*s' % (self._cw,self._cw, v))
411 def _puzstr(self, pi, puzzle):
412 if not isinstance(puzzle,list): puzzle = [puzzle]
413 try: standing = max([pi.standings[p] for p in puzzle])
414 except KeyError: return '?'
415 if not standing: return ''
418 c1 = standingvals[standing][0]
419 if standing < 3: c1 = c1.lower() # 3 = Master
423 s += '*' * (standing / 2)
424 s += '+' * (standing % 2)
427 def headings(self, lhs='', rhs=None):
428 def puzn_redact(name):
429 if isinstance(name,list):
431 ["%.*s" % (self._cw/2, puzn_redact(n))
434 if spc < 0: return name
435 return name[0:min(4,spc)] + name[spc+1:]
437 self._pline(lhs, map(puzn_redact, self._puzzles), rhs)
439 def literalline(self, line):
443 def pirate_dummy(self, name, standingstring, extra=None):
444 self._pline(name, standingstring * len(self._puzzles), extra)
445 def pirate(self, pi, extra=None):
446 puzstrs = [self._puzstr(pi,puz) for puz in self._puzzles]
447 self._pline(pi.name, puzstrs, extra)
450 #---------- chat log parser ----------
453 # This is essentially a transparent, dumb, data class.
463 def __init__(pa, pn, v, time, event):
467 pa.last_event = event
468 pa.last_chat_time = None
469 pa.last_chat_chan = None
476 age = now - pa.pi_fetched
477 guide = random.randint(120,240)
480 debug('PirateAboard refresh %d > %d %s' % (
481 age, guide, pa.name))
485 wait = fetcher.need_wait(now, imaginary)
487 debug('PirateAboard fetcher not ready %d' % wait)
489 pa.pi = PirateInfo(pa.name, 600)
493 class ChatLogTracker:
494 # This is quite complex so we make it opaque. Use the
495 # official invokers, accessors etc.
497 def __init__(self, myself_pi, logfn):
498 self._pl = {} # self._pl['Pirate'] =
499 self._vl = {} # self._vl['Vessel']['Pirate'] = PirateAboard
500 # self._vl['Vessel']['#lastinfo']
501 # self._vl['Vessel']['#name']
502 # self._v = self._vl[self._vessel]
504 self._myself = myself_pi
505 self._f = file(logfn)
507 self._progress = [0, os.fstat(self._f.fileno()).st_size]
508 self._disembark_myself()
509 self._need_redisplay = False
510 self._lastvessel = None
512 def _disembark_myself(self):
515 self.force_redisplay()
517 def force_redisplay(self):
518 self._need_redisplay = True
520 def _vessel_updated(self, v, timestamp):
521 v['#lastinfo'] = timestamp
522 self.force_redisplay()
524 def _onboard_event(self,v,timestamp,pirate,event):
525 pa = self._pl.get(pirate, None)
526 if pa is not None and pa.v is v:
527 pa.last_time = timestamp
528 pa.last_event = event
530 if pa is not None: del pa.v[pirate]
531 pa = PirateAboard(pirate, v, timestamp, event)
532 self._pl[pirate] = pa
534 self._vessel_updated(v, timestamp)
537 def _trash_vessel(self, v):
539 if pn.startswith('#'): continue
543 if v is self._v: self._disembark_myself()
544 self.force_redisplay()
546 def _vessel_stale(self, v, timestamp):
547 return timestamp - v['#lastinfo'] > opts.ship_reboard_clearout
549 def _vessel_check_expire(self, v, timestamp):
550 if not self._vessel_stale(v, timestamp):
552 self._debug_line_disposition(timestamp,'',
553 'stale-reset ' + v['#name'])
554 self._trash_vessel(v)
557 def expire_garbage(self, timestamp):
558 for v in self._vl.values():
559 self._vessel_check_expire(v, timestamp)
561 def _vessel_lookup(self, vn, timestamp, dml=[], create=False):
562 v = self._vl.get(vn, None)
564 v = self._vessel_check_expire(v, timestamp)
571 self._vl[vn] = v = { '#name': vn }
572 self._vessel_updated(v, timestamp)
575 def _find_matching_vessel(self, pattern, timestamp, cmdr,
576 dml=[], create=False):
577 # use when a commander pirate `cmdr' specified a vessel
578 # by name `pattern' (either may be None)
579 # if create is true, will create the vessel
580 # record if an exact name is specified
582 if (pattern is not None and
584 and len(pattern.split(' ')) == 2):
587 return self._vessel_lookup(
588 vn, timestamp, dml=dml, create=create)
591 pattern_check = lambda vn: True
593 re = '(?:.* )?%s$' % pattern.lower().replace('*','.+')
594 pattern_check = regexp.compile(re, regexp.I).match
598 cmdr_pa = self._pl.get(cmdr, None)
599 if cmdr_pa: tries.append((cmdr_pa.v, 'cmdr'))
601 tries.append((self._v, 'here'))
604 for (v, dm) in tries:
605 if v is None: dml.append(dm+'?'); continue
608 if not pattern_check(vn):
616 if pattern is not None and '*' in pattern:
619 for (vn,v) in self._vl.iteritems()
620 if not self._vessel_stale(v, timestamp)
623 #debug('CLT-RE /%s/ wanted (%s) searched (%s)' % (
625 # '/'.join(tried_vns),
626 # '/'.join([vn for (vn,v) in search])))
636 def _debug_line_disposition(self,timestamp,l,m):
637 debug('CLT %13s %-40s %s' % (timestamp,m,l))
639 def chatline(self,l):
640 rm = lambda re: regexp.match(re,l)
641 d = lambda m: self._debug_line_disposition(timestamp,l,m)
644 m = rm('=+ (\\d+)/(\\d+)/(\\d+) =+$')
646 self._date = [int(x) for x in m.groups()]
647 self._previous_timestamp = None
648 return d('date '+`self._date`)
650 if self._date is None:
651 return d('date unset')
653 m = rm('\\[(\d\d):(\d\d):(\d\d)\\] ')
655 return d('no timestamp')
658 time_tuple = (self._date +
659 [int(x) for x in m.groups()] +
661 timestamp = time.mktime(time_tuple)
662 if timestamp >= self._previous_timestamp: break
664 self._debug_line_disposition(timestamp,'',
665 'new date '+`self._date`)
667 self._previous_timestamp = timestamp
669 l = l[l.find(' ')+1:]
671 def ob_x(pirate,event):
672 return self._onboard_event(
673 self._v, timestamp, pirate, event)
674 def ob1(did): ob_x(m.group(1), did); return d(did)
675 def oba(did): return ob1('%s %s' % (did, m.group(2)))
677 def disembark(v, timestamp, pirate, event):
679 v, timestamp, pirate, 'leaving '+event)
683 def disembark_me(why):
684 self._disembark_myself()
685 return d('disembark-me '+why)
687 m = rm('Going aboard the (\\S.*\\S)\\.\\.\\.$')
690 pn = self._myself.name
692 v = self._vessel_lookup(vn, timestamp, dm, create=True)
693 self._lastvessel = self._vessel = vn
695 ob_x(pn, 'we boarded')
696 self.expire_garbage(timestamp)
697 return d(' '.join(dm))
700 return d('no vessel')
702 m = rm('(\\w+) has come aboard\\.$')
703 if m: return ob1('boarded');
705 m = rm('You have ordered (\\w+) to do some (\\S.*\\S)\\.$')
707 (who,what) = m.groups()
708 pa = ob_x(who,'ord '+what)
709 if what == 'Gunning':
711 return d('duty order')
713 m = rm('(\\w+) abandoned a (\\S.*\\S) station\\.$')
714 if m: oba('stopped'); return d("end")
716 def chat_core(speaker, chan):
717 try: pa = self._pl[speaker]
718 except KeyError: return 'mystery'
719 if pa.v is not self._v: return 'elsewhere'
720 pa.last_chat_time = timestamp
721 pa.last_chat_chan = chan
722 self.force_redisplay()
727 dm = chat_core(speaker, chan)
728 return d('chat %s %s' % (chan, dm))
730 def chat_metacmd(chan):
731 (cmdr, metacmd) = m.groups()
732 metacmd = regexp.sub('\\s+', ' ', metacmd).strip()
734 '/([ad]) (?:([A-Za-z* ]+)\\s*:)?([A-Za-z ]+)$',
736 if not m2: return chat(chan)
738 (cmd, pattern, targets) = m2.groups()
739 dml = ['cmd', chan, cmd]
741 if cmd == 'a': each = self._onboard_event
742 else: each = disembark
744 if cmdr == self._myself.name:
746 how = 'cmd: %s' % cmd
749 how = 'cmd: %s %s' % (cmd,cmdr)
751 v = self._find_matching_vessel(
752 pattern, timestamp, cmdr, dml, create=True)
755 targets = targets.strip().split(' ')
756 dml.append(`len(targets)`)
757 for target in targets:
758 each(v, timestamp, target.title(), how)
759 self._vessel_updated(v, timestamp)
762 chat_core(cmdr, 'cmd '+chan)
765 m = rm('(\\w+) (?:issued an order|ordered everyone) "')
766 if m: return ob1('general order');
768 m = rm('(\\w+) says, "')
769 if m: return chat('public')
771 m = rm('(\\w+) tells ye, "')
772 if m: return chat('private')
774 m = rm('Ye told (\\w+), "(.*)"$')
775 if m: return chat_metacmd('private')
777 m = rm('(\\w+) flag officer chats, "')
778 if m: return chat('flag officer')
780 m = rm('(\\w+) officer chats, "(.*)"$')
781 if m: return chat_metacmd('officer')
783 m = rm('Ye accepted the offer to job with ')
784 if m: return disembark_me('jobbing')
786 m = rm('Ye hop on the ferry and are whisked away ')
787 if m: return disembark_me('ferry')
789 m = rm('Whisking away to yer home on the magical winds')
790 if m: return disembark_me('home')
792 m = rm('Game over\\. Winners: ([A-Za-z, ]+)\\.$')
794 pl = m.group(1).split(', ')
795 if not self._myself.name in pl:
796 return d('lost melee')
798 if ' ' in pn: continue
800 return d('won melee')
802 m = rm('(\\w+) is eliminated\\!')
803 if m: return ob1('eliminated in fray');
805 m = rm('(\\w+) has driven \w+ from the ship\\!')
806 if m: return ob1('boarder repelled');
808 m = rm('\w+ has bested (\\w+), and turns'+
809 ' to the rest of the ship\\.')
810 if m: return ob1('boarder unrepelled');
812 m = rm('(\\w+) has left the vessel\.')
815 disembark(self._v, timestamp, pirate, 'disembarked')
816 return d('disembarked')
818 return d('not-matched')
820 def _str_vessel(self, vn, v):
821 s = ' vessel %s\n' % vn
822 s += ' '*20 + "%-*s %13s\n" % (
823 max_pirate_namelen, '#lastinfo',
825 assert v['#name'] == vn
826 for pn in sorted(v.keys()):
827 if pn.startswith('#'): continue
830 assert self._pl[pn] == pa
831 s += ' '*20 + "%s %-*s %13s %-30s %13s %s\n" % (
832 (' ','G')[pa.gunner],
833 max_pirate_namelen, pn,
834 pa.last_time, pa.last_event,
835 pa.last_chat_time, pa.last_chat_chan)
839 s = '''<ChatLogTracker
842 ''' % (self._myself.name, self._vessel)
843 assert ((self._v is None and self._vessel is None) or
844 (self._v is self._vl[self._vessel]))
845 if self._vessel is not None:
846 s += self._str_vessel(self._vessel, self._v)
847 for vn in sorted(self._vl.keys()):
848 if vn == self._vessel: continue
849 s += self._str_vessel(vn, self._vl[vn])
853 assert pa.v in self._vl.values()
857 def catchup(self, progress=None):
859 more = self._f.readline()
862 self._progress[0] += len(more)
863 if progress: progress.progress(*self._progress)
866 if self._lbuf.endswith('\n'):
867 self.chatline(self._lbuf.rstrip())
870 debug(self.__str__())
871 if progress: progress.caughtup()
874 rv = self._need_redisplay
875 self._need_redisplay = False
878 # returns our pirate name
879 return self._myself.name
880 def vesselname(self):
881 # returns the vessel name we're aboard or None
883 def lastvesselname(self):
884 # returns the last vessel name we were aboard or None
885 return self._lastvessel
886 def aboard(self, vesselname=True):
887 # returns a list of PirateAboard the vessel
888 # sorted by pirate name
889 # you can pass this None and you'll get []
890 # or True for the current vessel (which is the default)
891 # the returned value is a fresh list of persistent
892 # PirateAboard objects
893 if vesselname is True: v = self._v
894 else: v = self._vl.get(vesselname.title())
895 if v is None: return []
897 for pn in sorted(v.keys())
898 if not pn.startswith('#') ]
900 #---------- implementations of actual operation modes ----------
902 def do_pirate(pirates, bu):
904 for pirate in pirates:
905 info = PirateInfo(pirate)
906 print '%s: %s,' % (`pirate`, info)
909 def prep_crew_of(args, bu, max_age=300):
910 if len(args) != 1: bu('crew-of takes one pirate name')
911 pi = PirateInfo(args[0], max_age)
912 if pi.crew is None: return None
913 return CrewInfo(pi.crew[0], max_age)
915 def do_crew_of(args, bu):
916 ci = prep_crew_of(args, bu)
919 def do_standings_crew_of(args, bu):
920 ci = prep_crew_of(args, bu, 60)
921 tab = StandingsTable(sys.stdout)
923 for (rank, members) in ci.crew:
924 if not members: continue
926 tab.literalline('%s:' % rank)
928 pi = PirateInfo(p, random.randint(900,1800))
931 class ProgressPrintPercentage:
932 def __init__(self, f=sys.stdout):
934 def progress_string(self,done,total):
935 return "scan chat logs %3d%%\r" % ((done*100) / total)
936 def progress(self,*a):
937 self._f.write(self.progress_string(*a))
939 def show_init(self, pirate, ocean):
940 print >>self._f, 'Starting up, %s on the %s ocean' % (
946 #----- modes which use the chat log parser are quite complex -----
948 def prep_chat_log(args, bu,
949 progress=ProgressPrintPercentage(),
950 max_myself_age=3600):
951 if len(args) != 1: bu('this action takes only chat log filename')
953 logfn_re = '(?:.*/)?([A-Z][a-z]+)_([a-z]+)_'
954 match = regexp.match(logfn_re, logfn)
955 if not match: bu('chat log filename is not in expected format')
956 (pirate, ocean) = match.groups()
957 fetcher.default_ocean(ocean)
959 progress.show_init(pirate, fetcher.ocean)
960 myself = PirateInfo(pirate,max_myself_age)
961 track = ChatLogTracker(myself, logfn)
964 track.catchup(progress)
967 track.force_redisplay()
969 return (myself, track)
971 def do_track_chat_log(args, bu):
972 (myself, track) = prep_chat_log(args, bu)
979 #----- ship management aid -----
981 class Display_dumb(ProgressPrintPercentage):
983 ProgressPrintPercentage.__init__(self)
989 class Display_overwrite(ProgressPrintPercentage):
991 ProgressPrintPercentage.__init__(self)
993 null = file('/dev/null','w')
994 curses.setupterm(fd=null.fileno())
996 self._clear = curses.tigetstr('clear')
998 self._debug('missing clear!')
999 self.show = Display_dumb.show
1002 self._t = {'el':'', 'ed':''}
1003 if not self._init_sophisticated():
1004 for k in self._t.keys(): self._t[k] = ''
1005 self._t['ho'] = self._clear
1007 def _debug(self,m): debug('display overwrite: '+m)
1009 def _init_sophisticated(self):
1010 for k in self._t.keys():
1011 s = curses.tigetstr(k)
1013 self._t['ho'] = curses.tigetstr('ho')
1014 if not self._t['ho']:
1015 cup = curses.tigetstr('cup')
1016 self._t['ho'] = curses.tparm(cup,0,0)
1017 missing = [k for k in self._t.keys() if not self._t[k]]
1019 self.debug('missing '+(' '.join(missing)))
1024 w = sys.stdout.write
1025 def wti(k): w(self._t[k])
1029 for l in s.rstrip().split('\n'):
1038 def realstart(self):
1039 sys.stdout.write(self._clear)
1043 def do_ship_aid(args, bu):
1044 if opts.ship_duty is None: opts.ship_duty = True
1046 displayer = globals()['Display_'+opts.display]()
1048 (myself, track) = prep_chat_log(args, bu, progress=displayer)
1050 displayer.realstart()
1052 if os.isatty(0): kr_create = KeystrokeReader
1053 else: kr_create = DummyKeystrokeReader
1056 kreader = kr_create(0, 10)
1057 ship_aid_core(myself, track, displayer, kreader)
1062 class KeyBasedSorter:
1063 def compar_key_pa(self, pa):
1064 pi = pa.pirate_info()
1065 if pi is None: return None
1066 return self.compar_key(pi)
1067 def lsort_pa(self, l):
1068 l.sort(key = self.compar_key_pa)
1070 class NameSorter(KeyBasedSorter):
1071 def compar_key(self, pi): return pi.name
1072 def desc(self): return 'name'
1074 class SkillSorter(NameSorter):
1075 def __init__(self, relevant):
1076 self._want = frozenset(relevant.split('/'))
1078 for p in core_duty_puzzles:
1079 if isinstance(p,basestring): self._avoid.add(p)
1080 else: self._avoid |= set(p)
1081 self._avoid -= self._want
1082 self._desc = '%s' % relevant
1084 def desc(self): return self._desc
1086 def compar_key(self, pi):
1088 pi.standings.get(puz,-1)
1089 for puz in self._want
1092 -pi.standings.get(puz,standing_limit)
1093 for puz in self._avoid
1096 def negate(x): return -x
1097 debug('compar_key %s bw=%s ba=%s' % (pi.name, `best_want`,
1099 return (-best_want, map(negate, best_avoid), pi.name)
1101 def ship_aid_core(myself, track, displayer, kreader):
1104 vn = track.vesselname()
1105 if vn: return (vn, " on board the %s" % vn)
1106 vn = track.lastvesselname()
1107 if vn: return (vn, " ashore from the %s" % vn)
1108 return (None, " not on a vessel")
1111 if t is None: return ' ' * 22
1112 return " %-4s %-16s" % (format_time_interval(now - t),e)
1114 displayer.show(track.myname() + find_vessel()[1] + '...')
1124 (vn, s) = find_vessel()
1125 s = track.myname() + s
1126 s += " at %s" % time.strftime("%Y-%m-%d %H:%M:%S")
1130 aboard = track.aboard(vn)
1131 sort.lsort_pa(aboard)
1134 tbl = StandingsTable(tbl_s)
1136 if track.vesselname(): howmany = ' %d aboard' % len(aboard)
1139 tbl.headings(howmany, ' sorted by '+sort.desc())
1142 pi = pa.pirate_info()
1145 if pa.gunner: xs += 'G '
1147 xs += timeevent(pa.last_time, pa.last_event)
1148 xs += timeevent(pa.last_chat_time, pa.last_chat_chan)
1151 tbl.pirate_dummy(pa.name, rotate_nya[0], xs)
1155 s += tbl_s.getvalue()
1161 rotate_nya = rotate_nya[1:3] + rotate_nya[0]
1165 elif k == 'g': sort = SkillSorter('Gunning')
1166 elif k == 'c': sort = SkillSorter('Carpentry')
1167 elif k == 's': sort = SkillSorter('Sailing/Rigging')
1168 elif k == 'b': sort = SkillSorter('Bilging')
1169 elif k == 'n': sort = SkillSorter('Navigating')
1170 elif k == 'd': sort = SkillSorter('Battle Navigation')
1171 elif k == 't': sort = SkillSorter('Treasure Haul')
1172 elif k == 'a': sort = NameSorter()
1173 else: pass # unknown key command
1175 #---------- individual keystroke input ----------
1177 class DummyKeystrokeReader:
1178 def __init__(self,fd,timeout_dummy): pass
1179 def stop(self): pass
1180 def getch(self): sleep(1); return None
1181 def info(self): return ' [noninteractive]'
1183 class KeystrokeReader(DummyKeystrokeReader):
1184 def __init__(self, fd, timeout_decisec=0):
1186 self._saved = termios.tcgetattr(fd)
1187 a = termios.tcgetattr(fd)
1188 a[3] &= ~(termios.ECHO | termios.ECHONL |
1189 termios.ICANON | termios.IEXTEN)
1190 a[6][termios.VMIN] = 0
1191 a[6][termios.VTIME] = timeout_decisec
1192 termios.tcsetattr(fd, termios.TCSANOW, a)
1194 termios.tcsetattr(self._fd, termios.TCSANOW, self._saved)
1197 byte = os.read(self._fd, 1)
1198 if not len(byte): return None
1203 #---------- main program ----------
1206 global opts, fetcher
1209 '''usage: .../yoweb-scrape [OPTION...] ACTION [ARGS...]
1211 yoweb-scrape [--ocean OCEAN ...] pirate PIRATE
1212 yoweb-scrape [--ocean OCEAN ...] crew-of PIRATE
1213 yoweb-scrape [--ocean OCEAN ...] standings-crew-of PIRATE
1214 yoweb-scrape [--ocean OCEAN ...] track-chat-log CHAT-LOG
1215 yoweb-scrape [options] ship-aid CHAT-LOG (must be .../PIRATE_OCEAN_chat-log*)
1217 display modes (for --display) apply to ship-aid:
1218 --display=dumb just print new information, scrolling the screen
1219 --display=overwrite use cursor motion, selective clear, etc. to redraw at top''')
1221 ao('-O','--ocean',dest='ocean', metavar='OCEAN', default=None,
1222 help='select ocean OCEAN')
1223 ao('--cache-dir', dest='cache_dir', metavar='DIR',
1224 default='~/.yoweb-scrape-cache',
1225 help='cache yoweb pages in DIR')
1226 ao('-D','--debug', action='count', dest='debug', default=0,
1227 help='enable debugging output')
1228 ao('--debug-fd', type='int', dest='debug_fd',
1229 help='write any debugging output to specified fd')
1230 ao('-q','--quiet', action='store_true', dest='quiet',
1231 help='suppress warning output')
1232 ao('--display', action='store', dest='display',
1233 type='choice', choices=['dumb','overwrite'],
1234 help='how to display ship aid')
1236 ao('--ship-duty', action='store_true', dest='ship_duty',
1237 help='show ship duty station puzzles')
1238 ao('--all-puzzles', action='store_false', dest='ship_duty',
1239 help='show all puzzles, not just ship duty stations')
1241 ao('--min-cache-reuse', type='int', dest='min_max_age',
1242 metavar='SECONDS', default=60,
1243 help='always reuse cache yoweb data if no older than this')
1245 (opts,args) = pa.parse_args()
1249 print >>sys.stderr, copyright_info
1250 pa.error('need a mode argument')
1252 if opts.debug_fd is not None:
1253 opts.debug_file = os.fdopen(opts.debug_fd, 'w')
1255 opts.debug_file = sys.stdout
1258 mode_fn_name = 'do_' + mode.replace('_','#').replace('-','_')
1259 try: mode_fn = globals()[mode_fn_name]
1260 except KeyError: pa.error('unknown mode "%s"' % mode)
1263 opts.expire_age = max(3600, opts.min_max_age)
1265 opts.ship_reboard_clearout = 3600
1267 if opts.cache_dir.startswith('~/'):
1268 opts.cache_dir = os.getenv('HOME') + opts.cache_dir[1:]
1270 if opts.display is None:
1271 if ((opts.debug > 0 and opts.debug_fd is None)
1272 or not os.isatty(sys.stdout.fileno())):
1273 opts.display = 'dumb'
1275 opts.display = 'overwrite'
1277 fetcher = Fetcher(opts.ocean, opts.cache_dir)
1279 mode_fn(args[1:], pa.error)