#!/usr/bin/env python3 import json import urllib.request as urlreq from urllib.parse import urlencode import sys import argparse SERVER="https://paste.openttdcoop.org" DEFAULT_LANG="text" def create_paste(payload): """Attempt to create a paste, given a dict of parameters """ form_data = urlencode(payload, doseq=True) req = urlreq.urlopen(SERVER + "/api/json/create", form_data.encode("utf-8")) return json.loads(req.read().decode("utf-8")) def list_langs(): def fmtcols(mylist, cols): maxwidth = len(max(mylist, key=len)) justifyList = [x.ljust(maxwidth) for x in mylist] lines = (' '.join(justifyList[i:i + cols]) for i in range(0, len(justifyList), cols)) return "\n".join(lines) req = urlreq.urlopen(SERVER + "/api/json/parameter/language") lang_list = json.loads(req.read().decode("utf-8"))["result"]["values"] return fmtcols(lang_list, 6) parser = argparse.ArgumentParser(description="""#openttdcoop Paste Script. Script is made to work with sticky-notes v1.8. It may work with earlier/later versions but no guarantees""") parser.add_argument("-l", "--language", default=DEFAULT_LANG, help="""Can be any language which is supported by sticky-notes. Default: {}""".format(DEFAULT_LANG)) parser.add_argument("-t", "--title", help="""The title you would like to give to your paste. Default: none""") parser.add_argument("-d", "--data", help="""The data you would like to publish to the paste. You can also pipe data into this script via stdin (No need to provide -d option in which case)""") parser.add_argument("-v", "--private", action="store_true", help="""Set your paste to private. This will make your paste only be accessible if the proper hash has been supplied with the paste ID""") parser.add_argument("-p", "--password", help="""Set a password on the paste which will be needed to see it. Can be seen as more secure than private""") parser.add_argument("-L", "--languages", action="store_true", help="""Get a list of possible languages""") args = parser.parse_args() if args.languages: try: print(list_langs()) except KeyError: print("Error getting list of languages") sys.exit(0) # Create paste payload = {} # Mandatory parameters payload["data"] = args.data if args.data else sys.stdin.read() payload["language"] = args.language # Optional parameters if args.private: payload["private"] = "true" if args.title: payload["title"] = args.title if args.password: payload["password"] = args.password req_json = create_paste(payload) try: out_url = SERVER + '/' + req_json["result"]["id"] if "hash" in req_json["result"]: out_url += '/' + req_json["result"]["hash"] print("Pasted to: " + out_url) except KeyError: print("Error pasting: " + str(req_json))