chiark / gitweb /
Reorg chat log to make ship aid nice
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sat, 16 May 2009 17:56:45 +0000 (18:56 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sat, 16 May 2009 17:56:45 +0000 (18:56 +0100)
yoweb-scrape

index 5b2fe9841f00a98c9320448a4b7f316813333e78..2d968921ab1eed4023fea7344ce90951f2c7c21d 100755 (executable)
@@ -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})
@@ -327,6 +331,7 @@ class StandingsTable:
                if not isinstance(puzzle,list): puzzle = [puzzle]
                try: standing = max([pi.standings[p] for p in puzzle])
                except KeyError: return '?'
+               if isinstance(standing,basestring): return standing
                if not standing: return ''
                s = ''
                if self._cw > 4:
@@ -393,13 +398,22 @@ class PirateAboard:
        # 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.pn = 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(self):
+               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):
@@ -426,7 +440,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
@@ -620,7 +634,7 @@ class ChatLogTracker:
                if progress: progress.caughtup()
 
 class ProgressPrintPercentage:
-       def __init__(self, f): self._f = f
+       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()
@@ -628,25 +642,33 @@ class ProgressPrintPercentage:
                self._f.write('                   \r')
                self._f.flush()
 
-def do_track_chat_log(args, bu):
-       if len(args) != 1: bu('ship-aid takes only chat log filename')
+def simple_printer(x): print x
+
+def run_chat_log(args, bu, progress=ProgressPrintPercentage(),
+               display=simple_printer, 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()
-
-       track = ChatLogTracker(pirate, logfn)
-       myself_pi = PirateInfo(pirate,3600)
+       
+       myself_pi = PirateInfo(pirate,max_myself_age)
        track = ChatLogTracker(myself_pi, logfn)
-       track.catchup(ProgressPrintPercentage(sys.stdout))
+
+       track.catchup(progress)
        while True:
                track.catchup()
                if track.need_redisplay:
-                       print track
+                       display(track)
                        track.need_redisplay = False
                time.sleep(1)
 
+def do_track_chat_log(args, bu):
+       run_chat_log(args, bu)
+
+#def do_ship_aid(args, bu):
+
 def main():
        global opts, fetcher
 
@@ -672,6 +694,8 @@ actions:
 
        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()