Skip to content

Instantly share code, notes, and snippets.

@kiorky
Created November 21, 2012 10:31
Show Gist options
  • Save kiorky/4124177 to your computer and use it in GitHub Desktop.
Save kiorky/4124177 to your computer and use it in GitHub Desktop.
urlparse
#!/usr/bin/env bash
URL="$1"
extract() {
local url=$1
python << EOF
import urlparse
u = '$url'
pu = urlparse.urlparse(u)
netloc = pu.netloc
tlds = [a.strip() for a in open('tld.txt').read().splitlines() if a.strip()]
step = 2
matching_tlds = [tld for tld in tlds if netloc.endswith(tld)]
matching_tlds.sort(key=lambda x:len(x))
if matching_tlds:
step += matching_tlds[-1].count('.') -1
print '.'.join(netloc.split('.')[-step:])
EOF
}
echo "$1: $(extract $1)"
# vim:set et sts=4 ts=4 tw=80:
other.co.uk
co.uk
bar.co.uk
@kiorky
Copy link
Author

kiorky commented Nov 21, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment