Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib.request
- import json
- from datetime import datetime
- url_string = "http://api.nbp.pl/api/exchangerates/rates/a/gbp/2022-01-01/2023-01-01/?format=json"
- def format_date(date: str):
- day = date[4:6]
- if day[0] == '0':
- date = date.replace(day, day[1:], 1)
- day = day[1]
- match day:
- case "1" | "21" | "31":
- date = date.replace(day, day + 'st', 1)
- case "2" | "22":
- date = date.replace(day, day + 'nd', 1)
- case "3" | "23":
- date = date.replace(day, day + 'rd', 1)
- case _:
- date = date.replace(day, day + 'th', 1)
- return date
- with urllib.request.urlopen(url_string) as url:
- data = json.load(url)
- print("Currency: ", data['currency'])
- daily_rates = []
- for row in data['rates']:
- day = {'date': row['effectiveDate'],
- 'rate': row['mid']}
- daily_rates.append(day)
- peak = max(daily_rates, key=lambda item: item['rate'])
- depression = min(daily_rates, key=lambda item: item['rate'])
- peak_date = datetime.strptime(peak['date'], "%Y-%m-%d").date().strftime("%a %d %b %Y")
- depression_date = datetime.strptime(depression['date'], "%Y-%m-%d").date().strftime("%a %d %b %Y")
- print(f"Max: {peak['rate']} on:", format_date(peak_date))
- print(f"Max: {depression['rate']} on:", format_date(depression_date))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement