Skip to content

Instantly share code, notes, and snippets.

@spynappels
Created July 3, 2017 07:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spynappels/234660d0a41d906ba7306fab58c6532d to your computer and use it in GitHub Desktop.
Save spynappels/234660d0a41d906ba7306fab58c6532d to your computer and use it in GitHub Desktop.
A Python3 script to fetch and display the balance and value of a specified BTC wallet, as well as current USD price and percentage change since a specified date, defaulting to last entry in blockchain.info Market Price chart. Display used is Pimoroni inky pHAT.
#!/usr/bin/env python3
# Stefan Pynappels, 2017
# v0.1
# Due to the slow refresh rate of the Pimoroni inky pHAT, this is best called at
# intervals from cron. I use a 15min interval on my test system.
# Import pre-requisite modules
import json
import requests
from PIL import ImageFont
import inkyphat
import datetime
# Set variables
# replace with your own wallet ID (public key NOT private key)
wallet = "1PXLgcjt2SHtRTCLt1yyoqo69Gv6AocoJA"
balance_url = "https://blockchain.info/q/addressbalance/" + wallet
ticker_url = "https://blockchain.info/ticker"
old_price_url = "https://api.blockchain.info/charts/market-price?timespan=1days&format=json"
font_file = inkyphat.fonts.FredokaOne
font_size = 22
med_font_size = 18
small_font_size = 12
# Make API calls
balance = requests.get(balance_url)
ticker = requests.get(ticker_url).json()
prices = requests.get(old_price_url).json()
# Format data
# To see wallet value in local currency, replace currency code in price
# declaration below. Some examples include:
# GBP is British Pounds
# USD is US Dollars
# EUR is Euro
# JPY is Japanese Yen
# etc.....
btc = int(balance.text)/100000000
gbp_price = ticker['GBP']['15m']
usd_price = ticker['USD']['15m']
old_usd_price = prices['values'][0]['y']
old_timestamp = prices['values'][0]['x']
wal_val = btc*gbp_price
perc_up_dn = ((usd_price/old_usd_price) - 1)*100
bal_str = "Bal: " + str('{:.8f}'.format(btc)) + " BTC"
val_str = "Value £" + str('{:7.2f}'.format(wal_val))
usd_price_str = "$" + str('{:.2f}'.format(usd_price))
perc_str = str('{:.1f}'.format(perc_up_dn)) + "%"
date_string = str(datetime.datetime.fromtimestamp(int(old_timestamp)).strftime('%d/%m/%y %H:%M'))
# Actually display data on Pimoroni inky pHAT
top = 0
left = 0
text1 = str(bal_str).format(font_size)
font = inkyphat.ImageFont.truetype(font_file, font_size)
width, height = font.getsize(text1)
inkyphat.text((0, top), text1, 1, font=font)
top += height + 5
text2 = str(val_str).format(font_size)
font = inkyphat.ImageFont.truetype(font_file, font_size)
width, height = font.getsize(text2)
inkyphat.text((0, top), text2, 1, font=font)
top += height + 5
top_offset = top
text3 = "USD".format(small_font_size)
font = inkyphat.ImageFont.truetype(font_file, small_font_size)
width, height = font.getsize(text3)
inkyphat.text((0, top), text3, 1, font=font)
left_offset = left + width + 2
top += height + 2
text4 = "Price".format(small_font_size)
font = inkyphat.ImageFont.truetype(font_file, small_font_size)
width, height = font.getsize(text4)
inkyphat.text((0, top), text4, 1, font=font)
top += height + 2
left_offset = max(left_offset, left + width + 2)
text5 = str(usd_price_str).format(med_font_size)
font = inkyphat.ImageFont.truetype(font_file, med_font_size)
width, height = font.getsize(text5)
inkyphat.text((left_offset, top_offset), text5, 1, font=font)
left_offset = left_offset + width + 5
top = top_offset
left = left_offset
text6 = "%".format(small_font_size)
font = inkyphat.ImageFont.truetype(font_file, small_font_size)
width, height = font.getsize(text6)
inkyphat.text((left, top), text6, 1, font=font)
left_offset = left + width + 2
top += height
text7 = "up/dn".format(small_font_size)
font = inkyphat.ImageFont.truetype(font_file, small_font_size)
width, height = font.getsize(text7)
inkyphat.text((left, top), text7, 1, font=font)
top += height
left_offset = max(left_offset, left + width + 2)
text9 = "since".format(small_font_size)
font = inkyphat.ImageFont.truetype(font_file, small_font_size)
width, height = font.getsize(text9)
inkyphat.text((left, top), text9, 1, font=font)
top += height
text10 = date_string.format(small_font_size)
font = inkyphat.ImageFont.truetype(font_file, small_font_size)
width, height = font.getsize(text10)
inkyphat.text((left, top), text10, 1, font=font)
top = top_offset
left = left_offset
text8 = str(perc_str).format(font_size)
font = inkyphat.ImageFont.truetype(font_file, font_size)
width, height = font.getsize(text8)
inkyphat.text((left, top), text8, 2, font=font)
inkyphat.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment