Monstera

projektB - changed

Jan 15th, 2023 (edited)
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. import json
  2. import urllib
  3. from urllib.request import urlopen
  4. from datetime import datetime
  5.  
  6.  
  7. def date_format(date: str):  # making format of date 'better'
  8. weekday = date[0:4]
  9.     day = date[4:6]
  10.     month_and_year = date[6:15]
  11.  
  12.     day_suffix = "th"
  13.  
  14.     if day == '01' or day == '21' or day == '31':
  15.         suffix = "st"
  16.     elif day == '02' or day == '22':
  17.         suffix = "nd"
  18.     elif day == '03' or day == '23':
  19.         suffix = "rd"
  20.  
  21.     if date[4] == '0':
  22.         return date[9] + day_suffix
  23.  
  24.     return weekday + day + day_suffix + month_and_year
  25.  
  26. max_date = " "
  27. max_value = 0
  28. min_date = ""
  29. min_value = 10
  30.  
  31. url = "https://api.nbp.pl/api/exchangerates/rates/a/chf/2022-01-01/2023-01-01/?format=json"
  32.  
  33. with urllib.request.urlopen(url_address) as url:
  34.     data = json.load(url)
  35.  
  36.     days = []
  37.     for row in data["rates"]:
  38.         day = {"date": row["effectiveDate"], "rate": row["mid"]}
  39.         days.append(day)
  40.  
  41.     peak = max(days, key=lambda day: day["rate"])
  42.     depression = min(days, key=lambda day: day["rate"])
  43.  
  44.     max_date = datetime.strptime(peak["date"], '%Y-%m-%d').date().strftime("%a %d %b %Y")
  45.     min_date = datetime.strptime(depression["date"], '%Y-%m-%d').date().strftime("%a %d %b %Y")
  46.  
  47.  
  48. print(date_format(max_date), peak["rate"])
  49. print(date_format(min_date), depression["rate"])
Advertisement
Add Comment
Please, Sign In to add comment