Last active
August 29, 2015 14:04
-
-
Save HyShai/e0a83811d8c635bd226f to your computer and use it in GitHub Desktop.
url-lengthener.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
@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.
Now as a repo - https://github.com/HyShai/url-cleaner
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks great!