diff options
| author | Anton Bobov <abobov@gmail.com> | 2020-07-26 21:49:41 +0500 |
|---|---|---|
| committer | Anton Bobov <abobov@gmail.com> | 2020-07-26 21:49:41 +0500 |
| commit | d449097301953bbd626433814cab5aa0680e3aca (patch) | |
| tree | 8b55e65df33b93495cf029fb564ee291fe4eacf2 /ledger-price-db-update.py | |
| parent | 279750251f0d6e864d3d80855b7fd3164387e3c6 (diff) | |
Updated price fetch script.
Diffstat (limited to 'ledger-price-db-update.py')
| -rwxr-xr-x | ledger-price-db-update.py | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/ledger-price-db-update.py b/ledger-price-db-update.py index b80191c..1ab1d4f 100755 --- a/ledger-price-db-update.py +++ b/ledger-price-db-update.py @@ -36,10 +36,12 @@ import requests CONFIG_FILE = '~/.ledger-commodities' config = ConfigParser() config.read_file(open(os.path.expanduser(CONFIG_FILE), 'r', 'utf-8')) - +headers = { + 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36' +} def get_json(url, **kwargs): - response = requests.get(url, **kwargs) + response = requests.get(url, timeout=10, headers=headers, **kwargs) return json.loads(response.content) @@ -69,8 +71,7 @@ def stocks(): for symbol in config.get('stocks', 'symbols').split(','): params = {"assetclass": "stocks"} url = r'https://api.nasdaq.com/api/quote/%s/info' % (symbol) - response = requests.get(url, params=params) - data = json.loads(response.content) + data = get_json(url, params=params) price = float(data['data']['keyStats']['PreviousClose']['value'][1:]) print_price(symbol, price, '$') @@ -78,9 +79,8 @@ def stocks(): 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] + if 'data' in data and len(data['data']) > 0: + return data['data'][0][index] return None |
