Execute a script that triggers a notification at blocknotify

Execute a script that triggers a notification at blocknotify

Ok, I'm running a bitcoin core 0.16.3 full node at my mac book pro laptop and my idea is to issue a system notification every time my node gets a new block.

Notification sample

For that I wrote down this simple python script.

import argparse
import sys
import os
from bitcoin.rpc import Proxy, InWarmupError
import logging

# Setup log
logging.basicConfig(filename='/Users/nelson/Logs/best_block.log',level=logging.DEBUG)

# Specify your own configuration file path
CONFIG_FILE = '/Volumes/External/Blockchain/bitcoin.conf'

# Parsing script argumnts
parser = argparse.ArgumentParser(description='Process a bitcoin best block notification')
parser.add_argument('blockhash')
args = parser.parse_args()

# Retrieving the block hash
blockhash = args.blockhash

def notify(title, subtitle, message):
    cmd = "osascript -e 'display notification \"{}\" with title \"{}\" subtitle\"{}\" sound name \"Glass\"'".format(message, title, subtitle)
    r = os.system(cmd)
    logging.debug('cmd returned: {}'.format(r))

proxy = Proxy(btc_conf_file=CONFIG_FILE)
try:
    block = proxy.call('getblock', blockhash)
    # Calculating the segwit ratio
    segwit_ratio = float(block['size']) / float(block['strippedsize'])

    title = 'Block {} found'.format(block['height'])
    subtitle = 'Transaction count: {}'.format(len(block['tx']))
    message = 'Segwit ratio: {:.2f}'.format(segwit_ratio)

    print('About to notify about block with hash {}'.format(blockhash))
    notify(title, subtitle, message)
    logging.debug('Issued notification about block: {}'.format(blockhash))
except InWarmupError:
    print('InWarmupError. The node is bootstrapping')

Now I should only have to add the following line to the bitcoin.conf:

blocknotify=python /Users/my_home/bin/best_block.py %s

The problem is that I never get the notification to show up. I know the script is being called and apparently runs correctly, since I can check the log file entries produced after the notify call.

Also if I try to just run the script from the command line by typing:

python ~/bin/best_block.py 0000000000000000001cdaf29fcb1b09eb46585cb30f3b721f8221d8d7d85927

I do get the notification. It is only when the bitcoin-core software calls it running in daemon mode that nothing happens. I wonder why it's not working. Maybe is it because the full node is in daemon mode?

I understand this question probably relates more to the apple stack exchange forum, but I figured it could also have something to do with the bitcoin-core software somehow, that's why I'm posting it here.

https://ift.tt/2PrY19n

Comments

Popular posts from this blog

Bitcoin Core errors with database block

Bitfinex & Market Synergy To Offer Institutional Calibre Cryptocurrency Connectivity