Coverage for jbank/euribor.py: 0%

1 import logging

2 from typing import List

3 import requests

4 from jutil.parse import parse_datetime

5 from jutil.format import dec4

6 from jutil.xml import xml_to_dict

7 from jbank.models import EuriborRate

8

9 logger = logging.getLogger(__name__)

10

11

12 def fetch_latest_euribor_rates(commit: bool = False, verbose: bool = False) -> List[EuriborRate]:

13 feed_url = (

14 "https://www.suomenpankki.fi/WebForms/ReportViewerPage.aspx?report=/tilastot/markkina-_ja_hallinnolliset_korot/euribor_korot_today_xml_en&output=xml"

15 )

16 res = requests.get(feed_url)

17 if verbose:

18 logger.info("GET %s HTTP %s\n%s", feed_url, res.status_code, res.content)

19 if res.status_code >= 300:

20 raise Exception(f"Failed to load Euribor rate feed from {feed_url}")

21 results = xml_to_dict(res.content)

22 data = results["data"]["period_Collection"]["period"]

23 rates: List[EuriborRate] = []

24

25 if isinstance(data, list): # Sometime get a list or single item

26 assert len(data) > 0

27 data = data[len(data) - 1] # Get from newest date

28

29 record_date = parse_datetime(data["@value"]).date()

30 for rate_data in data["matrix1_Title_Collection"]["rate"]:

31 name = rate_data["@name"]

32 rate = dec4(rate_data["intr"]["@value"])

33 obj = EuriborRate(name=name, record_date=record_date, rate=rate)

34 rates.append(obj)

35 if commit:

36 out: List[EuriborRate] = []

37 for obj in rates:

38 out.append(EuriborRate.objects.save_unique(obj.record_date, obj.name, obj.rate))

39 return out

40 return rates