Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kultprok

kultprok/isgd.py Secret

Last active December 19, 2015 18:29
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 kultprok/f4f62e4e9bc59e575726 to your computer and use it in GitHub Desktop.
Save kultprok/f4f62e4e9bc59e575726 to your computer and use it in GitHub Desktop.
Shorten links via is.gd-API using Pythonista
import clipboard
from console import alert
from sys import argv, exit
from urllib import urlopen
import webbrowser
def error_dialog(title, message):
'''A diaolog box for error messages.'''
try:
alert(title, message)
except KeyboardInterrupt:
pass
webbrowser.open('drafts://')
exit(message)
def shorten_with_isgd(long_url):
'''basic link-shortening via is.gd.'''
api_url_base = 'http://is.gd/create.php?format=simple&url='
try:
response = urlopen(api_url_base + long_url)
except IOError:
error_dialog('Connection Error', 'Unable to perform request.')
if response.getcode() == 200:
short_link = response.read()
clipboard.set(short_link)
else:
error_dialog('Error', 'Status code: {0} - Message: {1}'.format(response.getcode(), response.read()))
webbrowser.open('drafts://')
if __name__ == '__main__':
shorten_with_isgd(argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment