Automating short URL creation on iOS
import urllib import json import webbrowser import sys url = sys.argv api = 'https://api-ssl.bitly.com/v3/shorten' token = 'Insert your token here' request = api + '?access_token=' + token + '&longURL=' + url usock = urllib.urlopen(request) data = usock.read() dataJSON = json.loads(data) shortURL = dataJSON["data"]["url"] outputURL = 'drafts://x-callback-url/create?text=' + shortURL webbrowser.open(outputURL) usock.close()
- Create a new draft that has one line consisting of the URL to shorten
- Run the newly created action
- A new draft is automatically created with the short URL
If you do not have Pythonista this could be accomplished in a slightly less automated fashion using bitly’s x-callback-url scheme. When I looked into this I had no luck getting the x-success parameter to work though.
It is also worth noting that you must ensure your Pythonista script has the same name as the script you are calling in the first parameter of the Drafts action.
Special thanks to Federico Viticci and Alex Guyot for the inspiration.