view make-tld-conf.py @ 299:c5624b26a29d

ignore www.google-analytics.com
author Carl Byington <carl@five-ten-sg.com>
date Mon, 13 Oct 2014 13:37:31 -0700
parents 9f0d9fcb58dd
children 3a3b0cbcaf6e
line wrap: on
line source

#!/usr/bin/python

import os, sys, time

def cmd(c):
    return os.popen(c).read().rstrip('\n')

url = 'https://publicsuffix.org/list/effective_tld_names.dat'
c = 'curl %s 2>/dev/null' % url
content = cmd(c)
f=open('tld.conf','w')
f.write(time.strftime("// generated by make-tld-conf.py on %Y-%m-%d %H:%M:%S\n\n"))
for x in content.split('\n'):
    if (x == '') or (x[:2] == '//'): f.write('%s\n' % x)
    else:
        if x[:1] == '!':
            x = x[1:]
            prefix = '!'
        else:
            prefix = ''
        yy = []
        for xx in x.split('.'):
            y = unicode(xx, 'utf8')
            z = y.encode('punycode')
            if z[-1:] == '-': yy.append(xx)
            else:             yy.append('xn--%s' % z)
        y = '.'.join(yy)
        if not (y == x):
            f.write('// %s%s\n' % (prefix, x))
            x = y
        if not prefix and (len(x.split('.')) == 1):
            rc = cmd("dig %s ns +short" % x)
            if not rc:
                rc = cmd("sleep 3; dig %s ns +short" % x)
                if not rc: prefix = '// no ns record for '
        f.write('%s%s\n' % (prefix, x))
f.close()