Loading

#openttdcoop Paste Script

  1. #!/usr/bin/env python3
  2.  
  3. import json
  4. import urllib.request as urlreq
  5. from urllib.parse import urlencode
  6. import sys
  7. import argparse
  8.  
  9. SERVER="https://paste.openttdcoop.org"
  10. DEFAULT_LANG="text"
  11.  
  12. def create_paste(payload):
  13.     """Attempt to create a paste, given a dict of parameters
  14.    """
  15.     form_data = urlencode(payload, doseq=True)
  16.     req = urlreq.urlopen(SERVER + "/api/json/create", form_data.encode("utf-8"))
  17.     return json.loads(req.read().decode("utf-8"))
  18.  
  19. def list_langs():
  20.     def fmtcols(mylist, cols):
  21.         maxwidth = len(max(mylist, key=len))
  22.         justifyList = [x.ljust(maxwidth) for x in mylist]
  23.         lines = (' '.join(justifyList[i:i + cols])
  24.                  for i in range(0, len(justifyList), cols))
  25.         return "\n".join(lines)
  26.  
  27.     req = urlreq.urlopen(SERVER + "/api/json/parameter/language")
  28.     lang_list = json.loads(req.read().decode("utf-8"))["result"]["values"]
  29.     return fmtcols(lang_list, 6)
  30.  
  31. parser = argparse.ArgumentParser(description="""#openttdcoop Paste Script.
  32.                                 Script is made to work with sticky-notes v1.8.
  33.                                 It may work with earlier/later versions but no
  34.                                 guarantees""")
  35.  
  36. parser.add_argument("-l", "--language", default=DEFAULT_LANG,
  37.                     help="""Can be any language which is supported by
  38.                    sticky-notes. Default: {}""".format(DEFAULT_LANG))
  39. parser.add_argument("-t", "--title", help="""The title you would like to give
  40.                    to your paste. Default: none""")
  41. parser.add_argument("-d", "--data", help="""The data you would like to publish
  42.                    to the paste. You can also pipe data into this script via
  43.                    stdin (No need to provide -d option in which case)""")
  44. parser.add_argument("-v", "--private", action="store_true",
  45.                     help="""Set your paste to private. This will make your
  46.                    paste only be accessible if the proper hash has been
  47.                    supplied with the paste ID""")
  48. parser.add_argument("-p", "--password", help="""Set a password on the paste
  49.                    which will be needed to see it. Can be seen as more secure
  50.                    than private""")
  51. parser.add_argument("-L", "--languages", action="store_true",
  52.                     help="""Get a list of possible languages""")
  53.  
  54. args = parser.parse_args()
  55.  
  56. if args.languages:
  57.     try:
  58.         print(list_langs())
  59.     except KeyError:
  60.         print("Error getting list of languages")
  61.     sys.exit(0)
  62.  
  63. # Create paste
  64. payload = {}
  65. # Mandatory parameters
  66. payload["data"] = args.data if args.data else sys.stdin.read()
  67. payload["language"] = args.language
  68.  
  69. # Optional parameters
  70. if args.private:
  71.     payload["private"] = "true"
  72.  
  73. if args.title:
  74.     payload["title"] = args.title
  75.  
  76. if args.password:
  77.     payload["password"] = args.password
  78.  
  79. req_json = create_paste(payload)
  80. try:
  81.     out_url = SERVER + '/' + req_json["result"]["id"]
  82.     if "hash" in req_json["result"]:
  83.         out_url += '/' + req_json["result"]["hash"]
  84.     print("Pasted to: " + out_url)
  85. except KeyError:
  86.     print("Error pasting: " + str(req_json))
  87.  

Comments