Skip to content

Instantly share code, notes, and snippets.

@allanburleson
Last active January 14, 2016 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allanburleson/a3a2a2e7b7ae10792c6d to your computer and use it in GitHub Desktop.
Save allanburleson/a3a2a2e7b7ae10792c6d to your computer and use it in GitHub Desktop.
urlshorten.py
# 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