Browse Source

[wip] url shortener support (#19)

Signed-off-by: r4sas <r4sas@i2pmail.org>
dependabot/add-v2-config-file
R4SAS 5 years ago
parent
commit
6b6c33e545
Signed by untrusted user: r4sas
GPG Key ID: 66F6C87B98EBCFE2
  1. 22
      pbincli/api.py
  2. 17
      pbincli/cli.py

22
pbincli/api.py

@ -1,8 +1,8 @@
import requests import requests
class PrivateBin: class PrivateBin:
def __init__(self, server, settings=None): def __init__(self, settings=None):
self.server = server self.server = settings['server']
self.headers = {'X-Requested-With': 'JSONHttpRequest'} self.headers = {'X-Requested-With': 'JSONHttpRequest'}
if settings['proxy']: if settings['proxy']:
@ -72,3 +72,21 @@ class PrivateBin:
'v' in jsonldSchema['@context'] and 'v' in jsonldSchema['@context'] and
'@value' in jsonldSchema['@context']['v']) \ '@value' in jsonldSchema['@context']['v']) \
else 1 else 1
class Shortener:
def __init__(self, settings=None):
self.server = settings['server']
self.headers = {'X-Requested-With': 'JSONHttpRequest'}
if settings['proxy']:
self.proxy = {settings['proxy'].split('://')[0]: settings['proxy']}
else:
self.proxy = {}
if settings['noinsecurewarn']:
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
self.session = requests.Session()
self.session.verify = settings['nocheckcert']

17
pbincli/cli.py

@ -2,7 +2,7 @@
import os, sys, argparse import os, sys, argparse
import pbincli.actions import pbincli.actions
from pbincli.api import PrivateBin from pbincli.api import PrivateBin, Shortener
from pbincli.utils import PBinCLIException, validate_url from pbincli.utils import PBinCLIException, validate_url
CONFIG_PATHS = [os.path.join(".", "pbincli.conf", ), CONFIG_PATHS = [os.path.join(".", "pbincli.conf", ),
@ -36,8 +36,18 @@ def main():
send_parser.add_argument("-q", "--notext", default=False, action="store_true", help="don't send text in paste") send_parser.add_argument("-q", "--notext", default=False, action="store_true", help="don't send text in paste")
send_parser.add_argument("-c", "--compression", default="zlib", action="store", send_parser.add_argument("-c", "--compression", default="zlib", action="store",
choices=["zlib", "none"], help="set compression for paste (default: zlib). Note: works only on v2 paste format") choices=["zlib", "none"], help="set compression for paste (default: zlib). Note: works only on v2 paste format")
# URL shortener
send_parser.add_argument("-s", "--shorten", default=False, action="store_true", help="use URL shortener")
send_parser.add_argument("--shorten-api", default="yourls", action="store",
choices=["yourls"], help="select API used for selected URL shortener service")
send_parser.add_argument("--shorten-url", help="URL of shortener service API")
send_parser.add_argument("--shorten-user", help="Shortener username")
send_parser.add_argument("--shorten-pass", help="Shortener password")
send_parser.add_argument("--shorten-token", help="Shortener token")
# Certificates check bypass
send_parser.add_argument("--no-check-certificate", default=True, action="store_false", help="disable certificate validation") send_parser.add_argument("--no-check-certificate", default=True, action="store_false", help="disable certificate validation")
send_parser.add_argument("--no-insecure-warning", default=False, action="store_true", help="suppress InsecureRequestWarning (only with --no-check-certificate)") send_parser.add_argument("--no-insecure-warning", default=False, action="store_true", help="suppress InsecureRequestWarning (only with --no-check-certificate)")
#
send_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug") send_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug")
send_parser.add_argument("--dry", default=False, action="store_true", help="invoke dry run") send_parser.add_argument("--dry", default=False, action="store_true", help="invoke dry run")
send_parser.add_argument("stdin", help="input paste text from stdin", nargs="?", type=argparse.FileType("r"), default=sys.stdin) send_parser.add_argument("stdin", help="input paste text from stdin", nargs="?", type=argparse.FileType("r"), default=sys.stdin)
@ -78,15 +88,14 @@ def main():
var = "PRIVATEBIN_{}".format(key.upper()) var = "PRIVATEBIN_{}".format(key.upper())
if var in os.environ: CONFIG[key] = os.getenv(var) if var in os.environ: CONFIG[key] = os.getenv(var)
PBIN_URL = validate_url(CONFIG["server"])
SETTINGS = { SETTINGS = {
"server" : validate_url(CONFIG["server"])
"proxy": CONFIG["proxy"], "proxy": CONFIG["proxy"],
"nocheckcert": args.no_check_certificate, "nocheckcert": args.no_check_certificate,
"noinsecurewarn": args.no_insecure_warning "noinsecurewarn": args.no_insecure_warning
} }
api_client = PrivateBin(PBIN_URL, settings=SETTINGS) api_client = PrivateBin(SETTINGS)
if hasattr(args, "func"): if hasattr(args, "func"):
try: try:

Loading…
Cancel
Save