ulvis.paste.net

Paste Search Dynamic
Recent pastes
klist1
  1. #!/usr/bin/python3
  2. # Written by AaronP <aaronbpaden@gmail.com>
  3.  
  4. import sys
  5. import urllib.parse
  6. import urllib.request
  7. from docopt import docopt
  8.  
  9. __doc__ = """Pasteee -- Paste something to paste.ee
  10.  
  11. Usage:
  12.    pasteee [options] [<file>]
  13.    pasteee -h | --help
  14.  
  15. Options:
  16.    -d <text> --description=<text>    Set description
  17.    -h --help                         Show this screen.
  18.    -l <lang> --language=<lang>       Set language
  19.    -p --private                      Paste privately
  20.  
  21. """
  22.  
  23. def die(msg):
  24.     print(msg, file=sys.stderr)
  25.     return 1
  26.  
  27. def send_paste(txt, description, private, lang):
  28.     data = urllib.parse.urlencode({
  29.         "key": "public",
  30.         "description": description,
  31.         "language": lang,
  32.         "paste": txt,
  33.         "private": private,
  34.         "format": "simple",
  35.         "return": "link"}).encode()
  36.     link = urllib.request.urlopen("http://paste.ee/api", data).read().decode()
  37.  
  38.     if link == "error_invalid_language":
  39.         return die("Invalid language")
  40.     elif link == "error_no_paste":
  41.         return die("Paste failed.")
  42.  
  43.     print(link)
  44.     return 0
  45.  
  46. def main():
  47.     args = docopt(__doc__)
  48.     if args["--language"] == None:
  49.         language = "plain"
  50.     else:
  51.         language = args["--language"]
  52.     if args["--private"] == False:
  53.         private = 0
  54.     else:
  55.         private = 1
  56.     if args["--description"] == None:
  57.         description = ""
  58.     else:
  59.         description = args["--description"]
  60.  
  61.     if args["<file>"] == None:
  62.         txt = sys.stdin.read()
  63.     else:
  64.         try:
  65.             with open(args["<file>"], 'r') as f:
  66.                 txt = f.read()
  67.         except OSError as error:
  68.             return die("Could not open " + error.filename)
  69.  
  70.     if len(txt) < 1:
  71.         return die("Nothing to send.")
  72.  
  73.     return send_paste(txt, description, private, language)
  74.  
  75. if __name__ == "__main__":
  76.     sys.exit(main())
  77.  
  78. # vim: set ts=4 sts=4 sw=4 et:
  79.  
Parsed in 0.018 seconds