Created
January 12, 2016 10:33
-
-
Save msztolcman/60f220b603905bb2f891 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# -*- coding: utf-8 -*- | |
from __future__ import print_function, unicode_literals | |
# <bitbar.title>Alior Currencies</bitbar.title> | |
# <bitbar.version>v1.0.0</bitbar.version> | |
# <bitbar.author>Marcin Sztolcman</bitbar.author> | |
# <bitbar.author.github>msztolcman</bitbar.author.github> | |
# <bitbar.desc>Displays currencies from Alior Bank</bitbar.desc> | |
# <bitbar.dependencies>python, python-requests</bitbar.dependencies> | |
import copy | |
import json | |
import os, os.path | |
import re | |
import sys | |
from pprint import pprint, pformat | |
import requests | |
url = 'http://kantor.aliorbank.pl/forex/json/current' | |
cache_file = '/tmp/aliorchf.cache' | |
change_up = '+' | |
change_down = '-' | |
change_empty = ' ' | |
change_up_color = 'red' | |
change_down_color = 'green' | |
change_empty_color = 'white' | |
def fill_previous(previous, current): | |
ret = copy.deepcopy(current) | |
for cur, data in current.items(): | |
# print(cur, data, previous.get(cur)) | |
if not previous or cur not in previous or previous[cur]['sell'] == current[cur]['sell']: | |
ret[cur]['dir_sell'] = change_empty | |
ret[cur]['dir_sell_color'] = change_empty_color | |
elif previous[cur]['sell'] < current[cur]['sell']: | |
ret[cur]['dir_sell'] = change_up | |
ret[cur]['dir_sell_color'] = change_up_color | |
elif previous[cur]['sell'] > current[cur]['sell']: | |
ret[cur]['dir_sell'] = change_down | |
ret[cur]['dir_sell_color'] = change_down_color | |
if not previous or cur not in previous or previous[cur]['buy'] == current[cur]['buy']: | |
ret[cur]['dir_buy'] = change_empty | |
ret[cur]['dir_buy_color'] = change_empty_color | |
elif previous[cur]['buy'] < current[cur]['buy']: | |
ret[cur]['dir_buy'] = change_up | |
ret[cur]['dir_buy_color'] = change_up_color | |
elif previous[cur]['buy'] > current[cur]['buy']: | |
ret[cur]['dir_buy'] = change_down | |
ret[cur]['dir_buy_color'] = change_down_color | |
return ret | |
try: | |
data = requests.get(url) | |
data = data.json()['currencies'] | |
if os.path.isfile(cache_file): | |
previous = json.loads(open(cache_file).read()) | |
else: | |
previous = None | |
ret = {} | |
for item in data: | |
if item['currency1'] == 'PLN' and item['currency2'] == 'CHF': | |
ret['CHF'] = item | |
elif item['currency1'] == 'PLN' and item['currency2'] == 'EUR': | |
ret['EUR'] = item | |
elif item['currency1'] == 'PLN' and item['currency2'] == 'USD': | |
ret['USD'] = item | |
ret = fill_previous(previous, ret) | |
with open(cache_file, 'w') as fh: | |
json.dump(ret, fh) | |
print('CHF:%(dir_sell)s%(sell)s | color=%(dir_sell_color)s' % ret['CHF']) | |
print('---') | |
print('CHF: buy:%(dir_buy)s%(buy)s sell:%(dir_sell)s%(sell)s | color=%(dir_sell_color)s' % ret['CHF']) | |
print('EUR: buy:%(dir_buy)s%(buy)s sell:%(dir_sell)s%(sell)s | color=%(dir_sell_color)s' % ret['EUR']) | |
print('USD: buy:%(dir_buy)s%(buy)s sell:%(dir_sell)s%(sell)s | color=%(dir_sell_color)s' % ret['USD']) | |
except: | |
print('ERROR') | |
if sys.stdout.isatty(): | |
import traceback | |
traceback.print_exc() | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment