Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BitBay ticker library for using with some projects #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions BitBay-GTK.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from bitbay import BitBay
import time

class ComboBoxWindow(Gtk.Window):

def __init__(self):
self.bb=BitBay()
self.fields=["time","bid", "last", "ask", "volume", "average", "max", "min", "vwap"]
self.pair=''
Gtk.Window.__init__(self, title="BitBay Tickers")

self.set_default_size(200, 200)
self.set_border_width(10)

vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)

country_store = Gtk.ListStore(str)
countries = self.bb.getPairs()
for country in countries:
country_store.append([country])

country_combo = Gtk.ComboBox.new_with_model(country_store)
country_combo.connect("changed", self.on_country_combo_changed)
renderer_text = Gtk.CellRendererText()
country_combo.pack_start(renderer_text, True)
country_combo.add_attribute(renderer_text, "text", 0)
vbox.pack_start(country_combo, True, False, False)

self.liststore = Gtk.ListStore(str, str)
for field in self.fields:
self.liststore.append([field, ""])

treeview = Gtk.TreeView(model=self.liststore)

renderer_text = Gtk.CellRendererText()
column_text = Gtk.TreeViewColumn("Type", renderer_text, text=0)
treeview.append_column(column_text)


column_text = Gtk.TreeViewColumn("Value", renderer_text, text=1)
treeview.append_column(column_text)

vbox.pack_end(treeview, True, True, True)

self.button1 = Gtk.Button(label="Refresh")
self.button1.connect("clicked", self.on_button1_clicked)
vbox.pack_end(self.button1, True, True, 6)

self.add(vbox)

def on_country_combo_changed(self, combo):
tree_iter = combo.get_active_iter()
if tree_iter != None:
model = combo.get_model()
self.pair = model[tree_iter][0]
currencies = self.pair.split('/')
ticker = self.bb.getTicker(currencies[0], currencies[1])
self.liststore[0][1] = time.ctime(self.bb.lastTicker(currencies[0], currencies[1]))
i=1
for field in self.fields[1:]:
self.liststore[i][1] = str(ticker[field])
i+=1

def on_button1_clicked(self, widget):
currencies = self.pair.split('/')
ticker = self.bb.getTicker(currencies[0], currencies[1])
self.liststore[0][1] = time.ctime(self.bb.lastTicker(currencies[0], currencies[1]))
i=1
for field in self.fields[1:]:
self.liststore[i][1] = str(ticker[field])
i+=1

win = ComboBoxWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
1 change: 1 addition & 0 deletions BitBay-tickers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"DASHPLN": {"1513964834.855555": {"average": 4290.99, "bid": 4265.01, "max": 5468.26, "ask": 4314.78, "vwap": 4290.99, "volume": 1611.44081936, "min": 3139.79, "last": 4290.99}}, "BTCEUR": {"1513980726.9050183": {"average": 12800, "bid": 12812.81, "max": 14900, "vwap": 12800, "ask": 12999.9, "volume": 104.54398612, "min": 11108.32, "last": 12800}, "1513967635.2083585": {"average": 11980, "bid": 11711.71, "max": 14900, "ask": 11997.93, "vwap": 11980, "volume": 163.27606252, "min": 11108.32, "last": 11980}}, "BTCUSD": {"1513967741.9930544": {"average": 16199.99, "bid": 14000.52, "max": 17199, "ask": 16305.11, "vwap": 16199.99, "volume": 46.79281563, "min": 12500, "last": 16199.99}, "1513980731.2115457": {"average": 15330, "bid": 15400.01, "max": 17199, "vwap": 15330, "ask": 15996.79, "volume": 44.67381585, "min": 12500, "last": 15330}}, "LTCBTC": {"1513119093.8816297": {"average": 0.0186499, "bid": 0.01785001, "max": 0.02101045, "ask": 0.01864, "vwap": 0.0186499, "volume": 1762.14218292, "min": 0.01201, "last": 0.0186499}, "1513120091.2682283": {"average": 0.01729988, "bid": 0.01729988, "max": 0.02101045, "ask": 0.01862994, "vwap": 0.01729988, "volume": 1658.90784096, "min": 0.01201, "last": 0.01729988}, "1513964848.056593": {"average": 0.01852646, "bid": 0.01852646, "max": 0.01999996, "ask": 0.01893983, "vwap": 0.01852646, "volume": 828.60245772, "min": 0.01425704, "last": 0.01852646}}, "GAMEPLN": {"1513964883.7563627": {"average": 12.3, "bid": 12.27, "max": 17.2, "ask": 12.3, "vwap": 12.3, "volume": 1388214.35172748, "min": 9.01, "last": 12.3}}, "GAMEBTC": {"1513964878.6722872": {"average": 0.00026175, "bid": 0.000228, "max": 0.00030899, "ask": 0.00026295, "vwap": 0.00026175, "volume": 14781.92560259, "min": 0.00022614, "last": 0.00026175}}, "LTCPLN": {"1513254501.3564787": {"average": 1050, "bid": 1049.01, "max": 1212.99, "ask": 1050, "vwap": 1050, "volume": 22088.17248932, "min": 1020, "last": 1050}, "1513119022.2100961": {"average": 1120, "bid": 1113, "max": 1349, "ask": 1119.99, "vwap": 1120, "volume": 75753.70886091, "min": 707, "last": 1120}, "1513243908.3803074": {"average": 1061, "bid": 1061, "max": 1212.99, "ask": 1065.9, "vwap": 1061, "volume": 24391.72650559, "min": 1020, "last": 1061}, "1513120144.1406722": {"average": 1060, "bid": 1054.01, "max": 1349, "ask": 1060, "vwap": 1060, "volume": 74735.05070664, "min": 707, "last": 1060}, "1513120810.3605087": {"average": 1062.05, "bid": 1062.04, "max": 1349, "ask": 1065, "vwap": 1062.05, "volume": 74022.87231767, "min": 707, "last": 1062.05}, "1513210959.4517872": {"average": 1062, "bid": 1056.08, "max": 1212.99, "ask": 1065.8, "vwap": 1062, "volume": 25862.76388144, "min": 1014, "last": 1062}}, "BCCPLN": {"1513964047.798809": {"average": 9499, "bid": 9421, "max": 12299, "ask": 9499, "vwap": 9499, "volume": 3233.3118255, "min": 5800, "last": 9499}, "1513963727.23231": {"average": 9750, "bid": 9740, "max": 12299, "ask": 9789.94, "vwap": 9750, "volume": 3213.04662194, "min": 5800, "last": 9750}, "1513964380.513414": {"average": 9445, "bid": 9445, "max": 12299, "ask": 9498, "vwap": 9445, "volume": 3233.17646656, "min": 5800, "last": 9445}, "1513980720.0320268": {"average": 10428, "bid": 10220.22, "max": 12299, "vwap": 10428, "ask": 10428, "volume": 3627.11640526, "min": 5800, "last": 10428}, "1513963628.3047826": {"average": 9700, "bid": 9610, "max": 12299, "ask": 9700, "vwap": 9700, "volume": 3211.60826922, "min": 5800, "last": 9700}, "1513967703.9472706": {"average": 9500, "bid": 9496, "max": 12299, "ask": 9528.85, "vwap": 9500, "volume": 3253.20555461, "min": 5800, "last": 9500}}, "BTCPLN": {"1512934753.3715508": {"average": 54320, "bid": 54302.09, "max": 54950, "ask": 54320, "vwap": 54320, "volume": 1879.9352839, "min": 47400, "last": 54320}, "1512935353.299335": {"average": 54277, "bid": 54055.03, "max": 54950, "ask": 54597, "vwap": 54277, "volume": 1888.65291537, "min": 47400, "last": 54277}, "1512944020.6247249": {"average": 51930, "bid": 51901.02, "max": 54950, "ask": 52380, "vwap": 51930, "volume": 1935.20712159, "min": 47400, "last": 51930}, "1513089599.7244968": {"average": 59980, "bid": 59702, "max": 61999, "ask": 59980, "vwap": 59980, "volume": 2257.59510453, "min": 56890, "last": 59980}, "1513967725.9274917": {"average": 50047.14, "bid": 49999.99, "max": 59500, "ask": 50047.14, "vwap": 50047.14, "volume": 3349.48411513, "min": 38000, "last": 50047.14}, "1512935091.5601368": {"average": 54320.02, "bid": 54320.02, "max": 54950, "ask": 54350, "vwap": 54320.02, "volume": 1884.03934476, "min": 47400, "last": 54320.02}, "1512944354.5701492": {"average": 52000, "bid": 52000, "max": 54950, "ask": 52015.54, "vwap": 52000, "volume": 1929.04640157, "min": 47400, "last": 52000}, "1513853643.933944": {"average": 60900, "bid": 60502, "max": 66100, "ask": 60900, "vwap": 60900, "volume": 1428.27311486, "min": 58654.01, "last": 60900}, "1513859447.2961614": {"average": 61198.99, "bid": 61198.99, "max": 66100, "ask": 61199, "vwap": 61198.99, "volume": 1510.70252929, "min": 58654.01, "last": 61198.99}, "1513210939.099998": {"average": 58860, "bid": 58726.87, "max": 60689, "ask": 58864.35, "vwap": 58860, "volume": 1609.71460506, "min": 58500, "last": 58860}, "1512935692.9962654": {"average": 54565, "bid": 54300, "max": 54950, "ask": 54581, "vwap": 54565, "volume": 1894.58385488, "min": 47400, "last": 54565}, "1513254440.2547865": {"average": 59350.87, "bid": 59347, "max": 60689, "ask": 59359.38, "vwap": 59350.87, "volume": 1652.50006636, "min": 57120, "last": 59350.87}, "1512861452.4483418": {"average": 51945, "bid": 51945, "max": 56499, "ask": 51951, "vwap": 51945, "volume": 2031.54679127, "min": 45611, "last": 51945}, "1513118536.2035773": {"average": 60300, "bid": 60300, "max": 60748, "ask": 60340, "vwap": 60300, "volume": 1613.69331158, "min": 58008, "last": 60300}, "1513084414.2564704": {"average": 59188, "bid": 59100, "max": 61999, "ask": 59188, "vwap": 59188, "volume": 2256.11127897, "min": 56495.6, "last": 59188}, "1512936072.3606625": {"average": 54450, "bid": 54420.02, "max": 54950, "ask": 54500, "vwap": 54450, "volume": 1898.38844013, "min": 47400, "last": 54450}, "1513243917.3096995": {"average": 59173, "bid": 59172, "max": 60689, "ask": 59300, "vwap": 59173, "volume": 1540.75671004, "min": 57120, "last": 59173}, "1512943201.140105": {"average": 51900, "bid": 51900, "max": 54950, "ask": 51901, "vwap": 51900, "volume": 1918.55372597, "min": 47400, "last": 51900}, "1513089534.6641026": {"average": 59700, "bid": 59700, "max": 61999, "ask": 59990, "vwap": 59700, "volume": 2258.63732289, "min": 56890, "last": 59700}, "1513114660.688006": {"average": 59612, "bid": 59611, "max": 61800, "ask": 59798, "vwap": 59612, "volume": 1605.72006871, "min": 58008, "last": 59612}, "1512934568.3819125": {"average": 54300.01, "bid": 54300.01, "max": 54950, "ask": 54349.98, "vwap": 54300.01, "volume": 1881.73837104, "min": 47400, "last": 54300.01}, "1513967578.4293454": {"average": 49899, "bid": 49601.11, "max": 59500, "ask": 49899, "vwap": 49899, "volume": 3346.42299435, "min": 38000, "last": 49899}, "1512935873.7919536": {"average": 54320, "bid": 54549, "max": 54950, "ask": 54550, "vwap": 54320, "volume": 1899.09152037, "min": 47400, "last": 54320}, "1512934381.5653105": {"average": 54198.99, "bid": 53700, "max": 54950, "ask": 54199, "vwap": 54198.99, "volume": 1878.73945506, "min": 47400, "last": 54198.99}, "1512863662.9968092": {"average": 52212.9, "bid": 52199, "max": 56499, "ask": 52223.63, "vwap": 52212.9, "volume": 2027.41403701, "min": 45611, "last": 52212.9}, "1512944192.913413": {"average": 51901, "bid": 51905.69, "max": 54950, "ask": 51943.84, "vwap": 51901, "volume": 1933.89780182, "min": 47400, "last": 51901}, "1513964399.6672268": {"average": 49335, "bid": 49335, "max": 59500, "ask": 49850, "vwap": 49335, "volume": 3303.68831985, "min": 38000, "last": 49335}, "1513854399.187152": {"average": 60520, "bid": 60550, "max": 66100, "ask": 60850, "vwap": 60520, "volume": 1438.97525527, "min": 58654.01, "last": 60520}, "1512934866.854223": {"average": 54500, "bid": 54310, "max": 54950, "ask": 54500, "vwap": 54500, "volume": 1884.26654957, "min": 47400, "last": 54500}, "1512935458.8255858": {"average": 54597, "bid": 54580, "max": 54950, "ask": 54597, "vwap": 54597, "volume": 1888.05190667, "min": 47400, "last": 54597}, "1513853931.4990296": {"average": 60750, "bid": 60100, "max": 66100, "ask": 60750, "vwap": 60750, "volume": 1438.83358703, "min": 58654.01, "last": 60750}, "1512949942.3607037": {"average": 54003, "bid": 54001.03, "max": 55499, "ask": 54099, "vwap": 54003, "volume": 2085.24600584, "min": 47400, "last": 54003}, "1512777675.6288135": {"average": 55303.36, "bid": 55307.19, "max": 59500, "ask": 55491, "vwap": 55303.36, "volume": 3732.71958266, "min": 49500, "last": 55303.36}, "1512861683.6988416": {"average": 52000, "bid": 51999.99, "max": 56499, "ask": 52000, "vwap": 52000, "volume": 2033.33218708, "min": 45611, "last": 52000}, "1512777565.7712953": {"average": 55373.6, "bid": 55366.04, "max": 59500, "ask": 55498.01, "vwap": 55373.6, "volume": 3710.34687004, "min": 49500, "last": 55373.6}, "1512944128.2826393": {"average": 51901, "bid": 51900, "max": 54950, "ask": 51940.28, "vwap": 51901, "volume": 1933.71132474, "min": 47400, "last": 51901}, "1512861930.4446862": {"average": 52089, "bid": 52048.99, "max": 56499, "ask": 52089, "vwap": 52089, "volume": 2034.24360027, "min": 45611, "last": 52089}, "1513856853.6560996": {"average": 60996, "bid": 60500.01, "max": 66100, "ask": 60996, "vwap": 60996, "volume": 1501.36667237, "min": 58654.01, "last": 60996}, "1512934309.5180244": {"average": 54000, "bid": 53200.02, "max": 54950, "ask": 54000, "vwap": 54000, "volume": 1876.57928727, "min": 47400, "last": 54000}, "1512861578.1633134": {"average": 51995.88, "bid": 51995.88, "max": 56499, "ask": 51999.99, "vwap": 51995.88, "volume": 2032.41693393, "min": 45611, "last": 51995.88}, "1513112610.9610164": {"average": 59900, "bid": 59900, "max": 61999, "ask": 60000, "vwap": 59900, "volume": 1732.10190537, "min": 58008, "last": 59900}, "1513856145.639456": {"average": 60988, "bid": 60860, "max": 66100, "ask": 60950, "vwap": 60988, "volume": 1476.86106949, "min": 58654.01, "last": 60988}, "1513111608.761247": {"average": 60498, "bid": 60150, "max": 61999, "ask": 60498, "vwap": 60498, "volume": 1793.45804612, "min": 58008, "last": 60498}, "1513980742.1774187": {"average": 53550, "bid": 53500.01, "max": 59500, "vwap": 53550, "ask": 53582, "volume": 3486.5219674, "min": 38000, "last": 53550}, "1513967664.8510542": {"average": 49955.34, "bid": 49602.01, "max": 59500, "ask": 49955.34, "vwap": 49955.34, "volume": 3340.70435172, "min": 38000, "last": 49955.34}, "1513964825.7961595": {"average": 49278, "bid": 49050, "max": 59500, "ask": 49278, "vwap": 49278, "volume": 3283.64619596, "min": 38000, "last": 49278}, "1512935554.0673604": {"average": 54581, "bid": 54580, "max": 54950, "ask": 54581, "vwap": 54581, "volume": 1889.09818361, "min": 47400, "last": 54581}, "1513980816.95353": {"average": 53579, "bid": 53351, "max": 59500, "vwap": 53579, "ask": 53580.99, "volume": 3486.74973393, "min": 38000, "last": 53579}, "1512935944.2664237": {"average": 54520, "bid": 54370, "max": 54950, "ask": 54549.9, "vwap": 54520, "volume": 1899.59813602, "min": 47400, "last": 54520}, "1512782354.3529177": {"average": 55500, "bid": 55468.98, "max": 59500, "ask": 55500, "vwap": 55500, "volume": 3698.4579296, "min": 49500, "last": 55500}}}
80 changes: 80 additions & 0 deletions bitbay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# -*- coding: utf-8 -*-
import sys
import time
import urllib.request
import json
import os

