diff options
author | Cédric Picard <cedric.picard@efrei.net> | 2014-11-28 19:22:52 +0100 |
---|---|---|
committer | Cédric Picard <cedric.picard@efrei.net> | 2014-11-28 19:22:52 +0100 |
commit | f106195fbbf58d8dad3dfa80e92d165a7af229bc (patch) | |
tree | 75e1b1cc8b6b96c3f80547ee83f9a9f91780ee9b | |
parent | 5f0f749c7e1962b4a2f67ad1ff0342b5b49140d1 (diff) | |
download | pasteme-f106195fbbf58d8dad3dfa80e92d165a7af229bc.tar.gz pasteme-f106195fbbf58d8dad3dfa80e92d165a7af229bc.tar.bz2 pasteme-f106195fbbf58d8dad3dfa80e92d165a7af229bc.zip |
Added pastit, cool convenient cli client that *works*
-rwxr-xr-x | scripts/pastit.py | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/scripts/pastit.py b/scripts/pastit.py new file mode 100755 index 0000000..e515c40 --- /dev/null +++ b/scripts/pastit.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +""" +Pasteme's client + +Usage: pastit [-u url] [FILE] + +Arguments: + FILE File to send + If missing, reads from stdin + If FILE is an url, then the corresponding file + is downloaded and written on stdout. + +Options: + -u, --url url Alternate url of the pasteme + Default is http://paste.devys.org/ +""" + +import sys +import requests +from docopt import docopt + + +def post(text, url): + try: + req = requests.post(url, data={"content":text}) + except requests.exceptions.ConnectionError as e: + sys.exit(e) + return req.url + + +def get(url): + if not url.endswith("/raw"): + url += "/raw" + + try: + req = requests.get(url) + except requests.exceptions.ConnectionError as e: + sys.exit(e) + + return req.text + + +def main(): + args = docopt(__doc__) + + if '://' in args["FILE"]: + print(get(args["FILE"])) + return + + + url = args["--url"] or "http://paste.devys.org/" + + if args["FILE"]: + try: + text = open(args["FILE"]).read() + except (FileNotFoundError,PermissionError) as e: + exit(e) + else: + text = sys.stdin.read() + + print(post(text, url)) + + +if __name__ == "__main__": + main() |