From a7d62df9663a3d5a13dfb7c4914b03f877142e83 Mon Sep 17 00:00:00 2001 From: R4SAS Date: Fri, 7 Apr 2023 22:41:07 +0300 Subject: [PATCH] add get --output option for decoded data (#40) Signed-off-by: R4SAS --- pbincli/actions.py | 25 +++++++++++++++++-------- pbincli/cli.py | 2 ++ pbincli/utils.py | 11 +++++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/pbincli/actions.py b/pbincli/actions.py index ff0b146..8920b56 100644 --- a/pbincli/actions.py +++ b/pbincli/actions.py @@ -3,7 +3,7 @@ from urllib.parse import parse_qsl from pbincli.api import Shortener from pbincli.format import Paste -from pbincli.utils import PBinCLIError, check_writable, json_encode, uri_validator, validate_url_ending +from pbincli.utils import PBinCLIError, check_writable, json_encode, uri_validator, validate_url_ending, validate_path_ending def signal_handler(sig, frame): @@ -166,21 +166,30 @@ def get(args, api_client, settings=None): if len(text): if args.debug: print("{}\n".format(text.decode())) - filename = "paste-" + pasteid + ".txt" - print("Found text in paste. Saving it to {}".format(filename)) + if settings['output']: + paste_path = validate_path_ending(settings['output']) + "paste-" + pasteid + ".txt" + else: + paste_path = "paste-" + pasteid + ".txt" - check_writable(filename) - with open(filename, "wb") as f: + print("Found text in paste. Saving it to {}".format(paste_path)) + + check_writable(paste_path) + with open(paste_path, "wb") as f: f.write(text) f.close() attachment, attachment_name = paste.getAttachment() if attachment: - print("Found file, attached to paste. Saving it to {}\n".format(attachment_name)) + if settings['output']: + attachment_path = validate_path_ending(settings['output']) + attachment_name + else: + attachment_path = attachment_name + + print("Found file, attached to paste. Saving it to {}\n".format(attachment_path)) - check_writable(attachment_name) - with open(attachment_name, "wb") as f: + check_writable(attachment_path) + with open(attachment_path, "wb") as f: f.write(attachment) f.close() diff --git a/pbincli/cli.py b/pbincli/cli.py index 7abe37d..e8ce831 100755 --- a/pbincli/cli.py +++ b/pbincli/cli.py @@ -81,6 +81,7 @@ def main(): get_parser = subparsers.add_parser("get", description="Get data from PrivateBin instance") get_parser.add_argument("pasteinfo", help="\"PasteID#Passphrase\" or full URL") get_parser.add_argument("-p", "--password", help="Password for decrypting paste") + get_parser.add_argument("-o", "--output", default=argparse.SUPPRESS, help="Path to directory where decoded paste data will be saved") ## Connection options get_parser.add_argument("-s", "--server", default=argparse.SUPPRESS, help="Instance URL (default: https://paste.i2pd.xyz/, ignored if URL used in pasteinfo)") get_parser.add_argument("-x", "--proxy", default=argparse.SUPPRESS, help="Proxy server address (default: None)") @@ -124,6 +125,7 @@ def main(): 'short_user': None, 'short_pass': None, 'short_token': None, + 'output': None, 'no_check_certificate': False, 'no_insecure_warning': False, 'compression': None diff --git a/pbincli/utils.py b/pbincli/utils.py index 65f10b0..cb3e506 100644 --- a/pbincli/utils.py +++ b/pbincli/utils.py @@ -1,4 +1,5 @@ import json, ntpath, os, sys +from platform import system class PBinCLIException(Exception): pass @@ -35,6 +36,16 @@ def validate_url_ending(s): s = s + "/" return s +def validate_path_ending(s): + if system() == 'Windows': + slash = '\\' + else: + slash = '/' + + if not s.endswith(slash): + s = s + slash + return s + def uri_validator(x): from urllib.parse import urlsplit try: