view results/CC-MAIN-2019-35/warc_lmhx/mcdb/idx/tcb.py @ 333:282a35927943 trim

begin rework to do merge in python
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Sun, 01 Mar 2026 13:40:52 +0000
parents 83450325421f
children 252cf1263ad0
line wrap: on
line source

'''For use as callbacks from cdx2tsv.py, e.g.
   uz cdx-00120.gz | fgrep '"lastmod":'| cdx2tsv.py -c tcb /tmp/hst/cby status lastmod
'''
import sys, os, bisect

class Year:
  def __init__(self, year, far_end):
    self.year = year
    self.far_end = far_end

def init():
  global i, year, count, cur_status, years, nyears, res_dir, ff, far_ends

  with open("%s/results/CC-MAIN-2019-35/warc_lmhx/years.txt"%os.getenv("HOME"),"r") \
        as yf:
    y = 1993
    years=[Year(y:=(y+1),int(l)) for l in yf]

  far_ends = [y.far_end for y in years]

  nyears = len(years)
  #res_dir = sys.argv.pop(1)
  years.append(Year("oob",0))

  #print('i',years, nyears, file = sys.stderr)

def finish():
  global ff, i, count, cur_status
  ff[i].write(b'%d\t%d\n'%(count, cur_status))

def find_year(lastmod):
  global far_ends
  return years[bisect.bisect_right(far_ends, lastmod)]

def cbf(status,lastmod):
  global i, year, count, cur_status, years, nyears, res_dir, ff
  stat = int(status)
  lm = int(lastmod)
  #print('c',i, year, cur_status, count, stat, lm, years[i], file = sys.stderr)
  if (not year == 0) and (lm > years[i]):
    # change of year
    if count != 0:
      ff[i].write(b'%d\t%d\n'%(count, cur_status))
    cur_status = stat
    count = 1
    while lm > years[i]:
      #print('ny',year,i,lm,years[i],count,file=sys.stderr)
      i += 1
      year += 1
      if i == nyears:
        year = 0
        return
    if not ff[i]:
      ff[i] = open("%s/%s"%(res_dir, year),"ba")
  elif stat == cur_status:
    count += 1
  else:
    #print('ns',count,i,year,ff[i],file=sys.stderr)
    ff[i].write(b'%d\t%d\n'%(count, cur_status))
    cur_status = stat
    count = 1