diff options
author | Andreas Schneider <asn@cryptomilk.org> | 2013-10-02 12:26:48 +0200 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2013-10-02 12:26:48 +0200 |
commit | eec426227ed9bf812791c7db8e3c36da01e25dde (patch) | |
tree | 5a0511d063dbf89c0666e19299c15a9dd8891936 | |
download | cpaste-eec426227ed9bf812791c7db8e3c36da01e25dde.tar.gz cpaste-eec426227ed9bf812791c7db8e3c36da01e25dde.tar.xz cpaste-eec426227ed9bf812791c7db8e3c36da01e25dde.zip |
Initial commit.
-rwxr-xr-x | cpaste | 76 |
1 files changed, 76 insertions, 0 deletions
@@ -0,0 +1,76 @@ +#!/usr/bin/python +# +####################################################################### +# +# A script to paste to https://cpaste.org/ +# +# Copyright (c) 2013 Andreas Schneider <asn@samba.org> +# Copyright (c) 2013 Alexander Bokovoy <ab@samba.org> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +####################################################################### +# +# Requires: python-requests +# + +import sys +import json +import requests +from optparse import OptionParser + +parser = OptionParser() +parser.add_option("-f", "--file", dest="filename", + help="Read from a file instead of stdin", metavar="FILE") +parser.add_option("-l", "--lang", action="store", type="string", dest="lang", default="text", + help="Specifiy the language (defaults to text).", metavar="LANG") +parser.add_option("-p", "--private", + action="store_true", dest="private", default=False, + help="don't print status messages to stdout") +(options, args) = parser.parse_args() + +if options.filename: + f = open(options.filename) + if not f: + print "Oops, could not open file!" + + paste_data = f.readlines() +else: + paste_data = sys.stdin.readlines() + +if not paste_data: + print "Oops, we have no data" + sys.exit(1) + +# json payload +payload = {'data':''.join(paste_data), 'language': options.lang, 'private': options.private} + +# http content type +headers = {'Content-Type': 'application/json'} + +r = requests.post('http://cpaste.org/api/json/create', data=json.dumps(payload), headers=headers) + +result = json.loads(r.text)['result'] + +if result.get(u'error', None): + print "Oops, error with code %(error)s" % result + sys.exit(1) + +hash = result.get(u'hash', None) +if hash and len(hash) > 0: + print "https://cpaste.org/%(id)s/%(hash)s" % result +else: + print "https://cpaste.org/%(id)s" % result + +# http://cpaste.org/api/json/create |