Skip to content

Instantly share code, notes, and snippets.

@stania
Created September 14, 2013 18:24
Show Gist options
  • Save stania/6564331 to your computer and use it in GitHub Desktop.
Save stania/6564331 to your computer and use it in GitHub Desktop.
bitly commandline shortener
# -*- coding: utf-8 -*-
import traceback, sys
import bitly_api
from Tkinter import Tk
import tkMessageBox
access_token = ''
r = Tk()
def main():
short_url = None
r.withdraw()
try:
c = bitly_api.Connection(access_token=access_token)
result = c.shorten(to_unicode(sys.argv[1]))
short_url = result['url']
set_clipboard(short_url)
tkMessageBox.showinfo(title='Success', message='copied to clipboard: {}'.format(short_url))
except:
tkMessageBox.showerror(title='Error', message=traceback.format_exc())
finally:
r.destroy()
def to_unicode(s):
if type(s) == unicode:
return s
try:
return s.decode('utf-8')
except:
return s.decode('mbcs')
def set_clipboard(line):
line = to_unicode(line)
r.clipboard_clear()
r.clipboard_append(line)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment