summaryrefslogtreecommitdiffstats
path: root/py3status
diff options
context:
space:
mode:
authorAndré Doser <doser.andre@gmail.com>2015-01-28 13:04:20 +0100
committerAndré Doser <doser.andre@gmail.com>2015-01-28 13:04:20 +0100
commit25a3f1a6662c69b525ac8aed8497580284d2e5fc (patch)
tree2eaf9a02bcee6f2f86c512a9f6205462a209e32c /py3status
parent24469f3a455fb40caf0bed76cb6a8fe75f9c86c4 (diff)
Some refactoring, thanks Ultrabug
Diffstat (limited to 'py3status')
-rw-r--r--py3status/modules/bitcoin_price.py (renamed from py3status/modules/bitcoin-price.py)56
1 files changed, 38 insertions, 18 deletions
diff --git a/py3status/modules/bitcoin-price.py b/py3status/modules/bitcoin_price.py
index 61e7410..27dbf0c 100644
--- a/py3status/modules/bitcoin-price.py
+++ b/py3status/modules/bitcoin_price.py
@@ -7,7 +7,16 @@ Written and contributed by @tasse:
Andre Doser <doser.andre AT gmail.com>
"""
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:
@@ -18,7 +27,7 @@ class Py3status:
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
+ 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
@@ -26,9 +35,12 @@ class Py3status:
- 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
@@ -37,9 +49,15 @@ class Py3status:
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.currency_map = {'EUR': '€', 'USD': '$', 'GBP': '£',
- 'YEN': '¥', 'CNY': '¥', 'AUD': '$'}
self.url = 'http://api.bitcoincharts.com/v1/markets.json'
def _get_price(self, data, market, field):
@@ -52,23 +70,22 @@ class Py3status:
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
+ 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:
- response['color'] = i3s_config['color_bad']
- response['full_text'] = 'Bitcoincharts not reachable'
+ 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(",")
+ rates, markets = [], self.markets.split(',')
color_rate = None
for i, market in enumerate(markets):
market = market.strip()
@@ -79,12 +96,15 @@ class Py3status:
color_rate = rate
except KeyError:
continue
- out = market[:-3] if rate else market # market name
+ # market name
+ out = market[:-3] if rate else market
out += ': '
- out += 'N/A' if not rate else '{:.2f}'.format(rate) # rate
+ # 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:
@@ -95,6 +115,7 @@ class Py3status:
elif color_rate > self.last_price:
response['color'] = i3s_config['color_good']
self.last_price = color_rate
+
response['full_text'] = ', '.join(rates)
return response
@@ -105,6 +126,5 @@ if __name__ == '__main__':
from time import sleep
x = Py3status()
while True:
- print(x.get_rate([], {'color_good': 'green',
- 'color_bad': 'red'}))
+ print(x.get_rate([], {'color_good': 'green', 'color_bad': 'red'}))
sleep(5)