Compare commits

...

4 Commits

Author SHA1 Message Date
Luke Murphy 008b204ca1
Add change log entry for new addtext command. 6 years ago
Luke Murphy e57e6c9b6b
Remove old command name and use new one. Sync -> Pull. 6 years ago
Luke Murphy e5987cdd12
Add the addtext command. 6 years ago
Luke Murphy 5b141f4d77
Ingore egg files. 6 years ago
  1. 1
      .gitignore
  2. 3
      README.md
  3. 3
      bin/etherdump
  4. 101
      etherdump/commands/addtext.py

1
.gitignore

@ -5,3 +5,4 @@ venv/
testing/ testing/
padinfo.json padinfo.json
.etherdump .etherdump
*.egg*

3
README.md

@ -60,6 +60,9 @@ Change log / notes
Originally designed for use at: [constant](http://etherdump.constantvzw.org/). Originally designed for use at: [constant](http://etherdump.constantvzw.org/).
18 Dec 2018
------------
Feature request from [Varia](https://vvvvvvaria.org/): Allow to add magic words to existing pads before archiving. See [issue #1](https://gitlab.constantvzw.org/aa/etherdump/issues/1) for more.
17 Oct 2016 17 Oct 2016
----------------------------------------------- -----------------------------------------------

3
bin/etherdump

@ -18,6 +18,7 @@ where CMD could be:
revisionscount revisionscount
showmeta showmeta
html5tidy html5tidy
addtext
For more information on each command try: For more information on each command try:
etherdump CMD --help etherdump CMD --help
@ -27,7 +28,7 @@ For more information on each command try:
try: try:
cmd = sys.argv[1] cmd = sys.argv[1]
if cmd.startswith("-"): if cmd.startswith("-"):
cmd = "sync" cmd = "pull"
args = sys.argv args = sys.argv
else: else:
args = sys.argv[2:] args = sys.argv[2:]

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