Browse Source

Add the addtext command.

Closes  https://gitlab.constantvzw.org/aa/etherdump/issues/1.
add-new-addtext-command
Luke Murphy 6 years ago
parent
commit
e5987cdd12
No known key found for this signature in database GPG Key ID: 5E2EF5A63E3718CC
  1. 1
      bin/etherdump
  2. 101
      etherdump/commands/addtext.py

1
bin/etherdump

@ -18,6 +18,7 @@ where CMD could be:
revisionscount
showmeta
html5tidy
addtext
For more information on each command try:
etherdump CMD --help

101
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)
Loading…
Cancel
Save