#!/usr/bin/env python #---------------------------------------------------- # Licensed under WTFPL v2 # (see COPYING for full license text) # #---------------------------------------------------- # only works if useragent contains libcurl #---------------------------------------------------- '''[cat |] %prog [file1 file2 ...]''' __version__ = '0.4.1' __desc__ = ''' Upload file to paste.xinu.at and copy URL to clipboard or nopaste stdin ''' from optparse import OptionParser from subprocess import Popen, PIPE from urllib2 import urlopen import os import sys import netrc import re def password(): ''' netrc: machine paste.xinu.at password PASSWORD''' try: auth = netrc.netrc().authenticators('paste.xinu.at') except: return None if not auth: return None return auth[2] def do_upload(file): pw = password() curl_args = [] if pw: curl_args.append('-F') curl_args.append('password='+pw) url = Popen(merge([['curl', '-#', '-L', '-F', 'userfile=@'+file, ], curl_args, ['http://paste.xinu.at/file/do_upload']]), stdout=PIPE).communicate()[0] print url Popen('echo -n "%s" | nohup &>/dev/null xclip' % url, shell=True) def delete(id): pw = password() curl_args = [] if not pw: print 'Please set up .netrc correctly' sys.exit(1) else: curl_args.append('-F') curl_args.append('password='+pw) Popen(merge([['curl', '-#', '-L'], curl_args, ['http://paste.xinu.at/file/delete/'+id]] ),).communicate()[0] def get(id): print urlopen('http://paste.xinu.at/d/%s' % id).read() def merge(seq): merged = [] for s in seq: for x in s: merged.append(x) return merged def read_stdin(tmpfile): if os.isatty(sys.stdin.fileno()): print '^C to exit, ^D to send' try: content = sys.stdin.read() except KeyboardInterrupt: sys.exit() if not content: sys.exit() tmpfile = tmpfile.replace('\n', '') f = open(tmpfile, 'w') f.write(content) f.close() return tmpfile def main(): p = OptionParser(version=__version__, usage=__doc__, description=__desc__+password.__doc__) p.add_option('-d', '--delete', action='store_true', dest='delete', help='delete IDs', default=False) p.add_option('-e', '--extension', action='store', dest='extension', help='extension for tempfiles when pipeing (e.g. "diff")', default='') p.add_option('-g', '--get', action='store_true', dest='get', help='Download File IDs and output to stdout (use with care!)', default=False) options, args = p.parse_args() tmpfiles = [] tmpdir = Popen(['mktemp', '-d'], stdout=PIPE).communicate()[0] tmpdir = tmpdir.replace('\n', '') if args: for arg in args: if options.delete: delete(arg) continue if options.get: get(arg) continue if re.match('[a-z]+://.+', arg): os.chdir(tmpdir) tmpfiles.append(tmpdir) Popen(['wget', '-q', arg]).communicate()[0] for file in os.listdir(tmpdir): do_upload(file) else: do_upload(arg) else: tmpfile = read_stdin(tmpdir+"/stdin."+options.extension) tmpfiles.append(tmpfile) do_upload(tmpfile) os.chdir('/tmp') for path in tmpfiles: Popen(['rm', '-rf', path]) if __name__ == '__main__': main()