Skip to content

Instantly share code, notes, and snippets.

@HyShai
Last active August 29, 2015 14:04
Show Gist options
  • Save HyShai/e0a83811d8c635bd226f to your computer and use it in GitHub Desktop.
Save HyShai/e0a83811d8c635bd226f to your computer and use it in GitHub Desktop.
url-lengthener.py
welcome_msg = '''url_lengthener is a poor man's CleanLinks.
See: https://itunes.apple.com/us/app/clean-links/id623671942?mt=8
It takes a shortened url and returns the lenthened url.
The inbound shortened url can either be provided as a command line
argument or on the iOS clipboard.
url_lengthener follows all header redirects, instead of downloading
the entire webpage as CleanLinks does,
(See: https://twitter.com/gcaprio/status/418040618636435456)
which 1) saves you bandwidth and 2) doesn't register as a click (at least for bit.ly)
It also has optional support for LaunchCenterPro if it is installed.
Source code at: https://gist.github.com/HyShai/e0a83811d8c635bd226f'''
import urllib, clipboard, re, requests, webbrowser, sys
def url_lengthen(url): # recursively lengthen the url
new_url = requests.head(url).headers.get('location')
return url_lengthen(new_url) if new_url else url
url = sys.argv[1] if len(sys.argv) > 1 else clipboard.get()
if url:
url = url_lengthen(url)
else:
print(welcome_msg)
sys.exit()
#strip analytics garbage
url = re.sub(r'(?<=\&)?utm\w+=[^\&]+(\&)?','',url)
if webbrowser.can_open('launch://'):
params = urllib.quote('[prompt:Set Clipboard=%s]' % url)
launch = 'launch://clipboard?text=%s' % params
webbrowser.open(launch)
else:
print 'Copying'
console.write_link(url, url)
print 'to the clipboard'
clipboard.set(url)
@cclauss
Copy link

cclauss commented Aug 1, 2014

Looks great!

@HyShai
Copy link
Author

HyShai commented Aug 1, 2014

@cclaus wow so much cleaner - I merged your code. Thanks!

Also added regex to strip "utm" query params (analytics) - from my (limited) testing it will only strip the utm params and will leave all other query params.

@HyShai
Copy link
Author

HyShai commented Aug 1, 2014

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