Created
July 7, 2016 12:36
-
-
Save aybase/fa3db270b4aa356ae1a8f59e95caa3af to your computer and use it in GitHub Desktop.
Simple python crypto-currency ticker - Modified for use with Raspberry PI using 2.8" TFT display
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/env python | |
# Author Tom Fyuri. | |
# Pillaged by aybase ~07-2016 for use with Raspberry PI using 2.8" TFT display | |
# Simple bitcoin usd ticker (btc-e), you're free to modify or share following code however you like. | |
# Version 0.0.6. | |
import threading | |
import pygame | |
import httplib | |
import urllib | |
import json | |
import time | |
import sys, os | |
import copy | |
from pygame.locals import * | |
# setup some global stuff | |
pygame.init() | |
screen = pygame.display.set_mode((320,240),pygame.FULLSCREEN) | |
pygame.mouse.set_visible(False) | |
pygame.display.set_caption('BTC-e Live') | |
surface = pygame.Surface(screen.get_size()) | |
surface = surface.convert() | |
font = pygame.font.Font(None, 55) | |
font2 = pygame.font.Font(None, 55) | |
font3 = pygame.font.Font(None, 16) | |
update_time = 1500 | |
tickrate = 50 | |
class currency_data(): | |
pass | |
ticker_data = [["btc_usd","BTC"," $"," B",currency_data(),0], | |
["eth_usd","ETH"," $","L",currency_data(),0], | |
["eth_btc","ETH"," B","L",currency_data(),0]] | |
def quit(): | |
pygame.quit(); sys.exit() | |
def get_price(which): # | |
try: | |
conn = httplib.HTTPSConnection("btc-e.com", timeout=4) | |
conn.request("GET", "/api/2/"+which+"/ticker") | |
response = conn.getresponse() | |
j = json.load(response) | |
conn.close() | |
return j | |
except StandardError: | |
return None | |
def get_depth(which): # | |
try: | |
conn = httplib.HTTPSConnection("btc-e.com") | |
conn.request("GET", "/api/2/"+which+"/depth") | |
response = conn.getresponse() | |
j = json.load(response) | |
conn.close() | |
return j | |
except StandardError: | |
return None | |
def process_input(): | |
key = pygame.key.get_pressed() | |
for event in pygame.event.get(): | |
if event.type == KEYDOWN: | |
if event.key == K_ESCAPE: quit() | |
def update_data(): | |
# basic stuff | |
for i in ticker_data: | |
currency_name = i[0] | |
i[5] = copy.deepcopy(i[4]) # save previous? | |
pdata = i[5] | |
data = i[4] | |
data.name = i[1] | |
data.nom = i[2] | |
data.nom2 = i[3] | |
json_data = get_price(currency_name) | |
if json_data is None: | |
data.error = True | |
else: | |
data.error = False | |
data.last = json_data['ticker']['last'] | |
volume = 0 | |
#data.volume = json_data['ticker']['vol_cur'] | |
# more interesting thing, fattest walls | |
json_data = get_depth(currency_name) | |
if json_data is None: | |
data.error = True | |
else: | |
data.error = False | |
if ((hasattr(data,'error')) and (hasattr(pdata,'error')) and (data.error == False) and (pdata.error == False)): | |
if hasattr(data,'last_color'): | |
data.last_color = pdata.last_color | |
else: | |
# WHITE originally 255, 255, 255 | |
data.last_color = (242, 204, 133) | |
if (data.last > pdata.last): | |
# GREEN originally 0, 255, 0 | |
data.last_color = (49, 181, 49) | |
elif (data.last < pdata.last): | |
# RED originally 255, 0, 0 | |
data.last_color = (209, 29, 29) | |
def redraw(): | |
surface.fill((0, 0, 0)) | |
pos = 37; pos2 = 37; | |
for i in ticker_data: | |
data = i[4] | |
if data.error: | |
text = font.render("ERROR", 1, (44, 96, 209)) | |
text_pos = text.get_rect(); text_pos.y = pos; text_pos.x = 98 | |
surface.blit(text, text_pos) | |
else: | |
if (hasattr(data,'last_color')): | |
color = data.last_color | |
else: | |
color = (242, 204, 133) | |
text = font2.render("{0}{1}".format(round(data.last,5),data.nom), 1, color) | |
text_pos = text.get_rect(); text_pos.y = pos2; text_pos.x = 118 | |
surface.blit(text, text_pos) | |
# name | |
text = font.render(data.name, 1, (242, 204, 133)) | |
text_pos = text.get_rect(); text_pos.y = pos | |
text_pos.x = 15 | |
surface.blit(text, text_pos) | |
pos+=65 | |
pos2+=65 | |
screen.blit(surface, (0, 0)) | |
pygame.display.flip() | |
def main(): | |
clock = pygame.time.Clock() | |
update_delay = 0 | |
update_data() | |
redraw() | |
while True: | |
process_input() | |
update_delay = update_delay + tickrate | |
if (update_delay >= update_time): | |
update_delay = 0 | |
update_data() | |
redraw() | |
clock.tick(tickrate) | |
if __name__ == '__main__': main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment