Last active
August 21, 2016 13:31
-
-
Save mgeeky/2fb7a90e87c9a568c759 to your computer and use it in GitHub Desktop.
Pastebin uploader. Just register global hotkey for it (like Win+Y) and have it upload code from clipboard and return URL.
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
#!/usr/bin/env python | |
# | |
# Simple bit-ly URL shortener. | |
# Usage: | |
# python bit-ly.py <URL> | |
# or by having a URL address in clipboard. | |
# | |
import bitly_api | |
from Tkinter import Tk | |
import sys | |
import pprint | |
API_USER = "<YOUR API_USER>" | |
API_KEY = "<YOUR API_KEY>" | |
def PreparePasteCode(): | |
r = Tk() | |
r.withdraw() | |
code = r.clipboard_get() | |
r.destroy() | |
return code | |
def PutAddressIntoClipboard(address): | |
r = Tk() | |
r.withdraw() | |
r.clipboard_clear() | |
r.clipboard_append(address) | |
r.destroy() | |
return | |
def PrintBitlyError(e, url): | |
return "<!> Bitly error = " + str(e) + " (url: '" + url[:256] + "')" | |
def ShortenURL(b, url): | |
try: | |
response = b.shorten(url) | |
return response['url'] | |
except bitly_api.BitlyError as e: | |
PrintBitlyError(e, url) | |
if __name__ == '__main__': | |
longurl = "" | |
shorturl = "" | |
use_clipboard = False | |
if len(sys.argv) != 2: | |
use_clipboard = True | |
longurl = PreparePasteCode() | |
else: | |
longurl = sys.argv[1] | |
# Instantiate bit-ly connection. | |
b = bitly_api.Connection(API_USER, API_KEY) | |
shorturl = ShortenURL(b, longurl) | |
if use_clipboard: | |
PutAddressIntoClipboard(shorturl) | |
print shorturl | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment