From 94ef73439b52be5f6a4bd2ed02d2cbad0e43db6a Mon Sep 17 00:00:00 2001 From: Chris Berkhout Date: Tue, 13 Apr 2021 22:30:33 +0200 Subject: [PATCH] CoinDesk fetch. --- src/pricehist/sources/CoinDesk.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/pricehist/sources/CoinDesk.py b/src/pricehist/sources/CoinDesk.py index 1c7da7b..f37d343 100644 --- a/src/pricehist/sources/CoinDesk.py +++ b/src/pricehist/sources/CoinDesk.py @@ -30,3 +30,21 @@ class CoinDesk(): data = json.loads(response.content) symbols = sorted([item['currency'] for item in data]) return symbols + + def fetch(self, pair, start, end): + base, quote = pair.split('/') + if base not in self.bases(): + exit(f'Invalid base {base}') + if quote not in self.quotes(): + exit(f'Invalid quote {quote}') + + min_start = '2010-07-17' + if start < min_start: + exit(f'start {start} too early. The CoinDesk BPI only covers data from {min_start} onwards.') + + url = f'https://api.coindesk.com/v1/bpi/historical/close.json?currency={quote}&start={start}&end={end}' + response = requests.get(url) + data = json.loads(response.content) + bpi = data['bpi'] + + return bpi