From e5987cdd120702d3ec2b1e44a962d6fd018d3573 Mon Sep 17 00:00:00 2001 From: Luke Murphy Date: Tue, 18 Dec 2018 12:18:48 +0100 Subject: [PATCH] Add the addtext command. Closes https://gitlab.constantvzw.org/aa/etherdump/issues/1. --- bin/etherdump | 1 + etherdump/commands/addtext.py | 101 ++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 etherdump/commands/addtext.py diff --git a/bin/etherdump b/bin/etherdump index 9bdefee..5f5eb5f 100755 --- a/bin/etherdump +++ b/bin/etherdump @@ -18,6 +18,7 @@ where CMD could be: revisionscount showmeta html5tidy + addtext For more information on each command try: etherdump CMD --help diff --git a/etherdump/commands/addtext.py b/etherdump/commands/addtext.py new file mode 100644 index 0000000..3f3deaa --- /dev/null +++ b/etherdump/commands/addtext.py @@ -0,0 +1,101 @@ +#!/usr/bin/env python + +""" +addtext: + Add text to pads using various actions (append, prepend, etc.). + Use `--noduplicate` to avoid adding the message if already present. +""" + +from __future__ import print_function + +import codecs +import json +import sys +from argparse import ArgumentParser + +import requests + +try: + # python2 + from urllib2 import urlopen + from urllib import urlencode +except ImportError: + # python3 + from urllib.parse import urlencode + from urllib.request import urlopen + + +def main(args): + p = ArgumentParser('') + + p.add_argument('padid', help='the padid') + p.add_argument('message', help='the text to add') + p.add_argument( + '--padinfo', + default='.etherdump/settings.json', + help='settings, default: .etherdump/settings.json' + ) + p.add_argument( + '--noduplicate', + action='store_true', + help='Do not add text if already present' + ) + + actions_group = p.add_mutually_exclusive_group() + actions_group.add_argument( + '--prepend', + action='store_true', + help='Action: prepend message to the pad' + ) + actions_group.add_argument( + '--append', + action='store_true', + help='Action: append message to the pad' + ) + + args = p.parse_args(args) + + with open(args.padinfo) as f: + info = json.load(f) + apiurl = info.get('apiurl') + + data = {} + data['apikey'] = info['apikey'] + data['padID'] = args.padid + + requesturl = apiurl+'getText?'+urlencode(data) + resp = urlopen(requesturl).read() + resp = resp.decode('utf-8') + results = json.loads(resp) + + if results['code'] != 0 and results['data'] is None: + print("Unknown pad with PadId '{}'".format(args.padid)) + sys.exit(-1) + + message_already_present = args.message in results['data']['text'] + if message_already_present and args.noduplicate: + print('--noduplicate passed and message already present. Stopping.') + return + + # Handle things like \n correctly when passing into the pad content + escaped_message = codecs.decode(str(args.message), 'unicode_escape') + + if args.prepend: + data['text'] = escaped_message + results['data']['text'] + elif args.append: + data['text'] = results['data']['text'] + escaped_message + else: + print(( + 'No action specified. See `etherdump addtext ' + '--help` for more information about actions.' + )) + sys.exit(-1) + + requesturl = apiurl + 'setText' + results = requests.post(requesturl, data=data) + results = json.loads(results.text) + + if results['code'] != 0: + message = 'addtext: ERROR ({0}) on pad {1}: {2}' + print(message.format(results['code'], args.padid, results['message'])) + sys.exit(-1)