Created
July 19, 2012 16:54
-
-
Save dcolish/3145281 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| from argparse import ArgumentParser, FileType | |
| import base64 | |
| from ConfigParser import Error as ConfigurationError, SafeConfigParser | |
| from contextlib import closing | |
| import getpass | |
| from httplib import HTTPSConnection | |
| import json | |
| import os | |
| import pprint | |
| import sys | |
| from urllib import urlencode | |
| HOST= 'api.github.com' | |
| def copy_url(url): | |
| """Copy the url into the clipboard.""" | |
| # try windows first | |
| try: | |
| import win32clipboard | |
| except ImportError: | |
| # then give pbcopy a try. do that before gtk because | |
| # gtk might be installed on os x but nobody is interested | |
| # in the X11 clipboard there. | |
| from subprocess import Popen, PIPE | |
| for prog in 'pbcopy', 'xclip': | |
| try: | |
| client = Popen([prog], stdin=PIPE) | |
| except OSError: | |
| continue | |
| else: | |
| client.stdin.write(url) | |
| client.stdin.close() | |
| client.wait() | |
| break | |
| else: | |
| try: | |
| import pygtk | |
| pygtk.require('2.0') | |
| import gtk | |
| import gobject | |
| except ImportError: | |
| return | |
| gtk.clipboard_get(gtk.gdk.SELECTION_CLIPBOARD).set_text(url) | |
| gobject.idle_add(gtk.main_quit) | |
| gtk.main() | |
| else: | |
| win32clipboard.OpenClipboard() | |
| win32clipboard.EmptyClipboard() | |
| win32clipboard.SetClipboardText(url) | |
| win32clipboard.CloseClipboard() | |
| def authorize(user, password): | |
| with closing(HTTPSConnection(HOST, timeout=10)) as conn: | |
| data = { | |
| 'scopes': ['gist'], | |
| } | |
| encoded_auth = base64.b64encode('%s:%s' % (user, password)) | |
| conn.request('POST', '/authorizations', json.dumps(data), | |
| {'Authorization': 'Basic %s' % encoded_auth, | |
| 'Content-Type': 'application/json'}) | |
| resp = conn.getresponse() | |
| return json.loads(resp.read()) | |
| def post(filename, content, token): | |
| with closing(HTTPSConnection(HOST, timeout=10)) as conn: | |
| data = { | |
| 'public': True, | |
| 'files': { | |
| filename: { | |
| "content": content | |
| } | |
| }, | |
| } | |
| path = '/gists?%s' % urlencode({'access_token': token}) | |
| conn.request('POST', path, json.dumps(data), | |
| {'Content-Type': 'application/json'}) | |
| resp = conn.getresponse() | |
| if resp.status >= 200 and resp.status < 300: | |
| resp_data = json.loads(resp.read()) | |
| pprint(resp_data) | |
| copy_url(resp_data['html_url']) | |
| def main(): | |
| parser = ArgumentParser() | |
| parser.add_argument('-c', '--config', | |
| help='Configuration to use, defaults to ~/.pastebin', | |
| default=os.path.expanduser( | |
| os.path.join('~', '.gist'))) | |
| parser.add_argument('paste', nargs='?', | |
| help='Accepts a filename or - for stdin', | |
| type=FileType('r'), | |
| default=None) | |
| namespace = parser.parse_args() | |
| paste = namespace.paste | |
| if not paste and not os.isatty(sys.stdin.fileno()): | |
| paste = sys.stdin | |
| if paste: | |
| auth = {} | |
| info_path = os.path.expanduser(namespace.config) | |
| if os.path.exists(info_path): | |
| with open(info_path) as fp: | |
| auth = json.load(fp) | |
| else: | |
| auth = authorize(getpass.getuser(), getpass.getpass('Password: ')) | |
| with open(info_path, 'w+') as fp: | |
| json.dump(auth, fp) | |
| post(paste.name, paste.read(), auth['token']) | |
| else: | |
| parser.print_help() | |
| sys.exit(-1) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment