diff options
Diffstat (limited to 'bin/stikked')
-rw-r--r-- | bin/stikked | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/bin/stikked b/bin/stikked new file mode 100644 index 0000000..b209208 --- /dev/null +++ b/bin/stikked @@ -0,0 +1,115 @@ +#! /usr/bin/env python2 + +import argparse +import os +import sys +import pycurl +import StringIO +import subprocess +import urllib + +## CONFIGURATION { +user='example-user' +apiurl='http://paste.giev.de/api/create' +## } + + +def main(): + global verbose + + # parse arguments + parser = argparse.ArgumentParser(prog='stikked') + parser.add_argument('-V', '--version', action='version', version='%(prog)s 0.1') + parser.add_argument('-v', '--verbose', action='store_true', dest='verbose', help='verbose mode', default=False) + parser.add_argument('-s', dest='syntax', action='store', help='syntax to highlight', default='text') + parser.add_argument('-p', dest='private', action='store_true', help='set paste to private', default=False) + parser.add_argument('-t', dest='title', action='store', help='set title of paste') + parser.add_argument('fname', metavar='FILE|-', help='file to paste or - for stdin') + args = parser.parse_args() + if args.verbose: + verbose = args.verbose + print args + + # private + private = '1' if args.private else '0' + + # from stdin + if args.fname == '-': + title = args.title if args.title else 'stdin' + text=sys.stdin.read() + syntax=args.syntax + + # from file + else: + fname = args.fname + + if not os.path.isfile(fname): + print "No such file: {0}".format(fname) + sys.exit(1) + else: + title = args.title if args.title else os.path.split(fname)[1] + + if args.syntax == 'text': + ext = os.path.splitext(fname)[1][1:] + syntax = _detect_syntax(ext) + else: + syntax = args.syntax + + fopen = file(fname, 'r') + text = fopen.read() + + _paste(text, title, syntax, private) + + +# send paste to server +def _paste(text, title, syntax, private): + postparams = [ + ('text', text), + ('name', user), + ('title', title), + ('lang', syntax), + ('private', private)] + + postfields=urllib.urlencode(postparams) + + curl = pycurl.Curl() + curl.setopt(pycurl.URL, apiurl) + curl.setopt(pycurl.POST, True) + curl.setopt(pycurl.POSTFIELDS, postfields) + # Fixes the HTTP/1.1 417 Expectation Failed Bug + header = [] + header.append("Expect: ") + curl.setopt(pycurl.HTTPHEADER, header) + curl.setopt(pycurl.NOPROGRESS, True) + b = StringIO.StringIO() + curl.setopt(pycurl.WRITEFUNCTION, b.write) + curl.perform() + print b.getvalue(); + + # copy to primary clipboard + xsel_proc = xsel_proc = subprocess.Popen(['xsel', '-pi'], stdin=subprocess.PIPE) + xsel_proc.communicate(b.getvalue()) + + +# more possible of course +def _detect_syntax(ext): + if ext == 'h': + syntax='c' + elif ext == 'log': + syntax='logcat' + elif ext == "py": + syntax = 'python' + elif ext == "pl": + syntax = 'perl' + elif ext == "patch": + syntax = 'diff' + elif ext == 'c' or ext == 'cpp' or ext == 'java' or ext == 'sh': + syntax=ext + else: + syntax='text' + + return syntax + + +if __name__ == '__main__': + main() |