Luke Murphy
6 years ago
2 changed files with 102 additions and 0 deletions
@ -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…
Reference in new issue