Expand shortURL link to Original URL

Simple script to return or expand a shortURL link. It use Python.

It include more 20 different shortURL links provider.
If the link provider is not in the list set Force to True

set dialogText to "Paste the shortURL link:"
set theURLString to (display dialog dialogText default answer "https://apple.co/3oFQeDf")'s text returned

display alert (my expandShortURL:theURLString force:"False")

on expandShortURL:_theURL force:_bool
	set theScript to "
import requests
from urlparse import urlparse, urljoin

URL_SHORTENERS = [
    'bit.ly',
    'ow.ly',
    'dlvr.it',
    'fb.me',
    '4sq.com',
    'is.gd',
    'j.mp',
    'tmblr.co',
    'ibm.co',
    'on.fb.me',
    'vsb.li',
    'shar.es',
    'ht.ly',
    'wp.me',
    'dld.bz',
    'n.pr',
    'tiny.cc',
    'ar.gy',
    'adf.ly',
    'lnkd.in',
    'ow.ly',
    'slidesha.re',
    'bbc.in',
    'flic.kr',
    'bitly.com',
    'wapo.st',
    'aol.it',
    'monk.ly',
    'intel.ly',
    'go.ign.com',
    'twb.io',
    'short.to',
    's.co',
    'youtu.be',
    'tiny.cc',
    'huff.to',
    'on.mash.to',
    'goo.gl',
    'tinyurl.com',
    'git.io',
    'su.pr',
    'rww.to',
    'appd.it',
    'on.cnn.com',
    'trap.it',
    'dlvr.it',
    'zite.to',
    'itsh.bo',
]

def resolve_short_url(url, force=False, depth=1):
    domain = urlparse(url).netloc

    if not force and len(domain) >= 7 and domain not in URL_SHORTENERS:
        return url
    
    count = 0

    while count < depth:
        count += 1

        r = requests.head(url, allow_redirects=False)

        if 'location' not in r.headers:
            break
        
        url = r.headers['location']

        if url.startswith('//'):
            parsed_rurl = urlparse(r.url)
            url = '%s:%s' % (parsed_rurl.scheme, url)

        if not urlparse(url).netloc:
            url = urljoin(r.url, url)
        
    return url

print resolve_short_url('" & _theURL & "', '" & _bool & "',1)
"
	return (do shell script "python <<EOF" & theScript & "EOF") as text
end expandShortURL:force: