From c055b6c7842a48029d3326742d93a4ed685636ac Mon Sep 17 00:00:00 2001 From: André Doser Date: Mon, 26 Jan 2015 18:50:06 +0100 Subject: Bitcoin price checker --- py3status/modules/bitcoin-price-checker.py | 102 +++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 py3status/modules/bitcoin-price-checker.py (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price-checker.py b/py3status/modules/bitcoin-price-checker.py new file mode 100644 index 0000000..3549866 --- /dev/null +++ b/py3status/modules/bitcoin-price-checker.py @@ -0,0 +1,102 @@ +# -*- coding: utf-8 -*- +""" +Module for displaying bitcoin prices. + +Written and contributed by @tasse: + Andre Doser +""" +import json +import urllib.request as ul +from time import time +from collections import defaultdict +import re + +lastPrices = defaultdict(float) + + +class Py3status: + # btc-e, api,bitfinex, bitstamp, bitpay + websites = 'btc-e, bitstamp' + value = 'last' + cache_timeout = 5 + + def __init__(self): + self._hoster = {'https://btc-e.com/api/2/btc_usd/ticker': self._get_btce, + 'https://bitstamp.net/api/ticker/': self._get_bitstamp, + 'https://api.bitfinex.com/v1/pubticker/BTCUSD': self._get_bitfinex, + 'https://bitpay.com/api/rates': self._get_bitpay} + + # returns the price for a given site in USD + # value is elem of {high, low, avg, last} + def _get_btce(self, url, value): + try: + data = json.loads(ul.urlopen(url).read().decode()) + # default is 'last' price + value = data['ticker'].get(value, data['ticker']['last']) + except: + value = 'N/A' + return 'BTC-e', value + + def _get_bitstamp(self, url, value): + # bitstamp API is different in the following + if value == 'avg': + value = 'vwap' + try: + data = json.loads(ul.urlopen(url).read().decode()) + value = data.get(value, data['last']) + except: + value = 'N/A' + return 'BS', value + + def _get_bitfinex(self, url, value): + # bitfinex API is different in the following + if value == 'avg': + value = 'mid' + if value == 'last': + value = 'last_price' + try: + data = json.loads(ul.urlopen(url).read().decode()) + value = data.get(value, data['last_price']) + except: + value = 'N/A' + return 'BF', value + + def _get_bitpay(self, url, value): + try: + data = json.loads(ul.urlopen(url).read().decode()) + value = [x['rate'] for x in data if x['code'] == 'USD'][0] + except: + value = 'N/A' + return 'BP', value + + def get_rate(self, i3s_output_list, i3s_config): + response = {'full_text': '', 'name': 'bitcoin rates', + 'cached_until': time() + self.cache_timeout} + rates = [] + cnt = 0 + for url, f in self._hoster.items(): + rgx = re.search('.*//(.*)\..*', url).group(1) + if rgx not in self.websites: + continue + name, rate = f(url, self.value) + rates.append('{}: '.format(name) + + ('N/A' if rate == 'N/A' + else ('{:.2f}$'.format(float(rate))))) + lastPrices[f] = rate + cnt += 1 + # don't color multiple sites + if cnt == 1: + if rate < lastPrices[f]: + response['color'] = i3s_config['color_bad'] + elif rate > lastPrices[f]: + response['color'] = i3s_config['color_good'] + response['full_text'] = ', '.join(rates) + return response + +if __name__ == '__main__': + from time import sleep + x = Py3status() + while True: + print(x.get_rate([], {'color_good': 'green', + 'color_bad': 'red'})) + sleep(1) -- cgit v1.3 From 4fdaa161e781e47330dea4513804c71a66dcb2d1 Mon Sep 17 00:00:00 2001 From: André Doser Date: Mon, 26 Jan 2015 19:05:07 +0100 Subject: last prices dict has to be global --- py3status/modules/bitcoin-price-checker.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price-checker.py b/py3status/modules/bitcoin-price-checker.py index 3549866..6562075 100644 --- a/py3status/modules/bitcoin-price-checker.py +++ b/py3status/modules/bitcoin-price-checker.py @@ -18,7 +18,7 @@ class Py3status: # btc-e, api,bitfinex, bitstamp, bitpay websites = 'btc-e, bitstamp' value = 'last' - cache_timeout = 5 + cache_timeout = 120 def __init__(self): self._hoster = {'https://btc-e.com/api/2/btc_usd/ticker': self._get_btce, @@ -74,6 +74,7 @@ class Py3status: 'cached_until': time() + self.cache_timeout} rates = [] cnt = 0 + global lastPrices for url, f in self._hoster.items(): rgx = re.search('.*//(.*)\..*', url).group(1) if rgx not in self.websites: -- cgit v1.3 From e49177929ebd39333a5a1cc0dbcfb46b740cef1f Mon Sep 17 00:00:00 2001 From: André Doser Date: Tue, 27 Jan 2015 10:58:53 +0100 Subject: fixed last_price field --- py3status/modules/bitcoin-price-checker.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price-checker.py b/py3status/modules/bitcoin-price-checker.py index 6562075..5425147 100644 --- a/py3status/modules/bitcoin-price-checker.py +++ b/py3status/modules/bitcoin-price-checker.py @@ -5,20 +5,23 @@ Module for displaying bitcoin prices. Written and contributed by @tasse: Andre Doser """ +# -*- coding: utf-8 -*- +""" +Module for displaying bitcoin prices. +""" import json import urllib.request as ul from time import time -from collections import defaultdict import re -lastPrices = defaultdict(float) +last_price = 0 class Py3status: # btc-e, api,bitfinex, bitstamp, bitpay - websites = 'btc-e, bitstamp' + websites = 'btc-e' value = 'last' - cache_timeout = 120 + cache_timeout = 1 def __init__(self): self._hoster = {'https://btc-e.com/api/2/btc_usd/ticker': self._get_btce, @@ -74,7 +77,6 @@ class Py3status: 'cached_until': time() + self.cache_timeout} rates = [] cnt = 0 - global lastPrices for url, f in self._hoster.items(): rgx = re.search('.*//(.*)\..*', url).group(1) if rgx not in self.websites: @@ -83,14 +85,17 @@ class Py3status: rates.append('{}: '.format(name) + ('N/A' if rate == 'N/A' else ('{:.2f}$'.format(float(rate))))) - lastPrices[f] = rate cnt += 1 # don't color multiple sites + global last_price if cnt == 1: - if rate < lastPrices[f]: + if last_price == 0: + pass + elif rate < last_price: response['color'] = i3s_config['color_bad'] - elif rate > lastPrices[f]: + elif rate > last_price: response['color'] = i3s_config['color_good'] + last_price = rate response['full_text'] = ', '.join(rates) return response -- cgit v1.3 From 27111e8806ca2101a07fb7aed23413c7c1a43474 Mon Sep 17 00:00:00 2001 From: André Doser Date: Tue, 27 Jan 2015 11:04:15 +0100 Subject: Changed default value of cache_timeout --- py3status/modules/bitcoin-price-checker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price-checker.py b/py3status/modules/bitcoin-price-checker.py index 5425147..f20db00 100644 --- a/py3status/modules/bitcoin-price-checker.py +++ b/py3status/modules/bitcoin-price-checker.py @@ -21,7 +21,7 @@ class Py3status: # btc-e, api,bitfinex, bitstamp, bitpay websites = 'btc-e' value = 'last' - cache_timeout = 1 + cache_timeout = 120 def __init__(self): self._hoster = {'https://btc-e.com/api/2/btc_usd/ticker': self._get_btce, -- cgit v1.3 From 347bf9d24e2dfd314c7a29d786cfd02c5452290c Mon Sep 17 00:00:00 2001 From: André Doser Date: Tue, 27 Jan 2015 16:45:41 +0100 Subject: Using bitcoincharts. --- py3status/modules/bitcoin-price-checker.py | 113 +++++++++++------------------ 1 file changed, 41 insertions(+), 72 deletions(-) (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price-checker.py b/py3status/modules/bitcoin-price-checker.py index f20db00..c717952 100644 --- a/py3status/modules/bitcoin-price-checker.py +++ b/py3status/modules/bitcoin-price-checker.py @@ -1,14 +1,11 @@ # -*- coding: utf-8 -*- """ -Module for displaying bitcoin prices. +Module for displaying bitcoin prices using +the API by www.bitcoincharts.com. Written and contributed by @tasse: Andre Doser """ -# -*- coding: utf-8 -*- -""" -Module for displaying bitcoin prices. -""" import json import urllib.request as ul from time import time @@ -18,77 +15,49 @@ last_price = 0 class Py3status: - # btc-e, api,bitfinex, bitstamp, bitpay - websites = 'btc-e' - value = 'last' - cache_timeout = 120 - - def __init__(self): - self._hoster = {'https://btc-e.com/api/2/btc_usd/ticker': self._get_btce, - 'https://bitstamp.net/api/ticker/': self._get_bitstamp, - 'https://api.bitfinex.com/v1/pubticker/BTCUSD': self._get_bitfinex, - 'https://bitpay.com/api/rates': self._get_bitpay} - - # returns the price for a given site in USD - # value is elem of {high, low, avg, last} - def _get_btce(self, url, value): - try: - data = json.loads(ul.urlopen(url).read().decode()) - # default is 'last' price - value = data['ticker'].get(value, data['ticker']['last']) - except: - value = 'N/A' - return 'BTC-e', value - - def _get_bitstamp(self, url, value): - # bitstamp API is different in the following - if value == 'avg': - value = 'vwap' - try: - data = json.loads(ul.urlopen(url).read().decode()) - value = data.get(value, data['last']) - except: - value = 'N/A' - return 'BS', value - - def _get_bitfinex(self, url, value): - # bitfinex API is different in the following - if value == 'avg': - value = 'mid' - if value == 'last': - value = 'last_price' - try: - data = json.loads(ul.urlopen(url).read().decode()) - value = data.get(value, data['last_price']) - except: - value = 'N/A' - return 'BF', value - - def _get_bitpay(self, url, value): - try: - data = json.loads(ul.urlopen(url).read().decode()) - value = [x['rate'] for x in data if x['code'] == 'USD'][0] - except: - value = 'N/A' - return 'BP', value + # possible markets see http://bitcoincharts.com/markets/list/ + markets = 'btceEUR, btcdeEUR' + field = 'close' + cache_timeout = 900 # bitcoincharts: load max. every 15 min + symbols = True + color_index = -1 + _url = 'http://api.bitcoincharts.com/v1/markets.json' + _map = {'EUR': '€', 'USD': '$', 'GBP': '£', 'YEN': '¥', 'CNY': '¥', + 'AUD': '$'} + # markets according to the following lists: + # + def _get_price(self, data, market, field): + for m in data: + if m['symbol'] == market: + return m[field] def get_rate(self, i3s_output_list, i3s_config): response = {'full_text': '', 'name': 'bitcoin rates', 'cached_until': time() + self.cache_timeout} - rates = [] - cnt = 0 - for url, f in self._hoster.items(): - rgx = re.search('.*//(.*)\..*', url).group(1) - if rgx not in self.websites: - continue - name, rate = f(url, self.value) - rates.append('{}: '.format(name) - + ('N/A' if rate == 'N/A' - else ('{:.2f}$'.format(float(rate))))) - cnt += 1 - # don't color multiple sites + # get the data from the bitcoincharts website + try: + data = json.loads(ul.urlopen(self._url).read().decode()) + except Exception: + response['color'] = i3s_config['color_bad'] + response['full_text'] = 'Bitcoincharts not reachable' + return response + # get the rate for each market given + rates, markets = [], self.markets.split(",") + color_rate = None + for i, market in enumerate(markets): + market = market.strip() + try: + rate = self._get_price(data, market, self.field) + if i == self.color_index: + color_rate = rate + except Exception: + pass + rates.append('{}: '.format((market[:-3] if rate else market)) + + ('N/A' if not rate + else ('{:.2f}{}'.format(rate, (self._map.get(market[-3:], market[-3:]) if self.symbols else market[-3:]))))) + # don't color multiple sites if no color_index is given global last_price - if cnt == 1: + if len(rates) == 1 or self.color_index >= -1: if last_price == 0: pass elif rate < last_price: @@ -105,4 +74,4 @@ if __name__ == '__main__': while True: print(x.get_rate([], {'color_good': 'green', 'color_bad': 'red'})) - sleep(1) + sleep(5) -- cgit v1.3 From 594de019f780bc5dc5ea77ca5d14e688cd3d3347 Mon Sep 17 00:00:00 2001 From: André Doser Date: Tue, 27 Jan 2015 16:46:34 +0100 Subject: renamed file --- py3status/modules/bitcoin-price-checker.py | 77 ------------------------------ py3status/modules/crypto-price-checker.py | 77 ++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 77 deletions(-) delete mode 100644 py3status/modules/bitcoin-price-checker.py create mode 100644 py3status/modules/crypto-price-checker.py (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price-checker.py b/py3status/modules/bitcoin-price-checker.py deleted file mode 100644 index c717952..0000000 --- a/py3status/modules/bitcoin-price-checker.py +++ /dev/null @@ -1,77 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Module for displaying bitcoin prices using -the API by www.bitcoincharts.com. - -Written and contributed by @tasse: - Andre Doser -""" -import json -import urllib.request as ul -from time import time -import re - -last_price = 0 - - -class Py3status: - # possible markets see http://bitcoincharts.com/markets/list/ - markets = 'btceEUR, btcdeEUR' - field = 'close' - cache_timeout = 900 # bitcoincharts: load max. every 15 min - symbols = True - color_index = -1 - _url = 'http://api.bitcoincharts.com/v1/markets.json' - _map = {'EUR': '€', 'USD': '$', 'GBP': '£', 'YEN': '¥', 'CNY': '¥', - 'AUD': '$'} - # markets according to the following lists: - # - def _get_price(self, data, market, field): - for m in data: - if m['symbol'] == market: - return m[field] - - def get_rate(self, i3s_output_list, i3s_config): - response = {'full_text': '', 'name': 'bitcoin rates', - 'cached_until': time() + self.cache_timeout} - # get the data from the bitcoincharts website - try: - data = json.loads(ul.urlopen(self._url).read().decode()) - except Exception: - response['color'] = i3s_config['color_bad'] - response['full_text'] = 'Bitcoincharts not reachable' - return response - # get the rate for each market given - rates, markets = [], self.markets.split(",") - color_rate = None - for i, market in enumerate(markets): - market = market.strip() - try: - rate = self._get_price(data, market, self.field) - if i == self.color_index: - color_rate = rate - except Exception: - pass - rates.append('{}: '.format((market[:-3] if rate else market)) - + ('N/A' if not rate - else ('{:.2f}{}'.format(rate, (self._map.get(market[-3:], market[-3:]) if self.symbols else market[-3:]))))) - # don't color multiple sites if no color_index is given - global last_price - if len(rates) == 1 or self.color_index >= -1: - if last_price == 0: - pass - elif rate < last_price: - response['color'] = i3s_config['color_bad'] - elif rate > last_price: - response['color'] = i3s_config['color_good'] - last_price = rate - response['full_text'] = ', '.join(rates) - return response - -if __name__ == '__main__': - from time import sleep - x = Py3status() - while True: - print(x.get_rate([], {'color_good': 'green', - 'color_bad': 'red'})) - sleep(5) diff --git a/py3status/modules/crypto-price-checker.py b/py3status/modules/crypto-price-checker.py new file mode 100644 index 0000000..c717952 --- /dev/null +++ b/py3status/modules/crypto-price-checker.py @@ -0,0 +1,77 @@ +# -*- coding: utf-8 -*- +""" +Module for displaying bitcoin prices using +the API by www.bitcoincharts.com. + +Written and contributed by @tasse: + Andre Doser +""" +import json +import urllib.request as ul +from time import time +import re + +last_price = 0 + + +class Py3status: + # possible markets see http://bitcoincharts.com/markets/list/ + markets = 'btceEUR, btcdeEUR' + field = 'close' + cache_timeout = 900 # bitcoincharts: load max. every 15 min + symbols = True + color_index = -1 + _url = 'http://api.bitcoincharts.com/v1/markets.json' + _map = {'EUR': '€', 'USD': '$', 'GBP': '£', 'YEN': '¥', 'CNY': '¥', + 'AUD': '$'} + # markets according to the following lists: + # + def _get_price(self, data, market, field): + for m in data: + if m['symbol'] == market: + return m[field] + + def get_rate(self, i3s_output_list, i3s_config): + response = {'full_text': '', 'name': 'bitcoin rates', + 'cached_until': time() + self.cache_timeout} + # get the data from the bitcoincharts website + try: + data = json.loads(ul.urlopen(self._url).read().decode()) + except Exception: + response['color'] = i3s_config['color_bad'] + response['full_text'] = 'Bitcoincharts not reachable' + return response + # get the rate for each market given + rates, markets = [], self.markets.split(",") + color_rate = None + for i, market in enumerate(markets): + market = market.strip() + try: + rate = self._get_price(data, market, self.field) + if i == self.color_index: + color_rate = rate + except Exception: + pass + rates.append('{}: '.format((market[:-3] if rate else market)) + + ('N/A' if not rate + else ('{:.2f}{}'.format(rate, (self._map.get(market[-3:], market[-3:]) if self.symbols else market[-3:]))))) + # don't color multiple sites if no color_index is given + global last_price + if len(rates) == 1 or self.color_index >= -1: + if last_price == 0: + pass + elif rate < last_price: + response['color'] = i3s_config['color_bad'] + elif rate > last_price: + response['color'] = i3s_config['color_good'] + last_price = rate + response['full_text'] = ', '.join(rates) + return response + +if __name__ == '__main__': + from time import sleep + x = Py3status() + while True: + print(x.get_rate([], {'color_good': 'green', + 'color_bad': 'red'})) + sleep(5) -- cgit v1.3 From 684376fa3b80c04570389964699a54557dff18b9 Mon Sep 17 00:00:00 2001 From: André Doser Date: Tue, 27 Jan 2015 16:49:59 +0100 Subject: renamed file --- py3status/modules/bitcoin-price.py | 77 +++++++++++++++++++++++++++++++ py3status/modules/crypto-price-checker.py | 77 ------------------------------- 2 files changed, 77 insertions(+), 77 deletions(-) create mode 100644 py3status/modules/bitcoin-price.py delete mode 100644 py3status/modules/crypto-price-checker.py (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price.py b/py3status/modules/bitcoin-price.py new file mode 100644 index 0000000..c717952 --- /dev/null +++ b/py3status/modules/bitcoin-price.py @@ -0,0 +1,77 @@ +# -*- coding: utf-8 -*- +""" +Module for displaying bitcoin prices using +the API by www.bitcoincharts.com. + +Written and contributed by @tasse: + Andre Doser +""" +import json +import urllib.request as ul +from time import time +import re + +last_price = 0 + + +class Py3status: + # possible markets see http://bitcoincharts.com/markets/list/ + markets = 'btceEUR, btcdeEUR' + field = 'close' + cache_timeout = 900 # bitcoincharts: load max. every 15 min + symbols = True + color_index = -1 + _url = 'http://api.bitcoincharts.com/v1/markets.json' + _map = {'EUR': '€', 'USD': '$', 'GBP': '£', 'YEN': '¥', 'CNY': '¥', + 'AUD': '$'} + # markets according to the following lists: + # + def _get_price(self, data, market, field): + for m in data: + if m['symbol'] == market: + return m[field] + + def get_rate(self, i3s_output_list, i3s_config): + response = {'full_text': '', 'name': 'bitcoin rates', + 'cached_until': time() + self.cache_timeout} + # get the data from the bitcoincharts website + try: + data = json.loads(ul.urlopen(self._url).read().decode()) + except Exception: + response['color'] = i3s_config['color_bad'] + response['full_text'] = 'Bitcoincharts not reachable' + return response + # get the rate for each market given + rates, markets = [], self.markets.split(",") + color_rate = None + for i, market in enumerate(markets): + market = market.strip() + try: + rate = self._get_price(data, market, self.field) + if i == self.color_index: + color_rate = rate + except Exception: + pass + rates.append('{}: '.format((market[:-3] if rate else market)) + + ('N/A' if not rate + else ('{:.2f}{}'.format(rate, (self._map.get(market[-3:], market[-3:]) if self.symbols else market[-3:]))))) + # don't color multiple sites if no color_index is given + global last_price + if len(rates) == 1 or self.color_index >= -1: + if last_price == 0: + pass + elif rate < last_price: + response['color'] = i3s_config['color_bad'] + elif rate > last_price: + response['color'] = i3s_config['color_good'] + last_price = rate + response['full_text'] = ', '.join(rates) + return response + +if __name__ == '__main__': + from time import sleep + x = Py3status() + while True: + print(x.get_rate([], {'color_good': 'green', + 'color_bad': 'red'})) + sleep(5) diff --git a/py3status/modules/crypto-price-checker.py b/py3status/modules/crypto-price-checker.py deleted file mode 100644 index c717952..0000000 --- a/py3status/modules/crypto-price-checker.py +++ /dev/null @@ -1,77 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Module for displaying bitcoin prices using -the API by www.bitcoincharts.com. - -Written and contributed by @tasse: - Andre Doser -""" -import json -import urllib.request as ul -from time import time -import re - -last_price = 0 - - -class Py3status: - # possible markets see http://bitcoincharts.com/markets/list/ - markets = 'btceEUR, btcdeEUR' - field = 'close' - cache_timeout = 900 # bitcoincharts: load max. every 15 min - symbols = True - color_index = -1 - _url = 'http://api.bitcoincharts.com/v1/markets.json' - _map = {'EUR': '€', 'USD': '$', 'GBP': '£', 'YEN': '¥', 'CNY': '¥', - 'AUD': '$'} - # markets according to the following lists: - # - def _get_price(self, data, market, field): - for m in data: - if m['symbol'] == market: - return m[field] - - def get_rate(self, i3s_output_list, i3s_config): - response = {'full_text': '', 'name': 'bitcoin rates', - 'cached_until': time() + self.cache_timeout} - # get the data from the bitcoincharts website - try: - data = json.loads(ul.urlopen(self._url).read().decode()) - except Exception: - response['color'] = i3s_config['color_bad'] - response['full_text'] = 'Bitcoincharts not reachable' - return response - # get the rate for each market given - rates, markets = [], self.markets.split(",") - color_rate = None - for i, market in enumerate(markets): - market = market.strip() - try: - rate = self._get_price(data, market, self.field) - if i == self.color_index: - color_rate = rate - except Exception: - pass - rates.append('{}: '.format((market[:-3] if rate else market)) - + ('N/A' if not rate - else ('{:.2f}{}'.format(rate, (self._map.get(market[-3:], market[-3:]) if self.symbols else market[-3:]))))) - # don't color multiple sites if no color_index is given - global last_price - if len(rates) == 1 or self.color_index >= -1: - if last_price == 0: - pass - elif rate < last_price: - response['color'] = i3s_config['color_bad'] - elif rate > last_price: - response['color'] = i3s_config['color_good'] - last_price = rate - response['full_text'] = ', '.join(rates) - return response - -if __name__ == '__main__': - from time import sleep - x = Py3status() - while True: - print(x.get_rate([], {'color_good': 'green', - 'color_bad': 'red'})) - sleep(5) -- cgit v1.3 From dd211725b53755bd1c439bcf607e71ca4be49e89 Mon Sep 17 00:00:00 2001 From: André Doser Date: Tue, 27 Jan 2015 17:13:02 +0100 Subject: Pep8 --- py3status/modules/bitcoin-price.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price.py b/py3status/modules/bitcoin-price.py index c717952..915e0a1 100644 --- a/py3status/modules/bitcoin-price.py +++ b/py3status/modules/bitcoin-price.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- """ -Module for displaying bitcoin prices using +Module for displaying bitcoin prices using the API by www.bitcoincharts.com. Written and contributed by @tasse: @@ -9,7 +9,6 @@ Written and contributed by @tasse: import json import urllib.request as ul from time import time -import re last_price = 0 @@ -24,8 +23,7 @@ class Py3status: _url = 'http://api.bitcoincharts.com/v1/markets.json' _map = {'EUR': '€', 'USD': '$', 'GBP': '£', 'YEN': '¥', 'CNY': '¥', 'AUD': '$'} - # markets according to the following lists: - # + def _get_price(self, data, market, field): for m in data: if m['symbol'] == market: @@ -53,18 +51,21 @@ class Py3status: except Exception: pass rates.append('{}: '.format((market[:-3] if rate else market)) - + ('N/A' if not rate - else ('{:.2f}{}'.format(rate, (self._map.get(market[-3:], market[-3:]) if self.symbols else market[-3:]))))) + + ('N/A' if not rate + else ('{:.2f}{}'.format( + rate, + (self._map.get(market[-3:], market[-3:]) + if self.symbols else market[-3:]))))) # don't color multiple sites if no color_index is given global last_price if len(rates) == 1 or self.color_index >= -1: if last_price == 0: pass - elif rate < last_price: + elif color_rate < last_price: response['color'] = i3s_config['color_bad'] - elif rate > last_price: + elif color_rate > last_price: response['color'] = i3s_config['color_good'] - last_price = rate + last_price = color_rate response['full_text'] = ', '.join(rates) return response -- cgit v1.3 From 64e142b172d5d680064541787e1c14c637b77a85 Mon Sep 17 00:00:00 2001 From: André Doser Date: Tue, 27 Jan 2015 17:31:05 +0100 Subject: refactor --- py3status/modules/bitcoin-price.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price.py b/py3status/modules/bitcoin-price.py index 915e0a1..3f24c3b 100644 --- a/py3status/modules/bitcoin-price.py +++ b/py3status/modules/bitcoin-price.py @@ -50,15 +50,14 @@ class Py3status: color_rate = rate except Exception: pass - rates.append('{}: '.format((market[:-3] if rate else market)) - + ('N/A' if not rate - else ('{:.2f}{}'.format( - rate, - (self._map.get(market[-3:], market[-3:]) - if self.symbols else market[-3:]))))) + out = market[:-3] if rate else market # market name + out += 'N/A' if not rate else '{:.2f}'.format(rate) # rate + currency_sym = self._map.get(market[-3:], market[-3:]) + out += currency_sym if self.symbols else market + rates.append(out) # don't color multiple sites if no color_index is given global last_price - if len(rates) == 1 or self.color_index >= -1: + if len(rates) == 1 or self.color_index > -1: if last_price == 0: pass elif color_rate < last_price: -- cgit v1.3 From 1399d3cbaa5eaa25b4a22ddbca908591c357db27 Mon Sep 17 00:00:00 2001 From: André Doser Date: Tue, 27 Jan 2015 17:49:30 +0100 Subject: python2 support, sort --- py3status/modules/bitcoin-price.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price.py b/py3status/modules/bitcoin-price.py index 3f24c3b..b6d5359 100644 --- a/py3status/modules/bitcoin-price.py +++ b/py3status/modules/bitcoin-price.py @@ -7,7 +7,6 @@ Written and contributed by @tasse: Andre Doser """ import json -import urllib.request as ul from time import time last_price = 0 @@ -15,14 +14,15 @@ last_price = 0 class Py3status: # possible markets see http://bitcoincharts.com/markets/list/ - markets = 'btceEUR, btcdeEUR' + cache_timeout = 900 # load max. every 15 min (according to bitcoincharts) + color_index = -1 field = 'close' - cache_timeout = 900 # bitcoincharts: load max. every 15 min + markets = 'btceEUR, btcdeEUR' symbols = True - color_index = -1 - _url = 'http://api.bitcoincharts.com/v1/markets.json' + _map = {'EUR': '€', 'USD': '$', 'GBP': '£', 'YEN': '¥', 'CNY': '¥', 'AUD': '$'} + _url = 'http://api.bitcoincharts.com/v1/markets.json' def _get_price(self, data, market, field): for m in data: @@ -32,6 +32,10 @@ class Py3status: def get_rate(self, i3s_output_list, i3s_config): response = {'full_text': '', 'name': 'bitcoin rates', 'cached_until': time() + self.cache_timeout} + try: # python 3 + import urllib.request as ul + except: # python 2 + import urllib2 as ul # get the data from the bitcoincharts website try: data = json.loads(ul.urlopen(self._url).read().decode()) @@ -51,6 +55,7 @@ class Py3status: except Exception: pass out = market[:-3] if rate else market # market name + out += ': ' out += 'N/A' if not rate else '{:.2f}'.format(rate) # rate currency_sym = self._map.get(market[-3:], market[-3:]) out += currency_sym if self.symbols else market @@ -75,3 +80,4 @@ if __name__ == '__main__': print(x.get_rate([], {'color_good': 'green', 'color_bad': 'red'})) sleep(5) + break -- cgit v1.3 From bf62eab56bee550daae4e12067084a17aff876d4 Mon Sep 17 00:00:00 2001 From: André Doser Date: Tue, 27 Jan 2015 17:50:10 +0100 Subject: forget to remove break.. --- py3status/modules/bitcoin-price.py | 1 - 1 file changed, 1 deletion(-) (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price.py b/py3status/modules/bitcoin-price.py index b6d5359..3d2e65b 100644 --- a/py3status/modules/bitcoin-price.py +++ b/py3status/modules/bitcoin-price.py @@ -80,4 +80,3 @@ if __name__ == '__main__': print(x.get_rate([], {'color_good': 'green', 'color_bad': 'red'})) sleep(5) - break -- cgit v1.3 From 8f932503f79d193c36129548599b48489d840dc9 Mon Sep 17 00:00:00 2001 From: André Doser Date: Tue, 27 Jan 2015 17:52:17 +0100 Subject: documentation --- py3status/modules/bitcoin-price.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price.py b/py3status/modules/bitcoin-price.py index 3d2e65b..d9f98b7 100644 --- a/py3status/modules/bitcoin-price.py +++ b/py3status/modules/bitcoin-price.py @@ -15,10 +15,10 @@ last_price = 0 class Py3status: # possible markets see http://bitcoincharts.com/markets/list/ cache_timeout = 900 # load max. every 15 min (according to bitcoincharts) - color_index = -1 - field = 'close' - markets = 'btceEUR, btcdeEUR' - symbols = True + color_index = -1 # color output according to which market? + field = 'close' # see http://bitcoincharts.com/about/markets-api/ + markets = 'btceEUR, btcdeEUR' # comma separated + symbols = True # convert USD -> $ etc. _map = {'EUR': '€', 'USD': '$', 'GBP': '£', 'YEN': '¥', 'CNY': '¥', 'AUD': '$'} -- cgit v1.3 From f3bde41a2c4b1e4395c9903566f0e0c73234bf60 Mon Sep 17 00:00:00 2001 From: André Doser Date: Tue, 27 Jan 2015 18:01:54 +0100 Subject: removed te global variable last_price. Moved last_price, _map and _url to __init__ --- py3status/modules/bitcoin-price.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price.py b/py3status/modules/bitcoin-price.py index d9f98b7..d53ac93 100644 --- a/py3status/modules/bitcoin-price.py +++ b/py3status/modules/bitcoin-price.py @@ -9,8 +9,6 @@ Written and contributed by @tasse: import json from time import time -last_price = 0 - class Py3status: # possible markets see http://bitcoincharts.com/markets/list/ @@ -20,9 +18,11 @@ class Py3status: markets = 'btceEUR, btcdeEUR' # comma separated symbols = True # convert USD -> $ etc. - _map = {'EUR': '€', 'USD': '$', 'GBP': '£', 'YEN': '¥', 'CNY': '¥', - 'AUD': '$'} - _url = 'http://api.bitcoincharts.com/v1/markets.json' + def __init__(self): + self.last_price = 0 + self.currency_map = {'EUR': '€', 'USD': '$', 'GBP': '£', + 'YEN': '¥', 'CNY': '¥', 'AUD': '$'} + self.url = 'http://api.bitcoincharts.com/v1/markets.json' def _get_price(self, data, market, field): for m in data: @@ -38,7 +38,7 @@ class Py3status: import urllib2 as ul # get the data from the bitcoincharts website try: - data = json.loads(ul.urlopen(self._url).read().decode()) + data = json.loads(ul.urlopen(self.url).read().decode()) except Exception: response['color'] = i3s_config['color_bad'] response['full_text'] = 'Bitcoincharts not reachable' @@ -57,19 +57,18 @@ class Py3status: out = market[:-3] if rate else market # market name out += ': ' out += 'N/A' if not rate else '{:.2f}'.format(rate) # rate - currency_sym = self._map.get(market[-3:], market[-3:]) + currency_sym = self.currency_map.get(market[-3:], market[-3:]) out += currency_sym if self.symbols else market rates.append(out) # don't color multiple sites if no color_index is given - global last_price if len(rates) == 1 or self.color_index > -1: - if last_price == 0: + if self.last_price == 0: pass - elif color_rate < last_price: + elif color_rate < self.last_price: response['color'] = i3s_config['color_bad'] - elif color_rate > last_price: + elif color_rate > self.last_price: response['color'] = i3s_config['color_good'] - last_price = color_rate + self.last_pricee = color_rate response['full_text'] = ', '.join(rates) return response @@ -79,4 +78,4 @@ if __name__ == '__main__': while True: print(x.get_rate([], {'color_good': 'green', 'color_bad': 'red'})) - sleep(5) + sleep(5) \ No newline at end of file -- cgit v1.3 From 22f4abf8e22f8517be78d039551ab6871ddf14b9 Mon Sep 17 00:00:00 2001 From: André Doser Date: Tue, 27 Jan 2015 18:02:22 +0100 Subject: flake8 --- py3status/modules/bitcoin-price.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price.py b/py3status/modules/bitcoin-price.py index d53ac93..638c2c8 100644 --- a/py3status/modules/bitcoin-price.py +++ b/py3status/modules/bitcoin-price.py @@ -78,4 +78,4 @@ if __name__ == '__main__': while True: print(x.get_rate([], {'color_good': 'green', 'color_bad': 'red'})) - sleep(5) \ No newline at end of file + sleep(5) -- cgit v1.3 From a0e0144cecb032fa7d34dac8452bcb2b57f7a770 Mon Sep 17 00:00:00 2001 From: André Doser Date: Tue, 27 Jan 2015 19:29:36 +0100 Subject: removed typo --- py3status/modules/bitcoin-price.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price.py b/py3status/modules/bitcoin-price.py index 638c2c8..d98ba71 100644 --- a/py3status/modules/bitcoin-price.py +++ b/py3status/modules/bitcoin-price.py @@ -68,7 +68,7 @@ class Py3status: response['color'] = i3s_config['color_bad'] elif color_rate > self.last_price: response['color'] = i3s_config['color_good'] - self.last_pricee = color_rate + self.last_price = color_rate response['full_text'] = ', '.join(rates) return response -- cgit v1.3 From af7ee847d243d2a5d7a53fed2499ce45b9029522 Mon Sep 17 00:00:00 2001 From: André Doser Date: Tue, 27 Jan 2015 20:51:09 +0100 Subject: Docstrings --- py3status/modules/bitcoin-price.py | 44 ++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 9 deletions(-) (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price.py b/py3status/modules/bitcoin-price.py index d98ba71..a1bc2a6 100644 --- a/py3status/modules/bitcoin-price.py +++ b/py3status/modules/bitcoin-price.py @@ -11,20 +11,42 @@ from time import time class Py3status: - # possible markets see http://bitcoincharts.com/markets/list/ - cache_timeout = 900 # load max. every 15 min (according to bitcoincharts) - color_index = -1 # color output according to which market? - field = 'close' # see http://bitcoincharts.com/about/markets-api/ - markets = 'btceEUR, btcdeEUR' # comma separated - symbols = True # convert USD -> $ etc. + """ + Configuration parameters: + - cache_timeout: Should be at least 15 min according to bitcoincharts. + - color_index : Index of the market responsible for coloration, + meaning that the output is going to be green if the + price went up and red if it went down. + default: -1 means no coloration, + except when only one market is selected + - field : Field that is displayed per market, + see http://bitcoincharts.com/about/markets-api/ + - markets : Comma-separated list of markets. Supported markets can + be found at http://bitcoincharts.com/markets/list/ + - symbols : Try to match currency abbreviations to symbols, + e.g. USD -> $, EUR -> € and so on + """ + cache_timeout = 900 + color_index = -1 + field = 'closse' + markets = 'btceEUR, btcdeEUR' + symbols = True def __init__(self): + """ + Initialize last_price, set the currency mapping + and the url containing the data. + """ self.last_price = 0 self.currency_map = {'EUR': '€', 'USD': '$', 'GBP': '£', 'YEN': '¥', 'CNY': '¥', 'AUD': '$'} self.url = 'http://api.bitcoincharts.com/v1/markets.json' def _get_price(self, data, market, field): + """ + Given the data (in json format), returns the + field for a given market. + """ for m in data: if m['symbol'] == market: return m[field] @@ -50,17 +72,18 @@ class Py3status: market = market.strip() try: rate = self._get_price(data, market, self.field) - if i == self.color_index: + if i == self.color_index: # coloration color_rate = rate except Exception: - pass + continue out = market[:-3] if rate else market # market name out += ': ' out += 'N/A' if not rate else '{:.2f}'.format(rate) # rate currency_sym = self.currency_map.get(market[-3:], market[-3:]) out += currency_sym if self.symbols else market rates.append(out) - # don't color multiple sites if no color_index is given + # only colorize if an index is given or + # if only one market is selected if len(rates) == 1 or self.color_index > -1: if self.last_price == 0: pass @@ -73,6 +96,9 @@ class Py3status: return response if __name__ == '__main__': + """ + Test this module by calling it directly. + """ from time import sleep x = Py3status() while True: -- cgit v1.3 From 2fa9e819fce8556cb8fed744e6b5e29f178cbf9c Mon Sep 17 00:00:00 2001 From: André Doser Date: Tue, 27 Jan 2015 23:03:52 +0100 Subject: Error handling --- py3status/modules/bitcoin-price.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price.py b/py3status/modules/bitcoin-price.py index a1bc2a6..53da320 100644 --- a/py3status/modules/bitcoin-price.py +++ b/py3status/modules/bitcoin-price.py @@ -55,13 +55,15 @@ class Py3status: response = {'full_text': '', 'name': 'bitcoin rates', 'cached_until': time() + self.cache_timeout} try: # python 3 - import urllib.request as ul - except: # python 2 - import urllib2 as ul + from urllib.request import urlopen + from urllib.error import URLError + except ImportError: # python 2 + from urllib2 import urlopen + from urllib2 import URLError # get the data from the bitcoincharts website try: - data = json.loads(ul.urlopen(self.url).read().decode()) - except Exception: + data = json.loads(urlopen(self.url).read().decode()) + except URLError: response['color'] = i3s_config['color_bad'] response['full_text'] = 'Bitcoincharts not reachable' return response @@ -74,7 +76,7 @@ class Py3status: rate = self._get_price(data, market, self.field) if i == self.color_index: # coloration color_rate = rate - except Exception: + except KeyError: continue out = market[:-3] if rate else market # market name out += ': ' -- cgit v1.3 From 0d4aa14e59afcc20dcf30f51d224f4ba9e919141 Mon Sep 17 00:00:00 2001 From: André Doser Date: Wed, 28 Jan 2015 09:54:58 +0100 Subject: coloration fix --- py3status/modules/bitcoin-price.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price.py b/py3status/modules/bitcoin-price.py index 53da320..41a446a 100644 --- a/py3status/modules/bitcoin-price.py +++ b/py3status/modules/bitcoin-price.py @@ -28,8 +28,8 @@ class Py3status: """ cache_timeout = 900 color_index = -1 - field = 'closse' - markets = 'btceEUR, btcdeEUR' + field = 'close' + markets = 'btceUSD' symbols = True def __init__(self): @@ -74,7 +74,8 @@ class Py3status: market = market.strip() try: rate = self._get_price(data, market, self.field) - if i == self.color_index: # coloration + # coloration + if i == self.color_index or len(markets) == 1: color_rate = rate except KeyError: continue -- cgit v1.3 From 24469f3a455fb40caf0bed76cb6a8fe75f9c86c4 Mon Sep 17 00:00:00 2001 From: André Doser Date: Wed, 28 Jan 2015 09:56:13 +0100 Subject: . --- py3status/modules/bitcoin-price.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price.py b/py3status/modules/bitcoin-price.py index 41a446a..61e7410 100644 --- a/py3status/modules/bitcoin-price.py +++ b/py3status/modules/bitcoin-price.py @@ -29,7 +29,7 @@ class Py3status: cache_timeout = 900 color_index = -1 field = 'close' - markets = 'btceUSD' + markets = 'btceUSD, btcdeEUR' symbols = True def __init__(self): -- cgit v1.3 From 25a3f1a6662c69b525ac8aed8497580284d2e5fc Mon Sep 17 00:00:00 2001 From: André Doser Date: Wed, 28 Jan 2015 13:04:20 +0100 Subject: Some refactoring, thanks Ultrabug --- py3status/modules/bitcoin-price.py | 110 ------------------------------- py3status/modules/bitcoin_price.py | 130 +++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 110 deletions(-) delete mode 100644 py3status/modules/bitcoin-price.py create mode 100644 py3status/modules/bitcoin_price.py (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin-price.py b/py3status/modules/bitcoin-price.py deleted file mode 100644 index 61e7410..0000000 --- a/py3status/modules/bitcoin-price.py +++ /dev/null @@ -1,110 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Module for displaying bitcoin prices using -the API by www.bitcoincharts.com. - -Written and contributed by @tasse: - Andre Doser -""" -import json -from time import time - - -class Py3status: - """ - Configuration parameters: - - cache_timeout: Should be at least 15 min according to bitcoincharts. - - color_index : Index of the market responsible for coloration, - meaning that the output is going to be green if the - price went up and red if it went down. - default: -1 means no coloration, - except when only one market is selected - - field : Field that is displayed per market, - see http://bitcoincharts.com/about/markets-api/ - - markets : Comma-separated list of markets. Supported markets can - be found at http://bitcoincharts.com/markets/list/ - - symbols : Try to match currency abbreviations to symbols, - e.g. USD -> $, EUR -> € and so on - """ - cache_timeout = 900 - color_index = -1 - field = 'close' - markets = 'btceUSD, btcdeEUR' - symbols = True - - def __init__(self): - """ - Initialize last_price, set the currency mapping - and the url containing the data. - """ - self.last_price = 0 - self.currency_map = {'EUR': '€', 'USD': '$', 'GBP': '£', - 'YEN': '¥', 'CNY': '¥', 'AUD': '$'} - self.url = 'http://api.bitcoincharts.com/v1/markets.json' - - def _get_price(self, data, market, field): - """ - Given the data (in json format), returns the - field for a given market. - """ - for m in data: - if m['symbol'] == market: - return m[field] - - def get_rate(self, i3s_output_list, i3s_config): - response = {'full_text': '', 'name': 'bitcoin rates', - 'cached_until': time() + self.cache_timeout} - try: # python 3 - from urllib.request import urlopen - from urllib.error import URLError - except ImportError: # python 2 - from urllib2 import urlopen - from urllib2 import URLError - # get the data from the bitcoincharts website - try: - data = json.loads(urlopen(self.url).read().decode()) - except URLError: - response['color'] = i3s_config['color_bad'] - response['full_text'] = 'Bitcoincharts not reachable' - return response - # get the rate for each market given - rates, markets = [], self.markets.split(",") - color_rate = None - for i, market in enumerate(markets): - market = market.strip() - try: - rate = self._get_price(data, market, self.field) - # coloration - if i == self.color_index or len(markets) == 1: - color_rate = rate - except KeyError: - continue - out = market[:-3] if rate else market # market name - out += ': ' - out += 'N/A' if not rate else '{:.2f}'.format(rate) # rate - currency_sym = self.currency_map.get(market[-3:], market[-3:]) - out += currency_sym if self.symbols else market - rates.append(out) - # only colorize if an index is given or - # if only one market is selected - if len(rates) == 1 or self.color_index > -1: - if self.last_price == 0: - pass - elif color_rate < self.last_price: - response['color'] = i3s_config['color_bad'] - elif color_rate > self.last_price: - response['color'] = i3s_config['color_good'] - self.last_price = color_rate - response['full_text'] = ', '.join(rates) - return response - -if __name__ == '__main__': - """ - Test this module by calling it directly. - """ - from time import sleep - x = Py3status() - while True: - print(x.get_rate([], {'color_good': 'green', - 'color_bad': 'red'})) - sleep(5) diff --git a/py3status/modules/bitcoin_price.py b/py3status/modules/bitcoin_price.py new file mode 100644 index 0000000..27dbf0c --- /dev/null +++ b/py3status/modules/bitcoin_price.py @@ -0,0 +1,130 @@ +# -*- coding: utf-8 -*- +""" +Module for displaying bitcoin prices using +the API by www.bitcoincharts.com. + +Written and contributed by @tasse: + Andre Doser +""" +import json + +from time import time +try: + # python 3 + from urllib.error import URLError + from urllib.request import urlopen +except ImportError: + # python 2 + from urllib2 import URLError + from urllib2 import urlopen + + +class Py3status: + """ + Configuration parameters: + - cache_timeout: Should be at least 15 min according to bitcoincharts. + - color_index : Index of the market responsible for coloration, + meaning that the output is going to be green if the + price went up and red if it went down. + default: -1 means no coloration, + except when only one market is selected + - field : Field that is displayed per market, + see http://bitcoincharts.com/about/markets-api/ + - markets : Comma-separated list of markets. Supported markets can + be found at http://bitcoincharts.com/markets/list/ + - symbols : Try to match currency abbreviations to symbols, + e.g. USD -> $, EUR -> € and so on + """ + + # available configuration parameters + cache_timeout = 900 + color_index = -1 + field = 'close' + hide_on_error = False + markets = 'btceUSD, btcdeEUR' + symbols = True + + def __init__(self): + """ + Initialize last_price, set the currency mapping + and the url containing the data. + """ + self.currency_map = { + 'AUD': '$', + 'CNY': '¥', + 'EUR': '€', + 'GBP': '£', + 'USD': '$', + 'YEN': '¥' + } + self.last_price = 0 + self.url = 'http://api.bitcoincharts.com/v1/markets.json' + + def _get_price(self, data, market, field): + """ + Given the data (in json format), returns the + field for a given market. + """ + for m in data: + if m['symbol'] == market: + return m[field] + + def get_rate(self, i3s_output_list, i3s_config): + response = { + 'cached_until': time() + self.cache_timeout, + 'full_text': '' + } + + # get the data from the bitcoincharts website + try: + data = json.loads(urlopen(self.url).read().decode()) + except URLError: + if not self.hide_on_error: + response['color'] = i3s_config['color_bad'] + response['full_text'] = 'Bitcoincharts unreachable' + return response + + # get the rate for each market given + rates, markets = [], self.markets.split(',') + color_rate = None + for i, market in enumerate(markets): + market = market.strip() + try: + rate = self._get_price(data, market, self.field) + # coloration + if i == self.color_index or len(markets) == 1: + color_rate = rate + except KeyError: + continue + # market name + out = market[:-3] if rate else market + out += ': ' + # rate + out += 'N/A' if not rate else '{:.2f}'.format(rate) + currency_sym = self.currency_map.get(market[-3:], market[-3:]) + out += currency_sym if self.symbols else market + rates.append(out) + + # only colorize if an index is given or + # if only one market is selected + if len(rates) == 1 or self.color_index > -1: + if self.last_price == 0: + pass + elif color_rate < self.last_price: + response['color'] = i3s_config['color_bad'] + elif color_rate > self.last_price: + response['color'] = i3s_config['color_good'] + self.last_price = color_rate + + response['full_text'] = ', '.join(rates) + return response + +if __name__ == '__main__': + """ + Test this module by calling it directly. + """ + from time import sleep + x = Py3status() + while True: + print(x.get_rate([], {'color_good': 'green', 'color_bad': 'red'})) + sleep(5) -- cgit v1.3 From 87f443713fc45de49c6d0b15a29d1216de114088 Mon Sep 17 00:00:00 2001 From: André Doser Date: Wed, 28 Jan 2015 13:06:29 +0100 Subject: docstring hide_on_error --- py3status/modules/bitcoin_price.py | 1 + 1 file changed, 1 insertion(+) (limited to 'py3status/modules') diff --git a/py3status/modules/bitcoin_price.py b/py3status/modules/bitcoin_price.py index 27dbf0c..18b3348 100644 --- a/py3status/modules/bitcoin_price.py +++ b/py3status/modules/bitcoin_price.py @@ -30,6 +30,7 @@ class Py3status: except when only one market is selected - field : Field that is displayed per market, see http://bitcoincharts.com/about/markets-api/ + - hide_on_error: Display empty response if True, else an error message - markets : Comma-separated list of markets. Supported markets can be found at http://bitcoincharts.com/markets/list/ - symbols : Try to match currency abbreviations to symbols, -- cgit v1.3