chiark / gitweb /
Insert some blank lines to improve readability, including every 5 pirates in ship-aid
[ypp-sc-tools.db-live.git] / yoweb-scrape
index ed9533ef51c513b4628673712880bc9a0b0b1e3c..40cdad56c80dbea539ea2c4cc86c01828d35a2c1 100755 (executable)
@@ -89,8 +89,9 @@ class Fetcher:
                        ages.append(age)
                return ages
 
-       def need_wait(self, now):
+       def need_wait(self, now, imaginary=[]):
                ages = self._cache_scan(now)
+               ages += imaginary
                ages.sort()
                debug('Fetcher   ages ' + `ages`)
                min_age = 1
@@ -247,7 +248,7 @@ u'\\s*\\S*/([-A-Za-z]+)\\s*$|\\s*\\S*/\\S*\\s*\\(ocean\\-wide(?:\\s|\\xa0)+([-A-
                                skl.msg('puzzle "%s" no standing found' % puzzle)
                                continue
                        standing = sl[0]
-                       for i in range(0, len(standingvals)-1):
+                       for i in range(0, len(standingvals)):
                                if standing == standingvals[i]:
                                        self.standings[puzzle] = i
                        if not puzzle in self.standings:
@@ -331,7 +332,7 @@ class CrewInfo(SomethingSoupInfo):
 #---------- pretty-printer for tables of pirate puzzle standings ----------
 
 class StandingsTable:
-       def __init__(self, use_puzzles=None, col_width=6):
+       def __init__(self, use_puzzles=None, col_width=6, gap_every=5):
                if use_puzzles is None:
                        if opts.ship_duty:
                                use_puzzles=[
@@ -347,14 +348,21 @@ class StandingsTable:
                self._puzzles = use_puzzles
                self.s = ''
                self._cw = col_width-1
+               self._gap_every = gap_every
+               self._linecount = 0
 
        def _pline(self, pirate, puzstrs, extra):
+               if (self._linecount > 0
+                   and self._gap_every is not None
+                   and not (self._linecount % self._gap_every)):
+                       self.s += '\n'
                self.s += ' %-*s' % (max(max_pirate_namelen, 14), pirate)
                for v in puzstrs:
                        self.s += ' %-*.*s' % (self._cw,self._cw, v)
                if extra:
                        self.s += ' ' + extra
                self.s += '\n'
+               self._linecount += 1
 
        def _puzstr(self, pi, puzzle):
                if not isinstance(puzzle,list): puzzle = [puzzle]
@@ -381,9 +389,12 @@ class StandingsTable:
                        spc = name.find(' ')
                        if spc < 0: return name
                        return name[0:min(4,spc)] + name[spc+1:]
+               self._linecount = -2
                self._pline('', map(puzn_redact, self._puzzles), None)
+               self._linecount = 0
        def literalline(self, line):
                self.s += line + '\n'
+               self._linecount = 0
        def pirate_dummy(self, name, standingstring, extra=None):
                self._pline(name, standingstring * len(self._puzzles), extra)
        def pirate(self, pi, extra=None):
@@ -417,8 +428,23 @@ class PirateAboard:
                pa.pi = None
 
        def pirate_info(pa):
-               if not pa.pi and not fetcher.need_wait(time.time()):
-                       pa.pi = PirateInfo(pa.name, 3600)
+               now = time.time()
+               if pa.pi:
+                       age = now - pa.pi_fetched
+                       guide = random.randint(120,240)
+                       if age <= guide:
+                               return pa.pi
+                       debug('PirateAboard refresh %d > %d  %s' % (
+                               age, guide, pa.name))
+                       imaginary = [2,6]
+               else:
+                       imaginary = [1]
+               wait = fetcher.need_wait(now, imaginary)
+               if wait:
+                       debug('PirateAboard fetcher not ready %d' % wait)
+                       return pa.pi
+               pa.pi = PirateInfo(pa.name, 600)
+               pa.pi_fetched = now
                return pa.pi
 
 class ChatLogTracker:
@@ -521,7 +547,7 @@ class ChatLogTracker:
                if pattern is None:
                        pattern_check = lambda vn: True
                else:
-                       re = '(?:.* )?%s$' % pattern.lower().replace('*','.*')
+                       re = '(?:.* )?%s$' % pattern.lower().replace('*','.+')
                        pattern_check = regexp.compile(re, regexp.I).match
 
                tries = []
@@ -851,6 +877,7 @@ def do_standings_crew_of(args, bu):
        tab.headings()
        for (rank, members) in ci.crew:
                if not members: continue
+               tab.literalline('')
                tab.literalline('%s:' % rank)
                for p in members:
                        pi = PirateInfo(p, random.randint(900,1800))
@@ -884,9 +911,9 @@ def prep_chat_log(args, bu,
        if not match: bu('chat log filename is not in expected format')
        (pirate, ocean) = match.groups()
        fetcher.default_ocean(ocean)
-       
-       myself = PirateInfo(pirate,max_myself_age)
+
        progress.show_init(pirate, fetcher.ocean)
+       myself = PirateInfo(pirate,max_myself_age)
        track = ChatLogTracker(myself, logfn)
 
        opts.debug -= 2
@@ -983,30 +1010,29 @@ def do_ship_aid(args, bu):
 
        displayer.realstart()
 
-       while True:
-               track.catchup()
-               now = time.time()
-
-               s = "%s" % track.myname()
-
+       def find_vessel():
                vn = track.vesselname()
-               if vn is not None:
-                       s += " on board the %s" % vn
+               if vn: return (vn, " on board the %s" % vn)
+               vn = track.lastvesselname()
+               if vn: return (vn, " ashore from the %s" % vn)
+               return (None, " not on a vessel")
 
-               if vn is None:
-                       vn = track.lastvesselname()
-                       if vn is not None:
-                               s += " ashore from the %s" % vn
+       displayer.show(track.myname() + find_vessel()[1] + '...')
 
-               if vn is None:
-                       s += " not on a vessel"
+       while True:
+               track.catchup()
+               now = time.time()
 
+               (vn, s) = find_vessel()
+               s = track.myname() + s
                s += " at %s\n" % time.strftime("%Y-%m-%d %H:%M:%S")
 
                tbl = StandingsTable()
                tbl.headings()
 
-               for pa in track.aboard():
+               aboard = track.aboard(vn)
+
+               for pa in aboard:
                        pi = pa.pirate_info()
 
                        xs = ''
@@ -1065,6 +1091,10 @@ display modes (for --display) apply to ship-aid:
        ao('--all-puzzles', action='store_false', dest='ship_duty',
                help='show all puzzles, not just ship duty stations')
 
+       ao('--min-cache-reuse', type='int', dest='min_max_age',
+               metavar='SECONDS', default=60,
+               help='always reuse cache yoweb data if no older than this')
+
        (opts,args) = pa.parse_args()
        random.seed()
 
@@ -1082,8 +1112,8 @@ display modes (for --display) apply to ship-aid:
        except KeyError: pa.error('unknown mode "%s"' % mode)
 
        # fixed parameters
-       opts.min_max_age = 60
-       opts.expire_age = 3600
+       opts.expire_age = max(3600, opts.min_max_age)
+
        opts.ship_reboard_clearout = 3600
 
        if opts.cache_dir.startswith('~/'):