UBF -- The Ultimate Bitmessage Forum

User info

Welcome, Guest! Please login or register.


You are here » UBF -- The Ultimate Bitmessage Forum » BM API Client » post BM from bash-shell with python code


post BM from bash-shell with python code

Posts 1 to 2 of 2

1

you need:  :cool:

a  p.jpg  picture file
the shell file
the .py file
the mhtm file for any descriptive text in the BM

:hobo:
edit the keys.dat file beforehand!

to post a BM from the dolphin filemanager:

launch .sh from dolphin while BM is running!
:idea:



.sh file   pic.sh  :

-------

Code:
#!/bin/bash

# 
# python2.7  ./pic-master-5.py   -h

# Usage: pic-master-5.py [options] [message] --help -h for CLI params  -- open .py file for details ;-) 
# 
# Options:
#   -h, --help   show this help message and exit
#   -a HOST      Api host (default 'localhost')
#   -i PORT      Api port (default 8442)
#   -u USERNAME  Api username (default 'username')
#   -p PASSWORD  Api password (default 'password')
#   -f FROMADDR  'From' address (default 'BM-
#                2cW67GEKkHGonXKZLCzouLLxnLym3azS8r')
#   -t TOADDR    'To'   address (default 'BM-
#                2cW67GEKkHGonXKZLCzouLLxnLym3azS8r')
#   -s SUBJECT   Message subject
#   -e IMGFILE   Embed an image
#   --se         Append embedded filename to the subject
#   -m MSGFILE   Append the file content
#   -n           Allow empty message
#   -l TTL       Message TTL in hours (default 96)
#   -w           Wait until the message is sent out
#   -c ACKDATA   Check message status
# 



# use htm file or use plain text:   -m mfile

# uses ./p.jpg by default , html placed below pic in the final bitmessage




# python2.7  ./pic-master-5.py   -s 'BM subject line in here' -m mhtm 


  python2.7  ./pic-master-5.py   -s 'cool pics and stuff' -m mhtm

.py file  picmaster.py

Code:
#!/usr/bin/env python



#  usage:    python2.7 ./pic-master-5.py  -e ./pic1.jpg 

#  xterm CLI only, no launch from dolphin poss.    :-(   ...  except run with pythonw (wine version) does work here for some weird reason

#  yields:   No JSON object could be decoded  -- check all params!  usually this py does work.
#  make sure port# 8442 is active

#  watch ~/.config/PyBitmessage/keys.dat 

# keys.dat must include:

# apienabled = true
# apiport = 8442
# apiinterface = 127.0.0.1
# apiusername = Picmaster
# apipassword = Masterpic

#  see also https://bitmessage.org/wiki/API_Reference



import xmlrpclib, json, sys, os, time
from optparse import OptionParser

parser = OptionParser(usage = "Usage: %prog [options] [message] --help -h for CLI params  -- open .py file for details ;-) ")

parser.add_option("-a",   dest = "host",     default = "localhost",                             help = "Api host (default 'localhost')")
parser.add_option("-i",   dest = "port",     default = 8442, type = "int",                      help = "Api port (default 8442)")
parser.add_option("-u",   dest = "username", default = "Picmaster",                              help = "Api username (default 'username')")
parser.add_option("-p",   dest = "password", default = "Masterpic",                              help = "Api password (default 'password')")
parser.add_option("-f",   dest = "fromaddr", default = "BM-2cW67GEKkHGonXKZLCzouLLxnLym3azS8r", help = "'From' address (default 'BM-2cW67GEKkHGonXKZLCzouLLxnLym3azS8r')")
parser.add_option("-t",   dest = "toaddr",   default = "BM-2cW67GEKkHGonXKZLCzouLLxnLym3azS8r", help = "'To'   address (default 'BM-2cW67GEKkHGonXKZLCzouLLxnLym3azS8r')")
parser.add_option("-s",   dest = "subject",  default = "pic inside",                             help = "Message subject")

parser.add_option("-e",   dest = "imgfile",  default = "./p.jpg",                                help = "Embed an image")
## actual filename e.g.   pic1.jpg

parser.add_option("--se", dest = "imgname",  default = False, action = "store_true",            help = "Append embedded filename to the subject")
parser.add_option("-m",   dest = "msgfile",  default = "",                                      help = "Append the file content")
parser.add_option("-n",   dest = "empty",    default = False, action = "store_true",            help = "Allow empty message")
parser.add_option("-l",   dest = "ttl",      default = 4, type = "int",                          help = "Message TTL in hours (default 96)")
parser.add_option("-w",   dest = "wait",     default = False, action = "store_true",            help = "Wait until the message is sent out")
parser.add_option("-c",   dest = "ackdata",  default = "",                                      help = "Check message status")

(options, args) = parser.parse_args()


api = xmlrpclib.ServerProxy('http://%s:%s@%s:%d/' % (options.username, options.password, options.host, options.port))

if len(options.ackdata):
  try:                   print api.getStatus(options.ackdata); sys.exit()
  except Exception as e: print str(e); sys.exit(1)


if not len(options.fromaddr)*len(options.toaddr): parser.print_help(); sys.exit()

try:                   jsonAddresses = json.loads(api.listAddresses())
except Exception as e: print str(e); sys.exit(2)

lookup = next((item for item in jsonAddresses['addresses'] if item['address'] == options.fromaddr), False)
if not lookup:            print "Error: The 'From' address %s does not exist" % options.fromaddr; sys.exit(3)
if not lookup['enabled']: print "Error: The 'From' address %s exists but isn't enabled" % options.fromaddr; sys.exit(4)

msg = ''
if len(args): msg += args[0]
subj = options.subject

if len(options.imgfile):
  try:
    with open(options.imgfile, 'rb') as f:
      if len(msg): msg += '\n<br/>'
      msg += '<img src="data:image/%s;base64,%s">' % (os.path.splitext(options.imgfile)[1][1:].lower(), f.read().encode('base64').replace('\n', ''))
      if options.imgname: subj += os.path.basename(options.imgfile)
  except Exception as e: print str(e); sys.exit(5)

if len(options.msgfile):
  try:
    with open(options.msgfile, 'rb') as f:
      if len(options.imgfile): msg += '<br/>\n'
      msg += f.read()
  except Exception as e: print str(e); sys.exit(6)

if not options.empty and not len(subj+msg): print "Error: Empty message not allowed"; sys.exit(7)

try:                   ackData = api.sendMessage(options.toaddr, options.fromaddr, subj.encode('base64'), msg.encode('base64'), 2, options.ttl*60*60)
except Exception as e: print str(e); sys.exit(8)

print ackData
while options.wait and api.getStatus(ackData).find('msgsent'): time.sleep(1)

2

of course, no # in keys.dat for the entries, e.g.   apienabled = true

# is only need as escape code in the bash shell file


#  watch ~/.config/PyBitmessage/keys.dat

# keys.dat must include:

apienabled = true
apiport = 8442
apiinterface = 127.0.0.1
apiusername = Picmaster
apipassword = Masterpic

#  see also https://bitmessage.org/wiki/API_Reference

Last edited by user3 (Mar 10 2016 07:58)


You are here » UBF -- The Ultimate Bitmessage Forum » BM API Client » post BM from bash-shell with python code