class BitBay(object):
def __init__(self):
self.baseLink='https://bitbay.net/API/Public/'
self.interval=60
self.tickerPairs=dict(BTC=['PLN','USD','EUR'])
self.tickerPairs['LTC']=list(self.tickerPairs['BTC'])+['BTC']
cryptos=('BTC','LTC','ETH','LSK','GAME','DASH','BCC')
for cr in cryptos[2:]:
self.tickerPairs[cr]=list(self.tickerPairs['LTC'])
try:
f=open('BitBay-tickers.json')
self.ticker = json.loads(f.read())
f.close()
except:
self.ticker = dict()
self.updateTicker()

def getPairs(self,currency=None):
if currency == None:
#trzeba uzyć wszystkich par
pairs=[]
for key in sorted(list(self.tickerPairs.keys())):
for k2 in self.tickerPairs[key]:
pairs.append("%s/%s" % (key,k2))
else:
if currency not in tickerPairs.keys:
return pairs
for k2 in self.tickerPairs[currency]:
pairs.append("%s/%s" % (currency,k2))
return pairs

def updateTicker(self):
f=open('BitBay-tickers.json','w')
f.write(json.dumps(self.ticker))
f.close()

def testUpToDate(self,curr1,curr2):
tname=curr1+curr2
if tname in self.ticker.keys():
#1 or more tickers
lt = float(sorted(list(self.ticker[tname]))[-1])
if time.time()-lt < self.interval:
return True
return False
else:
return False

def lastTicker(self,curr1,curr2):
tname=curr1+curr2
if tname not in self.ticker.keys():
return float(0)
tt = sorted(list(self.ticker[tname].keys()))[-1]
return float(tt)

def getTicker(self,curr1,curr2,force=False):
tname=curr1+curr2
url=self.baseLink + tname + "/ticker.json"
if force | self.testUpToDate(curr1,curr2) == False:
try:
with urllib.request.urlopen(url) as f:
bhtml=f.read()
html=bhtml.decode()
f.close()
except:
print('*')
tt=str(time.time())
if not (tname in self.ticker.keys()):
self.ticker[tname]=dict()
self.ticker[tname][tt]=json.loads(html)
self.updateTicker()
tt = sorted(list(self.ticker[tname].keys()))[-1]
return self.ticker[tname][tt]

Binary file added bitcoin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bitcoin125.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bitcoin64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bitcoin900.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.