X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?a=blobdiff_plain;f=yoweb-scrape;h=1f5486e2a656f3c9fd2c8642213bde5b021d2dd4;hb=0e15228e6cb10cc9c6116435fb00d39652f1fc32;hp=2790ac7c7c66c27acb6ea3a8129c8a7b3ba3d65e;hpb=bd97786b00269408267fe97ccd3703fe28454dbe;p=ypp-sc-tools.web-test.git diff --git a/yoweb-scrape b/yoweb-scrape index 2790ac7..1f5486e 100755 --- a/yoweb-scrape +++ b/yoweb-scrape @@ -32,7 +32,7 @@ max_pirate_namelen = 12 def debug(m): - if opts.debug: + if opts.debug > 0: print m class Fetcher: @@ -70,7 +70,7 @@ class Fetcher: ages.append(age) return ages - def _rate_limit_cache_clean(self, now): + def need_wait(self, now): ages = self._cache_scan(now) ages.sort() debug('Fetcher ages ' + `ages`) @@ -83,6 +83,10 @@ class Fetcher: need_wait = max(need_wait, min_age - age) min_age += 3 min_age *= 1.25 + return need_wait + + def _rate_limit_cache_clean(self, now): + need_wait = self.need_wait(now) if need_wait > 0: debug('Fetcher wait %d' % need_wait) time.sleep(need_wait) @@ -280,7 +284,7 @@ class CrewInfo(SomethingSoupInfo): crew_rank_re = regexp.compile('/yoweb/images/crew') for row in tbl.contents: # findAll(recurse=False) - if isinstance(row, unicode): + if isinstance(row,basestring): continue is_rank = row.find('img', attrs={'src': crew_rank_re}) @@ -351,6 +355,8 @@ class StandingsTable: self._pline('', map(puzn_redact, self._puzzles)) def literalline(self, line): self.s += line + '\n' + def pirate_dummy(self, name, standingstring): + self._pline(name, standingstring * len(self._puzzles)) def pirate(self, pi): puzstrs = [self._puzstr(pi,puz) for puz in self._puzzles] self._pline(pi.name, puzstrs) @@ -388,18 +394,28 @@ def do_standings_crew_of(args, bu): class PirateAboard: # pa.v + # pa.name # pa.last_time # pa.last_event # pa.gunner # pa.last_chat_time # pa.last_chat_chan - def __init__(pa, v, time, event): + # pa.pi + + def __init__(pa, pn, v, time, event): + pa.name = pn pa.v = v pa.last_time = time pa.last_event = event pa.last_chat_time = None pa.last_chat_chan = None pa.gunner = False + pa.pi = None + + def pirate_info(pa): + if not pa.pi and not fetcher.need_wait(time.time()): + pa.pi = PirateInfo(pa.name, 3600) + return pa.pi class ChatLogTracker: def __init__(self, myself_pi, logfn): @@ -410,13 +426,13 @@ class ChatLogTracker: self._vessel = None # self._vl[self._vessel] self._date = None self._myself = myself_pi - self.need_redisplay = False + self._need_redisplay = False self._f = file(logfn) self._lbuf = '' self._progress = [0, os.fstat(self._f.fileno()).st_size] def _refresh(self): - self.need_redisplay = True + self._need_redisplay = True def _onboard_event(self,timestamp,pirate,event): try: pa = self._pl[pirate] @@ -426,7 +442,7 @@ class ChatLogTracker: pa.last_event = event else: if pa is not None: del pa.v[pirate] - pa = PirateAboard(self._v, timestamp, event) + pa = PirateAboard(pirate, self._v, timestamp, event) self._pl[pirate] = pa self._v[pirate] = pa self._v['#lastaboard'] = timestamp @@ -608,44 +624,101 @@ class ChatLogTracker: def catchup(self, progress=None): while True: more = self._f.readline() - if not more: return + if not more: break self._progress[0] += len(more) - if progress is not None: - progress(*self._progress) + if progress: progress.progress(*self._progress) self._lbuf += more if self._lbuf.endswith('\n'): self.chatline(self._lbuf.rstrip()) self._lbuf = '' - -def do_track_chat_log(args, bu): - if len(args) != 1: bu('ship-aid takes only chat log filename') + if progress: progress.caughtup() + + def changed(self): + rv = self._need_redisplay + self._need_redisplay = False + return rv + def myname(self): + # returns our pirate name + return self._myself.name + def vessel(self): + # returns the vessel we're aboard or None + return self._vessel + def aboard(self): + # returns a list of PirateAboard sorted by name + return [ self._v[pn] + for pn in sorted(self._v.keys()) + if not pn.startswith('#') ] + +class ProgressPrintPercentage: + def __init__(self, f=sys.stdout): self._f = f + def progress(self,done,total): + self._f.write("scan chat logs %3d%%\r" % ((done*100) / total)) + self._f.flush() + def caughtup(self): + self._f.write(' \r') + self._f.flush() + +def prep_chat_log(args, bu, + progress=ProgressPrintPercentage(), + max_myself_age=3600): + if len(args) != 1: bu('this action takes only chat log filename') logfn = args[0] logfn_re = '(?:.*/)?([A-Z][a-z]+)_([a-z]+)_chat-log-\\w+$' match = regexp.match(logfn_re, logfn) - if not match: bu('ship-aid chat log filename is not in default format') + if not match: bu('chat log filename is not in default format') (pirate, fetcher.ocean) = match.groups() + + myself = PirateInfo(pirate,max_myself_age) + track = ChatLogTracker(myself, logfn) - track = ChatLogTracker(pirate, logfn) - myself_pi = PirateInfo(pirate,3600) - track = ChatLogTracker(myself_pi, logfn) - - def pprogress(done,total): - sys.stdout.write("scan chat logs %3d%%\r" % - ((done*100) / total)) - sys.stdout.flush() + opts.debug -= 1 + track.catchup(progress) + opts.debug += 1 - track.catchup(pprogress) - sys.stdout.write(' \r') + return (myself, track) +def do_track_chat_log(args, bu): + (myself, track) = prep_chat_log(args, bu) while True: track.catchup() - if track.need_redisplay: + if track.changed(): print track - track.need_redisplay = False time.sleep(1) +def do_ship_aid(args, bu): + if opts.ship_duty is None: opts.ship_duty = True + + (myself, track) = prep_chat_log(args, bu) + + rotate_nya = '/-\\' + + while True: + track.catchup() + now = time.time() + sys.stdout.write("\n\n%s" % track.myname()) + + vn = track.vessel() + if vn is None: print " ...?"; return + + print " on board the %s at %s\n" % ( + vn, time.strftime("%Y-%m-%d %H:%M:%S")) + + tbl = StandingsTable() + tbl.headings() + + for pa in track.aboard(): + pi = pa.pirate_info() + if pi is None: + tbl.pirate_dummy(pa.name, rotate_nya[0]) + else: + tbl.pirate(pi) + + print tbl.results() + time.sleep(1) + rotate_nya = rotate_nya[1:2] + rotate_nya[0] + def main(): global opts, fetcher @@ -664,13 +737,15 @@ actions: ao('--cache-dir', dest='cache_dir', metavar='DIR', default='~/.yoweb-scrape-cache', help='cache yoweb pages in DIR') - ao('-D','--debug', action='store_true', dest='debug', default=False, + ao('-D','--debug', action='count', dest='debug', default=0, help='enable debugging output') ao('-q','--quiet', action='store_true', dest='quiet', help='suppress warning output') ao('--ship-duty', action='store_true', dest='ship_duty', help='show ship duty station puzzles') + ao('--all-puzzles', action='store_false', dest='ship_duty', + help='show all puzzles, not just ship duty stations') (opts,args) = pa.parse_args() random.seed()