askanton

Підключення до api #monobank | Вивчаємо #python

Mar 12th, 2023
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. from json import loads as jloads
  2. from datetime import datetime
  3. from requests import get
  4. import base64
  5. import pprint
  6.  
  7. api_token = "****************************************"  # API монобанку
  8.  
  9.  
  10. def b64decode(text):  # функція для декодування
  11.     result = base64.b64decode(text.encode("ascii"))
  12.     return result.decode("ascii")
  13.  
  14.  
  15. def timenow():  # функція для отримання поточного часу
  16.     return datetime.today().strftime("%B %d %H:%M:%S")
  17.  
  18.  
  19. def get_currency():  # отримуємо курси валют та записуємо в файл, токен не потрібен
  20.     req = get("https://api.monobank.ua/bank/currency").text
  21.     if "errorDescription" not in req:
  22.         with open("currency.json", "w") as f:
  23.             f.write(req)
  24.         print(f'{timenow()}: Done')
  25.     else:
  26.         print(f'{timenow()}: Error')
  27.  
  28.  
  29. def print_currency():  # виводимо цікаві для нас валюти в консоль
  30.     with open("currency.json", "r") as f:
  31.         cur = jloads(f.read())
  32.     res = str('Купівля/Продаж')
  33.     res += f'\nUSD: {cur[0]["rateBuy"]}/{cur[0]["rateSell"]}'
  34.     res += f'\nEUR: {cur[1]["rateBuy"]}/{cur[1]["rateSell"]}'
  35.     return res
  36.  
  37.  
  38. def get_info():  # отримуємо баланс по рахунках, потрібен токен
  39.     api = get("https://api.monobank.ua/personal/client-info", headers={'X-Token': api_token}).json()
  40.     pprint.pprint(api)
  41.  
  42.  
  43. # get_currency()  
  44. # print(print_currency())  
  45. get_info()
Add Comment
Please, Sign In to add comment