view results/CC-MAIN-2019-35/warc_lmhx/mcdb/idx/tcb.py @ 334:252cf1263ad0 trim

do merge in python working
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Sun, 01 Mar 2026 15:22:10 +0000
parents 282a35927943
children
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
from collections import Counter

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

  def write(self):
    global res_dir
    if self.stats:
      with open("%s/%s"%(res_dir, self.year),"bw") as f:
        for s,c in sorted(list(self.stats.items()),
                          key=lambda i:i[1], reverse=True):
          f.write(b'%d\t%d\n'%(c, s))

def init():
  global years, nyears, res_dir, far_ends, NLM

  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))

  years.append(NLM := Year("no_LM",0))

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

def finish():
  global years
  for y in years:
    y.write()
  

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

def cbf(status,lastmod):
  global NLM
  stat = int(status)
  if lastmod == 'NA':
    NLM.stats[stat] += 1
  else:
    lm = int(lastmod)
    find_year(lm).stats[stat]+=1