Created
July 19, 2012 23:11
-
-
Save dcolish/3147532 to your computer and use it in GitHub Desktop.
Python2.7 gist uploader
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 contextlib import closing | |
| import getpass | |
| from httplib import HTTPSConnection | |
| import json | |
| import os | |
| import sys | |
| from urllib import urlencode | |
| HOST = 'api.github.com' | |
| # Hoisted from lodgeit.py | |
| # :authors: 2007-2008 Georg Brandl <georg@python.org>, | |
| # 2006 Armin Ronacher <armin.ronacher@active-4.com>, | |
| # 2006 Matt Good <matt@matt-good.net>, | |
| # 2005 Raphael Slinckx <raphael@slinckx.net> | |
| 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(token, filename, content, message, public): | |
| with closing(HTTPSConnection(HOST, timeout=10)) as conn: | |
| data = { | |
| 'public': public, | |
| 'desciption': message, | |
| '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() | |
| import pdb; pdb.set_trace() | |
| if resp.status >= 200 and resp.status < 300: | |
| resp_data = json.loads(resp.read()) | |
| print resp_data['html_url'] | |
| copy_url(resp_data['html_url']) | |
| def main(): | |
| parser = ArgumentParser() | |
| parser.add_argument('-c', '--config', | |
| help='Configuration to use, defaults to ~/.gist', | |
| default=os.path.join('~', '.gist')) | |
| parser.add_argument('-n', '--name', | |
| help=('Give an explicit filename so gist will' | |
| ' highlight syntax. Most useful for stdin.'), | |
| default=None) | |
| parser.add_argument('-m', '--message', | |
| help='A message describing this gist', | |
| default='') | |
| parser.add_argument('--public', | |
| help='Set the default visibility for this paste.', | |
| default=True) | |
| 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) | |
| name = namespace.name or os.path.basename(paste.name) | |
| 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(auth['token'], name, paste.read(), | |
| namespace.message, namespace.public) | |
| 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