annotate bin/sort_date.py @ 80:db3c689175fe

catching up by hand with markup version, adding query string
author Henry Thompson <ht@markup.co.uk>
date Sat, 19 Aug 2023 15:53:59 -0400
parents
children 7bbb14f6e394
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
80
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
1 #!/usr/bin/python3
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
2 # Assumes you have used grep -v $'\t' on input for speed
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
3 # Recommended to also sed '/GMT$/s/\([^ ]\)GMT$/\1 GMT/'
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
4 # to fix a common 'bad' timestamp (~ .2% of inputs)
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
5 import email.utils
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
6 import sys
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
7 from urllib.parse import urlsplit, unquote
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
8 import re
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
9 # Thanks to https://stackoverflow.com/a/8776871
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
10 import locale
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
11 from functools import cmp_to_key
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
12
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
13 WWW=re.compile("www[0-9]*$")
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
14
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
15 def auth(s):
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
16 #print('auth',s,file=sys.stderr)
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
17 if '%' in s:
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
18 kk=[(unquote(k).encode('idna')).decode('ascii') for k in s.split('.')]
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
19 else:
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
20 kk=s.split('.')
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
21 kk.reverse()
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
22 if kk[0] == '':
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
23 # final full stop is pruned by CC
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
24 kk.pop(0)
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
25 while WWW.match(kk[-1]):
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
26 # any www... prefix is pruned
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
27 kk.pop()
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
28 return ','.join(kk)
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
29
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
30 def keyed(l):
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
31 uri, dateTime = l.split(b'\t',1)
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
32 uri=uri.decode('ascii')
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
33 dateTime=dateTime.decode('utf8') # occasional weird ones
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
34 #print('ul',uri,file=sys.stderr)
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
35 try:
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
36 try:
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
37 epoch = email.utils.parsedate_to_datetime(dateTime).timestamp()
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
38 except OverflowError:
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
39 epoch = 32535215999.0
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
40 parts = urlsplit(uri)
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
41 nl = parts.netloc
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
42 pq = '?%s'%parts.query if parts.query else '';
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
43 #print('nl',nl,file=sys.stderr)
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
44 if ':' in nl:
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
45 pa,pp=nl.split(':')
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
46 return ('%s:%s)%s%s'%(auth(pa), pp, parts.path, pq),epoch)
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
47 else:
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
48 return ('%s)%s%s'%(auth(nl), parts.path, pq),epoch)
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
49 except (TypeError,IndexError,ValueError) as e:
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
50 print(dateTime.rstrip(),e,sep='\t',file=sys.stderr)
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
51 return
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
52
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
53 locale.setlocale(locale.LC_ALL, "C")
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
54 ctk=cmp_to_key(locale.strcoll)
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
55
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
56 with open(sys.argv[1],"rb") as ff:
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
57 for tl in sorted((kk for l in ff if (kk:=keyed(l)) is not None),
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
58 key=lambda x:ctk(x[0])):
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
59 print(tl[0],tl[1],sep='\t')
db3c689175fe catching up by hand with markup version,
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
60