Last active
January 14, 2016 15:02
-
-
Save allanburleson/a3a2a2e7b7ae10792c6d to your computer and use it in GitHub Desktop.
urlshorten.py
This file contains 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
# coding: utf-8 | |
# For Pythonista iOS app | |
# Bookmarklet: | |
# javascript:window.location.href='pythonista://urlshorten?action=run&argv='+encodeURIComponent(window.location.href); | |
import appex | |
import bitly_api | |
import clipboard | |
import sys | |
shortener = bitly_api.Connection(access_token='YOUR_ACCESS_TOKEN_HERE') | |
if appex.is_running_extension(): | |
url = appex.get_url() | |
elif len(sys.argv) > 1: | |
url = sys.argv[1] | |
else: | |
url = clipboard.get() | |
try: | |
shorturl = shortener.shorten(url)[u'url'] | |
except: | |
print('There was an error!') | |
sys.exit(1) | |
print 'Shortened URL is ', shorturl | |
clipboard.set(shorturl) | |
print 'New URL copied to clipboard.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment