Skip to content

Instantly share code, notes, and snippets.

@SpiralP
Created April 27, 2015 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SpiralP/4f38046a163222b729a8 to your computer and use it in GitHub Desktop.
Save SpiralP/4f38046a163222b729a8 to your computer and use it in GitHub Desktop.
import urllib2
import json
import sys
import os
from Tkinter import Tk
URL="https://api.github.com/gists"
TOKEN=""
args = sys.argv[:]
args.pop(0)
files = {}
if len(args)>0:
for filename in args:
with open(filename,'rb') as file:
files[os.path.split(filename)[1]] = {"content": file.read()}
else:
good = False
tk = Tk()
tk.withdraw()
try:
cb = tk.clipboard_get()
files[""] = {"content": cb} # sys.stdin.read()
good = True
except:
pass
tk.destroy()
if not good:
print('not normal text')
import win32clipboard as c
c.OpenClipboard()
try:
format = c.RegisterClipboardFormat('FileName')
if c.IsClipboardFormatAvailable(format):
filename = c.GetClipboardData(format)[:-1] # hacks
with open(filename,'rb') as file:
files[os.path.split(filename)[1]] = {"content": file.read()}
good = True
except:
pass
c.CloseClipboard()
if not good:
print('noon')
exit(1)
o = {
"description": "",
"public": False,
"files": files
}
req = urllib2.Request(URL,json.dumps(o))
req.add_header('Authorization','token '+TOKEN)
resp = urllib2.urlopen(req)
r = json.load(resp)
resp.close()
url = r['html_url']
print(url)
tk = Tk()
tk.withdraw()
tk.clipboard_clear()
tk.clipboard_append(url)
tk.destroy()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment