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, lhs, 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+1, 15), lhs))
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 standings = standingstring * len(self._puzzles)
445 self._pline(' '+name, standings, extra)
446 def pirate(self, pi, extra=None):
447 puzstrs = [self._puzstr(pi,puz) for puz in self._puzzles]
448 self._pline(' '+pi.name, puzstrs, extra)
451 #---------- chat log parser ----------
454 # This is essentially a transparent, dumb, data class.
464 # Also used for jobbing applicants:
465 # happens when expires (to "-")
466 # - disembark, leaves crew no
467 # aboard evidence of them being aboard no
468 # applied "has applied for the job" 120s, configurable
469 # ashore "has taken a job" 30min, configurable
470 # declined "declined the job offer" 30s, configurable
471 # invited "has been invited to job" 120s, configurable
473 # pa.jobber None, 'ashore', 'applied', 'invited', 'declined'
474 # pa.expires expiry time time
476 def __init__(pa, pn, v, time, event):
480 pa.last_event = event
481 pa.last_chat_time = None
482 pa.last_chat_chan = None
491 age = now - pa.pi_fetched
492 guide = random.randint(120,240)
495 debug('PirateAboard refresh %d > %d %s' % (
496 age, guide, pa.name))
500 wait = fetcher.need_wait(now, imaginary)
502 debug('PirateAboard fetcher not ready %d' % wait)
504 pa.pi = PirateInfo(pa.name, 600)
508 class ChatLogTracker:
509 # This is quite complex so we make it opaque. Use the
510 # official invokers, accessors etc.
512 def __init__(self, myself_pi, logfn):
513 self._pl = {} # self._pl['Pirate'] =
514 self._vl = {} # self._vl['Vessel']['Pirate'] = PirateAboard
515 # self._vl['Vessel']['#lastinfo']
516 # self._vl['Vessel']['#name']
517 # self._v = self._vl[self._vessel]
519 self._myself = myself_pi
521 self._f = file(logfn)
522 flen = os.fstat(self._f.fileno()).st_size
524 if flen > max_backlog:
525 startpos = flen - max_backlog
526 self._f.seek(startpos)
528 self._progress = [0, flen - self._f.tell()]
529 self._disembark_myself()
530 self._need_redisplay = False
531 self._lastvessel = None
533 def _disembark_myself(self):
536 self.force_redisplay()
538 def force_redisplay(self):
539 self._need_redisplay = True
541 def _vessel_updated(self, v, timestamp):
543 v['#lastinfo'] = timestamp
544 self.force_redisplay()
546 def _onboard_event(self,v,timestamp,pirate,event,jobber=None):
547 pa = self._pl.get(pirate, None)
548 if pa is not None and pa.v is v:
549 pa.last_time = timestamp
550 pa.last_event = event
552 if pa is not None and pa.v is not None:
554 pa = PirateAboard(pirate, v, timestamp, event)
555 self._pl[pirate] = pa
556 if v is not None: v[pirate] = pa
559 if jobber is None: timeout = None
560 else: timeout = getattr(opts, 'timeout_'+jobber)
561 if timeout is None: pa.expires = None
562 else: pa.expires = timestamp + timeout
563 self._vessel_updated(v, timestamp)
566 def _expire_jobbers(self, now):
567 for pa in self._pl.values():
568 if pa.expires is None: continue
569 if pa.expires >= now: continue
571 del self._pl[pa.name]
572 if v is not None: del v[pa.name]
573 self.force_redisplay()
575 def _trash_vessel(self, v):
577 if pn.startswith('#'): continue
581 if v is self._v: self._disembark_myself()
582 self.force_redisplay()
584 def _vessel_stale(self, v, timestamp):
585 return timestamp - v['#lastinfo'] > opts.ship_reboard_clearout
587 def _vessel_check_expire(self, v, timestamp):
588 if not self._vessel_stale(v, timestamp):
590 self._debug_line_disposition(timestamp,'',
591 'stale-reset ' + v['#name'])
592 self._trash_vessel(v)
595 def expire_garbage(self, timestamp):
596 for v in self._vl.values():
597 self._vessel_check_expire(v, timestamp)
599 def _vessel_lookup(self, vn, timestamp, dml=[], create=False):
600 v = self._vl.get(vn, None)
602 v = self._vessel_check_expire(v, timestamp)
609 self._vl[vn] = v = { '#name': vn }
610 self._vessel_updated(v, timestamp)
613 def _find_matching_vessel(self, pattern, timestamp, cmdr,
614 dml=[], create=False):
615 # use when a commander pirate `cmdr' specified a vessel
616 # by name `pattern' (either may be None)
617 # if create is true, will create the vessel
618 # record if an exact name is specified
620 if (pattern is not None and
622 and len(pattern.split(' ')) == 2):
625 return self._vessel_lookup(
626 vn, timestamp, dml=dml, create=create)
629 pattern_check = lambda vn: True
631 re = '(?:.* )?%s$' % pattern.lower().replace('*','.+')
632 pattern_check = regexp.compile(re, regexp.I).match
636 cmdr_pa = self._pl.get(cmdr, None)
637 if cmdr_pa: tries.append((cmdr_pa.v, 'cmdr'))
639 tries.append((self._v, 'here'))
642 for (v, dm) in tries:
643 if v is None: dml.append(dm+'?'); continue
646 if not pattern_check(vn):
654 if pattern is not None and '*' in pattern:
657 for (vn,v) in self._vl.iteritems()
658 if not self._vessel_stale(v, timestamp)
661 #debug('CLT-RE /%s/ wanted (%s) searched (%s)' % (
663 # '/'.join(tried_vns),
664 # '/'.join([vn for (vn,v) in search])))
674 def _debug_line_disposition(self,timestamp,l,m):
675 debug('CLT %13s %-40s %s' % (timestamp,m,l))
677 def _rm_crew_l(self,re,l):
678 m = regexp.match(re,l)
679 if m and m.group(2) == self._myself.crew[1]:
684 def chatline(self,l):
685 rm = lambda re: regexp.match(re,l)
686 d = lambda m: self._debug_line_disposition(timestamp,l,m)
687 rm_crew = lambda re: self._rm_crew_l(re,l)
690 m = rm('=+ (\\d+)/(\\d+)/(\\d+) =+$')
692 self._date = [int(x) for x in m.groups()]
693 self._previous_timestamp = None
694 return d('date '+`self._date`)
696 if self._date is None:
697 return d('date unset')
699 m = rm('\\[(\d\d):(\d\d):(\d\d)\\] ')
701 return d('no timestamp')
704 time_tuple = (self._date +
705 [int(x) for x in m.groups()] +
707 timestamp = time.mktime(time_tuple)
708 if timestamp >= self._previous_timestamp: break
710 self._debug_line_disposition(timestamp,'',
711 'new date '+`self._date`)
713 self._previous_timestamp = timestamp
715 l = l[l.find(' ')+1:]
717 def ob_x(pirate,event):
718 return self._onboard_event(
719 self._v, timestamp, pirate, event)
720 def ob1(did): ob_x(m.group(1), did); return d(did)
721 def oba(did): return ob1('%s %s' % (did, m.group(2)))
723 def jb(pirate,jobber):
724 return self._onboard_event(
725 None, timestamp, pirate,
726 ("jobber %s" % jobber),
730 def disembark(v, timestamp, pirate, event):
732 v, timestamp, pirate, 'leaving '+event)
736 def disembark_me(why):
737 self._disembark_myself()
738 return d('disembark-me '+why)
740 m = rm('Going aboard the (\\S.*\\S)\\.\\.\\.$')
743 pn = self._myself.name
745 v = self._vessel_lookup(vn, timestamp, dm, create=True)
746 self._lastvessel = self._vessel = vn
748 ob_x(pn, 'we boarded')
749 self.expire_garbage(timestamp)
750 return d(' '.join(dm))
753 return d('no vessel')
755 m = rm('(\\w+) has come aboard\\.$')
756 if m: return ob1('boarded');
758 m = rm('You have ordered (\\w+) to do some (\\S.*\\S)\\.$')
760 (who,what) = m.groups()
761 pa = ob_x(who,'ord '+what)
762 if what == 'Gunning':
764 return d('duty order')
766 m = rm('(\\w+) abandoned a (\\S.*\\S) station\\.$')
767 if m: oba('stopped'); return d("end")
769 def chat_core(speaker, chan):
770 try: pa = self._pl[speaker]
771 except KeyError: return 'mystery'
772 if pa.v is not None and pa.v is not self._v:
774 pa.last_chat_time = timestamp
775 pa.last_chat_chan = chan
776 self.force_redisplay()
781 dm = chat_core(speaker, chan)
782 return d('chat %s %s' % (chan, dm))
784 def chat_metacmd(chan):
785 (cmdr, metacmd) = m.groups()
786 metacmd = regexp.sub('\\s+', ' ', metacmd).strip()
788 '/([adj]) (?:([A-Za-z* ]+)\\s*:)?([A-Za-z ]+)$',
790 if not m2: return chat(chan)
792 (cmd, pattern, targets) = m2.groups()
793 dml = ['cmd', chan, cmd]
795 if cmd == 'a': each = self._onboard_event
796 elif cmd == 'd': each = disembark
797 else: each = lambda *l: self._onboard_event(*l,
798 **{'jobber':'applied'})
800 if cmdr == self._myself.name:
802 how = 'cmd: %s' % cmd
805 how = 'cmd: %s %s' % (cmd,cmdr)
808 if pattern is not None:
812 v = self._find_matching_vessel(
813 pattern, timestamp, cmdr,
816 if cmd == 'j' or v is not None:
817 targets = targets.strip().split(' ')
818 dml.append(`len(targets)`)
819 for target in targets:
820 each(v, timestamp, target.title(), how)
821 self._vessel_updated(v, timestamp)
824 chat_core(cmdr, 'cmd '+chan)
827 m = rm('(\\w+) (?:issued an order|ordered everyone) "')
828 if m: return ob1('general order');
830 m = rm('(\\w+) says, "')
831 if m: return chat('public')
833 m = rm('(\\w+) tells ye, "')
834 if m: return chat('private')
836 m = rm('Ye told (\\w+), "(.*)"$')
837 if m: return chat_metacmd('private')
839 m = rm('(\\w+) flag officer chats, "')
840 if m: return chat('flag officer')
842 m = rm('(\\w+) officer chats, "(.*)"$')
843 if m: return chat_metacmd('officer')
845 m = rm('Ye accepted the offer to job with ')
846 if m: return disembark_me('jobbing')
848 m = rm('Ye hop on the ferry and are whisked away ')
849 if m: return disembark_me('ferry')
851 m = rm('Whisking away to yer home on the magical winds')
852 if m: return disembark_me('home')
854 m = rm('Game over\\. Winners: ([A-Za-z, ]+)\\.$')
856 pl = m.group(1).split(', ')
857 if not self._myself.name in pl:
858 return d('lost melee')
860 if ' ' in pn: continue
862 return d('won melee')
864 m = rm('(\\w+) is eliminated\\!')
865 if m: return ob1('eliminated in fray');
867 m = rm('(\\w+) has driven \w+ from the ship\\!')
868 if m: return ob1('boarder repelled');
870 m = rm('\w+ has bested (\\w+), and turns'+
871 ' to the rest of the ship\\.')
872 if m: return ob1('boarder unrepelled');
874 pirate = rm_crew("(\\w+) has taken a job with '(.*)'\\.")
875 if pirate: return jb(pirate, 'ashore')
877 pirate = rm_crew("(\\w+) has left '(.*)'\\.")
879 disembark(self._v, timestamp, pirate, 'left crew')
880 return d('left crew')
882 m = rm('(\w+) has applied for the posted job\.')
883 if m: return jb(m.group(1), 'applied')
885 pirate= rm_crew("(\\w+) has been invited to job for '(.*)'\\.")
886 if pirate: return jb(pirate, 'invited')
888 pirate = rm_crew("(\\w+) declined the job offer for '(.*)'\\.")
889 if pirate: return jb(pirate, 'declined')
891 m = rm('(\\w+) has left the vessel\.')
894 disembark(self._v, timestamp, pirate, 'disembarked')
895 return d('disembarked')
897 return d('not-matched')
899 def _str_pa(self, pn, pa):
900 assert self._pl[pn] == pa
901 s = ' '*20 + "%s %-*s %13s %-30s %13s %-20s %13s" % (
902 (' ','G')[pa.gunner],
903 max_pirate_namelen, pn,
904 pa.last_time, pa.last_event,
905 pa.last_chat_time, pa.last_chat_chan,
907 if pa.expires is not None:
908 s += " %-5d" % (pa.expires - pa.last_time)
912 def _str_vessel(self, vn, v):
913 s = ' vessel %s\n' % vn
914 s += ' '*20 + "%-*s %13s\n" % (
915 max_pirate_namelen, '#lastinfo',
917 assert v['#name'] == vn
918 for pn in sorted(v.keys()):
919 if pn.startswith('#'): continue
922 s += self._str_pa(pn,pa)
926 s = '''<ChatLogTracker
929 ''' % (self._myself.name, self._vessel)
930 assert ((self._v is None and self._vessel is None) or
931 (self._v is self._vl[self._vessel]))
932 if self._vessel is not None:
933 s += self._str_vessel(self._vessel, self._v)
934 for vn in sorted(self._vl.keys()):
935 if vn == self._vessel: continue
936 s += self._str_vessel(vn, self._vl[vn])
942 assert pa.v in self._vl.values()
944 s += self._str_pa(pa.name, pa)
948 def catchup(self, progress=None):
950 more = self._f.readline()
953 self._progress[0] += len(more)
954 if progress: progress.progress(*self._progress)
957 if self._lbuf.endswith('\n'):
958 self.chatline(self._lbuf.rstrip())
961 debug(self.__str__())
962 self._expire_jobbers(time.time())
964 if progress: progress.caughtup()
967 rv = self._need_redisplay
968 self._need_redisplay = False
971 # returns our pirate name
972 return self._myself.name
973 def vesselname(self):
974 # returns the vessel name we're aboard or None
976 def lastvesselname(self):
977 # returns the last vessel name we were aboard or None
978 return self._lastvessel
979 def aboard(self, vesselname=True):
980 # returns a list of PirateAboard the vessel
981 # sorted by pirate name
982 # you can pass this None and you'll get []
983 # or True for the current vessel (which is the default)
984 # the returned value is a fresh list of persistent
985 # PirateAboard objects
986 if vesselname is True: v = self._v
987 else: v = self._vl.get(vesselname.title())
988 if v is None: return []
990 for pn in sorted(v.keys())
991 if not pn.startswith('#') ]
993 # returns a the jobbers' PirateAboards,
994 # sorted by jobber class and reverse of expiry time
996 for pa in self._pl.values()
997 if pa.jobber is not None
1000 return (pa.jobber, -pa.expires)
1001 l.sort(key = compar_key)
1004 #---------- implementations of actual operation modes ----------
1006 def do_pirate(pirates, bu):
1008 for pirate in pirates:
1009 info = PirateInfo(pirate)
1010 print '%s: %s,' % (`pirate`, info)
1013 def prep_crew_of(args, bu, max_age=300):
1014 if len(args) != 1: bu('crew-of takes one pirate name')
1015 pi = PirateInfo(args[0], max_age)
1016 if pi.crew is None: return None
1017 return CrewInfo(pi.crew[0], max_age)
1019 def do_crew_of(args, bu):
1020 ci = prep_crew_of(args, bu)
1023 def do_standings_crew_of(args, bu):
1024 ci = prep_crew_of(args, bu, 60)
1025 tab = StandingsTable(sys.stdout)
1027 for (rank, members) in ci.crew:
1028 if not members: continue
1030 tab.literalline('%s:' % rank)
1032 pi = PirateInfo(p, random.randint(900,1800))
1035 class ProgressPrintPercentage:
1036 def __init__(self, f=sys.stdout):
1038 def progress_string(self,done,total):
1039 return "scan chat logs %3d%%\r" % ((done*100) / total)
1040 def progress(self,*a):
1041 self._f.write(self.progress_string(*a))
1043 def show_init(self, pirate, ocean):
1044 print >>self._f, 'Starting up, %s on the %s ocean' % (
1047 self._f.write(' \r')
1050 #----- modes which use the chat log parser are quite complex -----
1052 def prep_chat_log(args, bu,
1053 progress=ProgressPrintPercentage(),
1054 max_myself_age=3600):
1055 if len(args) != 1: bu('this action takes only chat log filename')
1057 logfn_re = '(?:.*/)?([A-Z][a-z]+)_([a-z]+)_'
1058 match = regexp.match(logfn_re, logfn)
1059 if not match: bu('chat log filename is not in expected format')
1060 (pirate, ocean) = match.groups()
1061 fetcher.default_ocean(ocean)
1063 progress.show_init(pirate, fetcher.ocean)
1064 myself = PirateInfo(pirate,max_myself_age)
1065 track = ChatLogTracker(myself, logfn)
1068 track.catchup(progress)
1071 track.force_redisplay()
1073 return (myself, track)
1075 def do_track_chat_log(args, bu):
1076 (myself, track) = prep_chat_log(args, bu)
1081 sleep(0.5 + 0.5 * random.random())
1083 #----- ship management aid -----
1085 class Display_dumb(ProgressPrintPercentage):
1087 ProgressPrintPercentage.__init__(self)
1090 def realstart(self):
1093 class Display_overwrite(ProgressPrintPercentage):
1095 ProgressPrintPercentage.__init__(self)
1097 null = file('/dev/null','w')
1098 curses.setupterm(fd=null.fileno())
1100 self._clear = curses.tigetstr('clear')
1102 self._debug('missing clear!')
1103 self.show = Display_dumb.show
1106 self._t = {'el':'', 'ed':''}
1107 if not self._init_sophisticated():
1108 for k in self._t.keys(): self._t[k] = ''
1109 self._t['ho'] = self._clear
1111 def _debug(self,m): debug('display overwrite: '+m)
1113 def _init_sophisticated(self):
1114 for k in self._t.keys():
1115 s = curses.tigetstr(k)
1117 self._t['ho'] = curses.tigetstr('ho')
1118 if not self._t['ho']:
1119 cup = curses.tigetstr('cup')
1120 self._t['ho'] = curses.tparm(cup,0,0)
1121 missing = [k for k in self._t.keys() if not self._t[k]]
1123 self.debug('missing '+(' '.join(missing)))
1128 w = sys.stdout.write
1129 def wti(k): w(self._t[k])
1133 for l in s.rstrip().split('\n'):
1142 def realstart(self):
1143 sys.stdout.write(self._clear)
1147 def do_ship_aid(args, bu):
1148 if opts.ship_duty is None: opts.ship_duty = True
1150 displayer = globals()['Display_'+opts.display]()
1152 (myself, track) = prep_chat_log(args, bu, progress=displayer)
1154 displayer.realstart()
1156 if os.isatty(0): kr_create = KeystrokeReader
1157 else: kr_create = DummyKeystrokeReader
1160 kreader = kr_create(0, 10)
1161 ship_aid_core(myself, track, displayer, kreader)
1166 class KeyBasedSorter:
1167 def compar_key_pa(self, pa):
1168 pi = pa.pirate_info()
1169 if pi is None: return None
1170 return self.compar_key(pi)
1171 def lsort_pa(self, l):
1172 l.sort(key = self.compar_key_pa)
1174 class NameSorter(KeyBasedSorter):
1175 def compar_key(self, pi): return pi.name
1176 def desc(self): return 'name'
1178 class SkillSorter(NameSorter):
1179 def __init__(self, relevant):
1180 self._want = frozenset(relevant.split('/'))
1182 for p in core_duty_puzzles:
1183 if isinstance(p,basestring): self._avoid.add(p)
1184 else: self._avoid |= set(p)
1185 self._avoid -= self._want
1186 self._desc = '%s' % relevant
1188 def desc(self): return self._desc
1190 def compar_key(self, pi):
1192 pi.standings.get(puz,-1)
1193 for puz in self._want
1196 -pi.standings.get(puz,standing_limit)
1197 for puz in self._avoid
1200 def negate(x): return -x
1201 debug('compar_key %s bw=%s ba=%s' % (pi.name, `best_want`,
1203 return (-best_want, map(negate, best_avoid), pi.name)
1205 def ship_aid_core(myself, track, displayer, kreader):
1208 vn = track.vesselname()
1209 if vn: return (vn, " on board the %s" % vn)
1210 vn = track.lastvesselname()
1211 if vn: return (vn, " ashore from the %s" % vn)
1212 return (None, " not on a vessel")
1215 if t is None: return ' ' * 22
1216 return " %-4s %-16s" % (format_time_interval(now - t),e)
1218 displayer.show(track.myname() + find_vessel()[1] + '...')
1228 (vn, s) = find_vessel()
1229 s = track.myname() + s
1230 s += " at %s" % time.strftime("%Y-%m-%d %H:%M:%S")
1235 tbl = StandingsTable(tbl_s)
1237 aboard = track.aboard(vn)
1238 sort.lsort_pa(aboard)
1240 jobbers = track.jobbers()
1242 if track.vesselname(): howmany = 'aboard: %2d' % len(aboard)
1245 tbl.headings(howmany, ' sorted by '+sort.desc())
1249 for pa in aboard + jobbers:
1250 if pa.jobber != last_jobber:
1251 last_jobber = pa.jobber
1253 tbl.literalline('jobbers '+last_jobber)
1255 pi = pa.pirate_info()
1258 if pa.gunner: xs += 'G '
1260 xs += timeevent(pa.last_time, pa.last_event)
1261 xs += timeevent(pa.last_chat_time, pa.last_chat_chan)
1264 tbl.pirate_dummy(pa.name, rotate_nya[0], xs)
1268 s += tbl_s.getvalue()
1274 rotate_nya = rotate_nya[1:3] + rotate_nya[0]
1278 elif k == 'g': sort = SkillSorter('Gunning')
1279 elif k == 'c': sort = SkillSorter('Carpentry')
1280 elif k == 's': sort = SkillSorter('Sailing/Rigging')
1281 elif k == 'b': sort = SkillSorter('Bilging')
1282 elif k == 'n': sort = SkillSorter('Navigating')
1283 elif k == 'd': sort = SkillSorter('Battle Navigation')
1284 elif k == 't': sort = SkillSorter('Treasure Haul')
1285 elif k == 'a': sort = NameSorter()
1286 else: pass # unknown key command
1288 #---------- individual keystroke input ----------
1290 class DummyKeystrokeReader:
1291 def __init__(self,fd,timeout_dummy): pass
1292 def stop(self): pass
1293 def getch(self): sleep(1); return None
1294 def info(self): return ' [noninteractive]'
1296 class KeystrokeReader(DummyKeystrokeReader):
1297 def __init__(self, fd, timeout_decisec=0):
1299 self._saved = termios.tcgetattr(fd)
1300 a = termios.tcgetattr(fd)
1301 a[3] &= ~(termios.ECHO | termios.ECHONL |
1302 termios.ICANON | termios.IEXTEN)
1303 a[6][termios.VMIN] = 0
1304 a[6][termios.VTIME] = timeout_decisec
1305 termios.tcsetattr(fd, termios.TCSANOW, a)
1307 termios.tcsetattr(self._fd, termios.TCSANOW, self._saved)
1310 byte = os.read(self._fd, 1)
1311 if not len(byte): return None
1316 #---------- main program ----------
1319 global opts, fetcher
1322 '''usage: .../yoweb-scrape [OPTION...] ACTION [ARGS...]
1324 yoweb-scrape [--ocean OCEAN ...] pirate PIRATE
1325 yoweb-scrape [--ocean OCEAN ...] crew-of PIRATE
1326 yoweb-scrape [--ocean OCEAN ...] standings-crew-of PIRATE
1327 yoweb-scrape [--ocean OCEAN ...] track-chat-log CHAT-LOG
1328 yoweb-scrape [options] ship-aid CHAT-LOG (must be .../PIRATE_OCEAN_chat-log*)
1330 display modes (for --display) apply to ship-aid:
1331 --display=dumb just print new information, scrolling the screen
1332 --display=overwrite use cursor motion, selective clear, etc. to redraw at top''')
1334 ao('-O','--ocean',dest='ocean', metavar='OCEAN', default=None,
1335 help='select ocean OCEAN')
1336 ao('--cache-dir', dest='cache_dir', metavar='DIR',
1337 default='~/.yoweb-scrape-cache',
1338 help='cache yoweb pages in DIR')
1339 ao('-D','--debug', action='count', dest='debug', default=0,
1340 help='enable debugging output')
1341 ao('--debug-fd', type='int', dest='debug_fd',
1342 help='write any debugging output to specified fd')
1343 ao('-q','--quiet', action='store_true', dest='quiet',
1344 help='suppress warning output')
1345 ao('--display', action='store', dest='display',
1346 type='choice', choices=['dumb','overwrite'],
1347 help='how to display ship aid')
1349 ao_jt = lambda wh, t: ao(
1350 '--timeout-sa-'+wh, action='store', dest='timeout_'+wh,
1351 default=t, help=('set timeout for expiring %s jobbers' % wh))
1352 ao_jt('applied', 120)
1353 ao_jt('invited', 120)
1354 ao_jt('declined', 30)
1355 ao_jt('ashore', 1800)
1357 ao('--ship-duty', action='store_true', dest='ship_duty',
1358 help='show ship duty station puzzles')
1359 ao('--all-puzzles', action='store_false', dest='ship_duty',
1360 help='show all puzzles, not just ship duty stations')
1362 ao('--min-cache-reuse', type='int', dest='min_max_age',
1363 metavar='SECONDS', default=60,
1364 help='always reuse cache yoweb data if no older than this')
1366 (opts,args) = pa.parse_args()
1370 print >>sys.stderr, copyright_info
1371 pa.error('need a mode argument')
1373 if opts.debug_fd is not None:
1374 opts.debug_file = os.fdopen(opts.debug_fd, 'w')
1376 opts.debug_file = sys.stdout
1379 mode_fn_name = 'do_' + mode.replace('_','#').replace('-','_')
1380 try: mode_fn = globals()[mode_fn_name]
1381 except KeyError: pa.error('unknown mode "%s"' % mode)
1384 opts.expire_age = max(3600, opts.min_max_age)
1386 opts.ship_reboard_clearout = 3600
1388 if opts.cache_dir.startswith('~/'):
1389 opts.cache_dir = os.getenv('HOME') + opts.cache_dir[1:]
1391 if opts.display is None:
1392 if ((opts.debug > 0 and opts.debug_fd is None)
1393 or not os.isatty(sys.stdout.fileno())):
1394 opts.display = 'dumb'
1396 opts.display = 'overwrite'
1398 fetcher = Fetcher(opts.ocean, opts.cache_dir)
1400 mode_fn(args[1:], pa.error)