diff options
Diffstat (limited to 'ledger-price-db-update.py')
| -rwxr-xr-x | ledger-price-db-update.py | 72 |
1 files changed, 56 insertions, 16 deletions
diff --git a/ledger-price-db-update.py b/ledger-price-db-update.py index d4d9bce..5167c13 100755 --- a/ledger-price-db-update.py +++ b/ledger-price-db-update.py @@ -1,6 +1,8 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- import os +import sys +import traceback from configparser import ConfigParser import requests import json @@ -23,9 +25,11 @@ from codecs import open # # [etf] # symbols=FXUS +# +# [bond] +# RUS-20=XS0088543193 CONFIG_FILE = '~/.ledger-commodities' -current_date = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S') config = ConfigParser() config.readfp(open(os.path.expanduser(CONFIG_FILE), 'r', 'utf-8')) @@ -35,8 +39,9 @@ def get_json(url, **kwargs): return json.loads(response.content) -def print_price(symbol, price, base): - print ('P %s %s %f %s' % (current_date, symbol, price, base)).encode('utf-8') +def print_price(symbol, price, base, date=datetime.datetime.now()): + date_str = date.strftime('%Y/%m/%d %H:%M:%S') + print ('P %s %s %f %s' % (date_str, symbol, price, base)).encode('utf-8') def exchange_rates(): @@ -55,28 +60,63 @@ def exchange_rates(): def stocks(): - params = { - "types": "ohlc", - "symbols": config.get('stocks', 'symbols') - } - data = get_json(r'https://api.iextrading.com/1.0/stock/market/batch', params=params) - for symbol in data.keys(): - price = data[symbol]['ohlc']['close']['price'] + for symbol in config.get('stocks', 'symbols').split(','): + post_data = { + 'msg': 'min', + 'symbol': symbol, + 'qesymbol': symbol + } + response = requests.post(r'https://www.nasdaq.com/callbacks/NLSHandler2.ashx', data=post_data) + data = json.loads(response.content) + price = data['data'][-1]['price'] print_price(symbol, price, '$') +def get_moex_value(data, name): + if 'columns' in data: + index = data['columns'].index(name) + if 'data' in data: + if len(data['data']) > 0: + return data['data'][0][index] + return None + def etfs(): symbols = config.get('etf', 'symbols') for symbol in symbols.split(','): - data = get_json(r'http://iss.moex.com/iss/engines/stock/markets/shares/boards/TQTF/securities/%s.jsonp' % symbol) + data = get_json(r'http://iss.moex.com/iss/engines/stock/markets/shares/boards/TQTF/securities/%s.jsonp' % symbol)['marketdata'] - value_index = data['marketdata']['columns'].index('LAST') - value = data['marketdata']['data'][0][value_index] + value = get_moex_value(data, 'LAST') print_price(symbol, value, 'R') +def bonds(): + symbols = config.items('bond') + date = datetime.datetime.now() - datetime.timedelta(days=1) + date_str = date.strftime('%Y-%m-%d') + for short, symbol in symbols: + url = r'https://iss.moex.com/iss/history/engines/stock/markets/bonds/boards/TQOD/securities/%s.jsonp?from=%s' % (symbol, date_str) + data = get_json(url)['history'] + if len(data['data']) > 0: + value = get_moex_value(data, 'CLOSE') + nominal = get_moex_value(data, 'FACEVALUE') + currency = get_moex_value(data, 'FACEUNIT') + if currency == 'USD': + currency = '$' + elif currency == 'EUR': + currency = '€' + elif currency == 'RUB': + currency = 'R' + if value is None or nominal is None or currency is None: + continue + print_price(short.upper(), value * nominal / 100.0, currency, date) + + def main(): - exchange_rates() - stocks() - etfs() + updates = [exchange_rates, stocks, etfs, bonds] + for update in updates: + try: + update() + except Exception as e: + print >> sys.stderr, 'Oops, update method `%s` failed: %s' % (update, e) + traceback.print_exc() if __name__ == '__main__': main() |
