summaryrefslogtreecommitdiff
path: root/ledger-price-db-update.py
diff options
context:
space:
mode:
authorAnton Bobov <abobov@gmail.com>2020-07-26 21:49:41 +0500
committerAnton Bobov <abobov@gmail.com>2020-07-26 21:49:41 +0500
commitd449097301953bbd626433814cab5aa0680e3aca (patch)
tree8b55e65df33b93495cf029fb564ee291fe4eacf2 /ledger-price-db-update.py
parent279750251f0d6e864d3d80855b7fd3164387e3c6 (diff)
Updated price fetch script.
Diffstat (limited to 'ledger-price-db-update.py')
-rwxr-xr-xledger-price-db-update.py14
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