view workers/bin/_timedWhich.py @ 44:1342f6669352

knock off a few more relatively common cases
author Henry S. Thompson <ht@markup.co.uk>
date Sat, 01 Dec 2018 12:13:34 +0000
parents 1d776e96c16a
children 21152d241e1a
line wrap: on
line source

#!/usr/bin/env python3
import re,sys,io

uin=io.TextIOWrapper(sys.stdin.buffer,encoding='latin1')
p1=re.compile('"WARC-Target-URI":"(https?):.*msgtype=response')
p2=re.compile('"Last-Modified":"([^"]*)"')
sep=re.compile('\.?[, \t]+')
losers=re.compile('(mon|fri|sun)(day)?|tue(sday)?|wed(nesday)?|thu(rsday)?|sat(urday)?|gmt([+-][\d:]+)?|[ap]m|\d\d?:\d\d:(\d\d(\.\d*)?\w*|rd)|\{ts|[-+]\d\d\d\d|\d\d?|:',re.I)
oddlast=re.compile('\d\w+[A-Z]{3,4}|[A-Z]\w+/[A-Z]\w+')
HTTP=0
HTTPS=1
tab=[{},{}]
nd=[0,0] # no date
sn=['http','https']
for l in uin:
  m=p1.search(l)
  if m:
    k=HTTP if m.group(1)=='http' else HTTPS
    m=p2.search(l,m.end())
    if m is None:
      nd[k]+=1
    else:
      t=tab[k]
      lm=m.group(1)
      lmc=sep.split(lm)
      if len(lmc)==1 and lmc[0].startswith('serve-proxy-cache:'):
        r='serve-proxy-cache:'
      else:
        if oddlast.fullmatch(lmc[-1]):
          lmc.pop()
        r=' '.join(c for c in lmc if not losers.fullmatch(c))
      t[r]=t.get(r,0)+1
for h in (HTTP,HTTPS):
  print("%s\t\t%s"%(sn[h],nd[h]))
  for (k,v) in tab[h].items():
    print("%s\t%s\t%s"%(sn[h],k,v))