Created
March 2, 2020 09:12
-
-
Save mcxemic/be0a0ccb69a98bdbbfbd468651a48641 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
import requests | |
import typing as t | |
import time | |
from datetime import datetime, date | |
MONO_ENDPOINT = "https://api.monobank.ua/" | |
MY_TOKEN = "some_very_secret_token" | |
INVOICE_PATH = "/personal/statement/0/{from_ts}" | |
MSS_DICT = { | |
5411: "🛒 Groceries and supermarkets", | |
5814: "🌯 Fast food restaurants", | |
4829: "💸 (🏠 ) Wire transfers and money orders", | |
5812: "🍽 Eating places and restaurants", | |
5912: "💊 Drug stores and pharmacies", | |
4121: "🚕 Taxi-cabs and limousines", | |
5977: "💄 Cosmetic shops", | |
5192: "📚 Books, periodicals and newspapers", | |
5499: "🍌 Miscellaneous food shops", | |
6536: "🏧 Privat24", | |
7991: "🎭 Tourist attractions and exhibits", | |
5651: "👕 Family clothing shops", | |
7999: "🎢 Recreation services", | |
8099: "⚕️ Medical services and health practitioners", | |
7929: "🗞 Bands, orchestras and miscellaneous entertainers", | |
} | |
def get_currency() -> t.List[dict]: | |
currency_endpoint = '/bank/currency' | |
currency_resp = requests.get(MONO_ENDPOINT + currency_endpoint).json() | |
return currency_resp | |
def get_timestamp_this_month() -> int: | |
first_day_in_current_time = str(date.today().replace(day=1)) | |
ts_current_month = time.mktime( | |
datetime.strptime(first_day_in_current_time, "%Y-%m-%d").timetuple() | |
) | |
return int(ts_current_month) | |
def get_invoice() -> t.List[dict]: | |
ts = get_timestamp_this_month() | |
response = requests.get( | |
MONO_ENDPOINT + INVOICE_PATH.format(from_ts=ts), | |
headers={"X-Token": MY_TOKEN}, | |
) | |
return response.json() | |
def get_flow(invoice: t.List[dict]) -> float: | |
amount = sum( | |
[ | |
i.get("amount") | |
for i in invoice | |
if i.get("description") != 'Виплата депозиту' | |
] | |
) | |
return amount/100 | |
def get_flow_by_type( | |
invoice: t.List[dict], | |
group_by: t.Optional[str] = "mcc", | |
): | |
dd = defaultdict(float) | |
for i in invoice: | |
if i.get("description") != 'Виплата депозиту': | |
dd[i.get(group_by)] += round(float(i.get("amount")/100),2) | |
return dd | |
invoice = get_invoice() | |
flow = get_flow(invoice) | |
print(f"Витрати за цей місяць: {flow}") | |
flow_by_mcc = get_flow_by_type(invoice) | |
print(f'Витрати по mcc:') | |
for k, v in flow_by_mcc.items(): | |
print(f'{MSS_DICT.get(k)}: {v}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment