annotate bin/sort_date.py @ 105:9403c02d5034

switch to gzip -7 to get comparable compressed cdx block size
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Wed, 13 Sep 2023 16:48:43 +0100
parents fc9a045c872b
children 40c460fed99f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
73
e8c667bf8965 compute timestamps, key and sort lmh lines
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
1 #!/usr/bin/python3
104
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
2 '''Process output of lmh_warc [new 3-column version]
97
135a8c56dcc2 include full URI in output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 88
diff changeset
3 Usage: <(uz ....warc.gz | fgrep $'\t'|sed "/GMT$/s/\([^ ]\)GMT$/\1 GMT/")
135a8c56dcc2 include full URI in output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 88
diff changeset
4 '''
135a8c56dcc2 include full URI in output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 88
diff changeset
5
73
e8c667bf8965 compute timestamps, key and sort lmh lines
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
6 # Assumes you have used grep -v $'\t' on input for speed
e8c667bf8965 compute timestamps, key and sort lmh lines
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
7 # Recommended to also sed '/GMT$/s/\([^ ]\)GMT$/\1 GMT/'
e8c667bf8965 compute timestamps, key and sort lmh lines
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
8 # to fix a common 'bad' timestamp (~ .2% of inputs)
97
135a8c56dcc2 include full URI in output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 88
diff changeset
9
73
e8c667bf8965 compute timestamps, key and sort lmh lines
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
10 import email.utils
e8c667bf8965 compute timestamps, key and sort lmh lines
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
11 import sys
85
1daa8e444cfe work-around for weird handling of %-encoding in Java impl. of SURT
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 84
diff changeset
12 from urllib.parse import urlsplit, quote, unquote
104
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
13 import surt
85
1daa8e444cfe work-around for weird handling of %-encoding in Java impl. of SURT
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 84
diff changeset
14
1daa8e444cfe work-around for weird handling of %-encoding in Java impl. of SURT
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 84
diff changeset
15 import re, codecs
1daa8e444cfe work-around for weird handling of %-encoding in Java impl. of SURT
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 84
diff changeset
16
88
49faf679d7df final keystroke fixes, recurse and decimal www stripping
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 86
diff changeset
17 WPAT = re.compile('(,www\\d*)+\\)')
49faf679d7df final keystroke fixes, recurse and decimal www stripping
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 86
diff changeset
18
77
bf09a1d80d7b make CC's own sorting explicit
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 76
diff changeset
19 # Thanks to https://stackoverflow.com/a/8776871
bf09a1d80d7b make CC's own sorting explicit
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 76
diff changeset
20 import locale
bf09a1d80d7b make CC's own sorting explicit
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 76
diff changeset
21 from functools import cmp_to_key
76
eeef811f734d handle corner cases with final . and initial www..+
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 75
diff changeset
22
85
1daa8e444cfe work-around for weird handling of %-encoding in Java impl. of SURT
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 84
diff changeset
23 def percent_encode(ude):
1daa8e444cfe work-around for weird handling of %-encoding in Java impl. of SURT
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 84
diff changeset
24 #print(ude.object,ude.object[ude.start:ude.end])
1daa8e444cfe work-around for weird handling of %-encoding in Java impl. of SURT
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 84
diff changeset
25 return (''.join('%%%X'%c for c in ude.object[ude.start:ude.end]),
1daa8e444cfe work-around for weird handling of %-encoding in Java impl. of SURT
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 84
diff changeset
26 ude.end)
75
177f7df2bf46 handle %-encoded utf-8 as idna
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 73
diff changeset
27
85
1daa8e444cfe work-around for weird handling of %-encoding in Java impl. of SURT
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 84
diff changeset
28 codecs.register_error('percent',percent_encode)
1daa8e444cfe work-around for weird handling of %-encoding in Java impl. of SURT
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 84
diff changeset
29
86
3a2ae6057242 handle double .www, more keep-me chars
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 85
diff changeset
30 # From RFC-3986:
3a2ae6057242 handle double .www, more keep-me chars
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 85
diff changeset
31 # gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
3a2ae6057242 handle double .www, more keep-me chars
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 85
diff changeset
32 # sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
3a2ae6057242 handle double .www, more keep-me chars
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 85
diff changeset
33 # / "*" / "+" / "," / ";" / "="
3a2ae6057242 handle double .www, more keep-me chars
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 85
diff changeset
34 # But # _is_ escaped in Java surt results
3a2ae6057242 handle double .www, more keep-me chars
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 85
diff changeset
35 # and additionally " \ : < = > ? \ ^ _ ` { | } are not
3a2ae6057242 handle double .www, more keep-me chars
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 85
diff changeset
36
88
49faf679d7df final keystroke fixes, recurse and decimal www stripping
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 86
diff changeset
37 # Note also that although quote already does _not_ quote - . / _ ~
97
135a8c56dcc2 include full URI in output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 88
diff changeset
38 # they are included below as that's what we find in surt.surt 0.3.1
88
49faf679d7df final keystroke fixes, recurse and decimal www stripping
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 86
diff changeset
39
49faf679d7df final keystroke fixes, recurse and decimal www stripping
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 86
diff changeset
40 # Also, Java surt strips _all_ leading 'www\d*.',
86
3a2ae6057242 handle double .www, more keep-me chars
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 85
diff changeset
41 # where python3 surt only strips the first one.
3a2ae6057242 handle double .www, more keep-me chars
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 85
diff changeset
42
104
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
43 # And Java strips so-called option session-ids, but python doesn't
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
44
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
45 import surt.DefaultIAURLCanonicalizer, surt.GoogleURLCanonicalizer
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
46
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
47 def notDefaultCanon(hu,**options):
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
48 if surt.GoogleURLCanonicalizer.DECIMAL_IP.match(hu.host):
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
49 # Try to fix the incompatibility between Java and
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
50 # Python surt handling of 'octal' numbers in numeric IPv4 addresses
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
51 # and it should! See "After this line:
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
52 #
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
53 # 15,225,107,143)" in .../azure/notes.txt
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
54 try:
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
55 bytestrs = hu.host.split(b'.')
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
56 hu.host = b'.'.join(b'%d'%int(bs) for bs in bytestrs)
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
57 except ValueError:
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
58 pass
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
59 # Either we don't hit any, or Java doesn't do path_strip_session_id
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
60 options.setdefault('query_strip_session_id',False)
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
61 return surt.DefaultIAURLCanonicalizer.canonicalize(hu,
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
62 **options)
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
63
85
1daa8e444cfe work-around for weird handling of %-encoding in Java impl. of SURT
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 84
diff changeset
64 def cdx_key(uristring):
104
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
65 _surt = quote(unquote(surt.surt(uristring,
fc9a045c872b use my own Canonicalizer to fix more obscure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
66 canonicalizer=notDefaultCanon),
86
3a2ae6057242 handle double .www, more keep-me chars
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 85
diff changeset
67 errors='percent'),
88
49faf679d7df final keystroke fixes, recurse and decimal www stripping
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 86
diff changeset
68 safe='''!"$&'()*+,-./:;<=>?@[\]^_`{|}~''' # '
49faf679d7df final keystroke fixes, recurse and decimal www stripping
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 86
diff changeset
69 ).lower()
49faf679d7df final keystroke fixes, recurse and decimal www stripping
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 86
diff changeset
70 return WPAT.sub(')',_surt)
73
e8c667bf8965 compute timestamps, key and sort lmh lines
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
71
e8c667bf8965 compute timestamps, key and sort lmh lines
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
72 def keyed(l):
85
1daa8e444cfe work-around for weird handling of %-encoding in Java impl. of SURT
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 84
diff changeset
73 uri, cc_stamp, dateTime = l.split('\t',2)
77
bf09a1d80d7b make CC's own sorting explicit
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 76
diff changeset
74 #print('ul',uri,file=sys.stderr)
73
e8c667bf8965 compute timestamps, key and sort lmh lines
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
75 try:
76
eeef811f734d handle corner cases with final . and initial www..+
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 75
diff changeset
76 try:
eeef811f734d handle corner cases with final . and initial www..+
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 75
diff changeset
77 epoch = email.utils.parsedate_to_datetime(dateTime).timestamp()
eeef811f734d handle corner cases with final . and initial www..+
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 75
diff changeset
78 except OverflowError:
eeef811f734d handle corner cases with final . and initial www..+
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 75
diff changeset
79 epoch = 32535215999.0
97
135a8c56dcc2 include full URI in output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 88
diff changeset
80 return ((cdx_key(uri), cc_stamp, uri), epoch)
75
177f7df2bf46 handle %-encoded utf-8 as idna
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 73
diff changeset
81 except (TypeError,IndexError,ValueError) as e:
73
e8c667bf8965 compute timestamps, key and sort lmh lines
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
82 print(dateTime.rstrip(),e,sep='\t',file=sys.stderr)
e8c667bf8965 compute timestamps, key and sort lmh lines
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
83 return
e8c667bf8965 compute timestamps, key and sort lmh lines
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
84
97
135a8c56dcc2 include full URI in output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 88
diff changeset
85 fstr = sys.argv[1]
135a8c56dcc2 include full URI in output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 88
diff changeset
86
135a8c56dcc2 include full URI in output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 88
diff changeset
87 with open(fstr,"r") as ff:
85
1daa8e444cfe work-around for weird handling of %-encoding in Java impl. of SURT
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 84
diff changeset
88 # crucial that the following is done _after_ the file is opened
1daa8e444cfe work-around for weird handling of %-encoding in Java impl. of SURT
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 84
diff changeset
89 # with the default (utf-8) locale!
1daa8e444cfe work-around for weird handling of %-encoding in Java impl. of SURT
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 84
diff changeset
90 locale.setlocale(locale.LC_ALL, "C")
1daa8e444cfe work-around for weird handling of %-encoding in Java impl. of SURT
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 84
diff changeset
91 ctk=cmp_to_key(locale.strcoll)
97
135a8c56dcc2 include full URI in output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 88
diff changeset
92 for key, ts in sorted((kk for l in ff if (kk:=keyed(l)) is not None),
85
1daa8e444cfe work-around for weird handling of %-encoding in Java impl. of SURT
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 84
diff changeset
93 key=lambda x:x[0]):
97
135a8c56dcc2 include full URI in output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 88
diff changeset
94 print(key[0],key[1],key[2],ts,sep='\t')
77
bf09a1d80d7b make CC's own sorting explicit
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 76
diff changeset
95