diff options
| author | Anton Bobov <abobov@gmail.com> | 2019-03-12 22:38:54 +0500 |
|---|---|---|
| committer | Anton Bobov <abobov@gmail.com> | 2019-03-12 22:38:54 +0500 |
| commit | 45ff10897a19f2251bea5f222af13ffbe311dc97 (patch) | |
| tree | 7111ce1178ba454eaa2105561658226077b94c29 | |
| parent | 2bf34f4efd5643b847170443ca9aa03dafed976c (diff) | |
Ledger prices update script updated.
| -rwxr-xr-x | d-backup | 1 | ||||
| -rwxr-xr-x | ledger-price-db-update.py | 82 | ||||
| -rwxr-xr-x | ledger-price-db-update.sh | 36 |
3 files changed, 83 insertions, 36 deletions
@@ -27,6 +27,7 @@ EOF backup() { duplicity cleanup --force "$TARGET" + duplicity remove-all-but-n-full 3 --force "$TARGET" duplicity \ --exclude-filelist "$HOME/.duplicity-exclude" \ --full-if-older-than 2M \ diff --git a/ledger-price-db-update.py b/ledger-price-db-update.py new file mode 100755 index 0000000..d4d9bce --- /dev/null +++ b/ledger-price-db-update.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import os +from configparser import ConfigParser +import requests +import json +import datetime +from codecs import open + +# Config sample: +# +# [fixer] +# access_key = 00000000000000000000000000000000 +# base=RUB +# +# [exchange-symbols] +# USD=$ +# EUR=€ +# RUB=R +# +# [stocks] +# symbols=V,AMD +# +# [etf] +# symbols=FXUS + +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')) + + +def get_json(url, **kwargs): + response = requests.get(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 exchange_rates(): + base = config.get('fixer', 'base') + symbols = dict([(symbol.upper(), commodity) for symbol, commodity in config.items('exchange-symbols')]) + params = { + "access_key": config.get('fixer', 'access_key'), + "symbols": ','.join(symbols.keys() + [base,]) + } + + rates = get_json(r'http://data.fixer.io/api/latest', params=params)['rates'] + base_value = rates[base] + for symbol, value in rates.items(): + if symbol != base: + print_price(symbols[symbol], base_value / value, symbols[base]) + + +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'] + print_price(symbol, price, '$') + +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) + + value_index = data['marketdata']['columns'].index('LAST') + value = data['marketdata']['data'][0][value_index] + print_price(symbol, value, 'R') + +def main(): + exchange_rates() + stocks() + etfs() + +if __name__ == '__main__': + main() diff --git a/ledger-price-db-update.sh b/ledger-price-db-update.sh deleted file mode 100755 index 46d7bb5..0000000 --- a/ledger-price-db-update.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash -# -# Get currency exchanges rate and update a ledger price file. -# -# Read commodities from file in format: COMMODITY SYMBOL (ex.: EURUSD $) - -set -e - -LEDGER_COMMODITIES="$HOME/.ledger-commodities" -if [ ! -f "$LEDGER_COMMODITIES" ] ; then - echo "No ledger commodities files: $LEDGER_COMMODITIES" - exit 1 -fi -LEDGER="$HOME/.ledgerrc" -if [[ ! -f "$LEDGER" ]] ; then - echo "No ledger configuration file: $LEDGER" >&2 - exit 13 -fi -PRICE_DB_FILE=$(sed -nE "s/^--price-db (.*)/\1/p" "$LEDGER") -PRICE_DB_FILE="${PRICE_DB_FILE/#\~/$HOME}" - -print_rate() { - currency="$1" - symbol="$2" - - val=$(xe "$currency=X") - - [[ $val =~ ^[0-9]+\.[0-9]+$ ]] && echo P $(date +"%Y/%m/%d %H:%M:%S") $symbol $val R -} - -{ - cat "$LEDGER_COMMODITIES" | while read commodity symbol ; do - print_rate "$commodity" "$symbol" - done -} >> "$PRICE_DB_FILE" - |
