From ae69b9ec4eea4d47de6026994a37c1f8bc8080e2 Mon Sep 17 00:00:00 2001 From: Chris Berkhout Date: Sat, 17 Apr 2021 16:51:24 +0200 Subject: [PATCH] Rename sources files and reutrn Price objects. --- src/pricehist/sources/__init__.py | 4 ++-- src/pricehist/sources/{CoinDesk.py => coindesk.py} | 9 +++++++-- src/pricehist/sources/{ECB.py => ecb.py} | 4 +++- 3 files changed, 12 insertions(+), 5 deletions(-) rename src/pricehist/sources/{CoinDesk.py => coindesk.py} (86%) rename src/pricehist/sources/{ECB.py => ecb.py} (94%) diff --git a/src/pricehist/sources/__init__.py b/src/pricehist/sources/__init__.py index b55f31b..a116737 100644 --- a/src/pricehist/sources/__init__.py +++ b/src/pricehist/sources/__init__.py @@ -1,5 +1,5 @@ -from .CoinDesk import CoinDesk -from .ECB import ECB +from .coindesk import CoinDesk +from .ecb import ECB by_id = { CoinDesk.id(): CoinDesk, diff --git a/src/pricehist/sources/CoinDesk.py b/src/pricehist/sources/coindesk.py similarity index 86% rename from src/pricehist/sources/CoinDesk.py rename to src/pricehist/sources/coindesk.py index f37d343..f541942 100644 --- a/src/pricehist/sources/CoinDesk.py +++ b/src/pricehist/sources/coindesk.py @@ -1,6 +1,9 @@ +from decimal import Decimal import json import requests +from pricehist.price import Price + class CoinDesk(): @staticmethod @@ -45,6 +48,8 @@ class CoinDesk(): 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'] + prices = [] + for (d, v) in data['bpi'].items(): + prices.append(Price(base, quote, d, Decimal(str(v)))) - return bpi + return prices diff --git a/src/pricehist/sources/ECB.py b/src/pricehist/sources/ecb.py similarity index 94% rename from src/pricehist/sources/ECB.py rename to src/pricehist/sources/ecb.py index ad84105..2638477 100644 --- a/src/pricehist/sources/ECB.py +++ b/src/pricehist/sources/ecb.py @@ -4,6 +4,8 @@ import json import requests from xml.etree import ElementTree +from pricehist.price import Price + class ECB(): @staticmethod @@ -66,6 +68,6 @@ class ECB(): # TODO what if it's not found for that day? (some quotes aren't in the earliest data) rate = Decimal(day.find(rate_xpath).attrib['rate']) all_rows.insert(0, (date, rate)) - selected = [ (d, r) for d, r in all_rows if d >= start and d <= end ] + selected = [ Price(base, quote, d, r) for d, r in all_rows if d >= start and d <= end ] return selected