Last active
September 8, 2019 18:54
-
-
Save calumtomeny/3c36ba7b6986c9e149e28c5b95e75aec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
from inky import InkyWHAT | |
from PIL import Image, ImageFont, ImageDraw | |
from font_fredoka_one import FredokaOne | |
import requests | |
import sched | |
import time | |
def get_latest_price(currency): | |
BITCOIN_API_URL = 'https://api.coinmarketcap.com/v1/ticker/' + currency + '/' | |
response = requests.get(BITCOIN_API_URL) | |
response_json = response.json() | |
return '$' + '{:.2f}'.format(round(float(response_json[0]['price_usd']))) | |
def render_ticker(bitcoin_price, eth_price): | |
bitcoinPrice = bitcoin_price | |
ethPrice = eth_price | |
inky_display = InkyWHAT('yellow') | |
inky_display.set_border(inky_display.WHITE) | |
img = Image.open('//home/pi/Documents/btc-eth.png') | |
draw = ImageDraw.Draw(img) | |
font = ImageFont.truetype(FredokaOne, 50) | |
x = 160 | |
wid = inky_display.WIDTH | |
(btcw, btch) = font.getsize(bitcoinPrice) | |
btcy = inky_display.HEIGHT / 4 - btch / 2 | |
draw.text((x, btcy), bitcoinPrice, inky_display.BLACK, font) | |
(ethw, ethh) = font.getsize(ethPrice) | |
ethy = inky_display.HEIGHT - inky_display.HEIGHT / 4 - ethh / 2 | |
draw.text((x, ethy), ethPrice, inky_display.BLACK, font) | |
rotated_image = img.rotate(180) | |
inky_display.set_image(rotated_image) | |
inky_display.show() | |
def show_error(): | |
message = 'Error...' | |
inky_display = InkyWHAT('yellow') | |
img = Image.new("P", (inky_display.WIDTH, inky_display.HEIGHT)) | |
draw = ImageDraw.Draw(img) | |
font = ImageFont.truetype(FredokaOne, 50) | |
(w, h) = font.getsize(message) | |
x = inky_display.WIDTH / 2 - w / 2 | |
y = inky_display.HEIGHT / 2 - h / 2 | |
draw.text((x, y), message, inky_display.BLACK, font) | |
rotated_image = img.rotate(180) | |
inky_display.set_image(rotated_image) | |
inky_display.show() | |
starttime = time.time() | |
previous_bitcoin_price = '' | |
previous_eth_price = '' | |
while True: | |
try: | |
current_bitcoin_price = get_latest_price('bitcoin') | |
current_eth_price = get_latest_price('ethereum') | |
if current_bitcoin_price != previous_bitcoin_price or current_eth_price != previous_eth_price: | |
render_ticker(current_bitcoin_price, current_eth_price) | |
else: | |
print('Skipping duplicate render') | |
previous_bitcoin_price = current_bitcoin_price | |
previous_eth_price = current_eth_price | |
time.sleep(60.0 - (time.time() - starttime) % 60.0) | |
except: | |
show_error() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment