X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?a=blobdiff_plain;f=yoweb-scrape;h=2d968921ab1eed4023fea7344ce90951f2c7c21d;hb=31cc3a65d81a3b9edd09372950ffbf3da5ab283b;hp=f66dda1f999f434d9c43d728cba07bd77b9967b4;hpb=ca2f709bb8508768d4cfa91c700e329d25a777d6;p=ypp-sc-tools.db-live.git diff --git a/yoweb-scrape b/yoweb-scrape index f66dda1..2d96892 100755 --- a/yoweb-scrape +++ b/yoweb-scrape @@ -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,16 +398,25 @@ 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): + def __init__(self, myself_pi, logfn): self._pl = {} # self._pl['Pirate'] = self._vl = {} # self._vl['Vessel']['Pirate'] = PirateAboard # self._vl['Vessel']['#lastaboard'] @@ -411,6 +425,9 @@ class ChatLogTracker: self._date = None self._myself = myself_pi 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 @@ -423,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 @@ -602,30 +619,56 @@ class ChatLogTracker: s += '>\n' return s -def do_ship_aid(args, bu): - if len(args) != 1: bu('ship-aid takes only chat log filename') + def catchup(self, progress=None): + while True: + more = self._f.readline() + if not more: break + + self._progress[0] += len(more) + if progress: progress.progress(*self._progress) + + self._lbuf += more + if self._lbuf.endswith('\n'): + self.chatline(self._lbuf.rstrip()) + self._lbuf = '' + if progress: progress.caughtup() + +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 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() - myself_pi = PirateInfo(pirate,3600) - track = ChatLogTracker(myself_pi) - f = file(logfn) - l = '' + + myself_pi = PirateInfo(pirate,max_myself_age) + track = ChatLogTracker(myself_pi, logfn) + + track.catchup(progress) while True: - l += f.readline() - if l.endswith('\n'): - track.chatline(l.rstrip()) - l = '' - continue - if l: - continue + 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 @@ -635,6 +678,7 @@ actions: yoweb-scrape [--ocean OCEAN ...] pirate PIRATE yoweb-scrape [--ocean OCEAN ...] crew-of PIRATE yoweb-scrape [--ocean OCEAN ...] standings-crew-of PIRATE + yoweb-scrape [--ocean OCEAN ...] track-chat-log CHAT-LOG yoweb-scrape [--ocean OCEAN ...] ship-aid CHAT-LOG ''') ao = pa.add_option @@ -650,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()