-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
62 lines (53 loc) · 2.24 KB
/
Copy pathutils.py
File metadata and controls
62 lines (53 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import requests
from datetime import datetime
from pprint import pprint
def get_data(url):
'''
Функция берет данные транзакции из JSON по ссылке и отдает сообщение прошло или вышла ошибка
'''
try:
response = requests.get(url)
if response.status_code == 200:
return response.json(), "INFO: Данные получены успешно\n"
return None, f"ERROR: status_code:{response.status_code}"
except requests.exceptions.ConnectionError:
return None, "ERROR: requests.exceptions.ConnectionError\n"
except requests.exceptions.JSONDecodeError:
return None, "ERROR: requests.exceptions.JSONDecodeError\n"
def get_filtered_data(data, filtered_empty_from=False):
'''
Функция фильтрует данные и оставляет только те, где есть EXECUTED
'''
data = [x for x in data if "state" in x and x ["state"] == "EXECUTED"]
if filtered_empty_from:
data = [x for x in data if "from" in x]
return data
def get_last_values(data, count_last_values):
'''
Функция оставляет последние 5 транзакций пользователей
'''
data = sorted(data, key=lambda x: x["date"], reverse=True)
data = data[:count_last_values]
return data
def get_formatted_data(data):
'''
Функция форматирует транзакции к нужному формату
'''
formatted_data = []
for row in data:
date = datetime.strptime(row["date"], "%Y-%m-%dT%H:%M:%S.%f").strftime("%d.%m.%Y")
description = row["description"]
from_info, from_bill = "", ""
if "from" in row:
sender = row["from"].split()
from_bill = sender.pop(-1)
from_bill = f"{from_bill[:4]} {from_bill[4:6]}** **** {from_bill[-4:]}"
from_info = " ".join(sender)
to = f"{row['to'].split()[0]} **{row['to'][-4:]}"
operation_amount = f"{row['operationAmount']['amount']} {row['operationAmount']['currency']['name']}"
formatted_data.append(f"""\
{date} {description}
{from_info} {from_bill} -> {to}
{operation_amount}
""")
return formatted_data