Skip to content

Instantly share code, notes, and snippets.

@Axik
Created March 9, 2023 16:42
Show Gist options
  • Save Axik/31ce159e04199bb2c2b8491443980dd2 to your computer and use it in GitHub Desktop.
Save Axik/31ce159e04199bb2c2b8491443980dd2 to your computer and use it in GitHub Desktop.
Приклад лотереї з монобанкі
import json
import uuid
import math
import quantumrandom
# Підставьте ім'я своєї виписки
with open("vipyska_.json") as f:
statement_list = json.loads(f.read())
def test_json_file():
assert isinstance(statement_list, list)
assert isinstance(statement_list[0], dict)
assert statement_list[0].keys() == set(
[
"id",
"time",
"description",
"mcc",
"originalMcc",
"amount",
"operationAmount",
"currencyCode",
"commissionRate",
"cashbackAmount",
"balance",
"hold",
]
), statement_list[0].keys()
test_json_file()
def round_down(n, decimals=0):
multiplier = 10**decimals
return math.floor(n * multiplier) / multiplier
def produce_tickets(input_statement_list):
lottery_tickets = {}
for item in input_statement_list:
number_of_tickets = int(
round_down(item["operationAmount"] / 100.0 / 100.0)
) # спочатку копіки потім 100 грн квиток
for _ in range(number_of_tickets):
lottery_tickets[str(uuid.uuid4())] = {
"id": item["id"],
"description": item["description"],
}
return lottery_tickets
def test_produce_tickets():
fixture_record = {
"id": "iXGA0Coz9tYdW1beRDWQownlfDZvo3WZKCkOicdTRg",
"time": 1677848502,
"description": "Від: Мото Кроссовіч",
"mcc": 4829,
"originalMcc": 4829,
"amount": 10000,
"operationAmount": 10000,
"currencyCode": 980,
"commissionRate": 0,
"cashbackAmount": 0,
"balance": 2468292,
"hold": False,
}
fixture_record2 = {
"id": "iXGA0Coz9tYdW1beRDWQownlfDZvo3WZKCkOicd123",
"time": 1677848513,
"description": "Від: Ямахус 250",
"mcc": 4829,
"originalMcc": 4829,
"amount": 20000,
"operationAmount": 20000,
"currencyCode": 980,
"commissionRate": 0,
"cashbackAmount": 0,
"balance": 2468292,
"hold": False,
}
fixture_record3 = {
"id": "iXGA0Coz9tYdW1beRDWQownlfDZvo3WZKCkOicd456",
"time": 1677848456,
"description": "Від: Кавасакіус 450",
"mcc": 4829,
"originalMcc": 4829,
"amount": 10000,
"operationAmount": 10000,
"currencyCode": 980,
"commissionRate": 0,
"cashbackAmount": 0,
"balance": 2468292,
"hold": False,
}
fixture_record4 = {
"id": "iXGA0Coz9tYdW1beRDWQownlfDZvo3WZKCkOicd789",
"time": 1677848512,
"description": "Від: Тест Тестовіч",
"mcc": 4829,
"originalMcc": 4829,
"amount": 50000,
"operationAmount": 50000,
"currencyCode": 980,
"commissionRate": 0,
"cashbackAmount": 0,
"balance": 2468292,
"hold": False,
}
# WHEN I PASS THOSE 4 RECORDS AS A LIST
result = produce_tickets(
[fixture_record, fixture_record2, fixture_record3, fixture_record4]
)
# SHOULD GET 9 TOTAL NUMBER OF TICKETS
assert len(result.keys()) == 9, result
# SHOULD GET 5 TICKET FOR ТЕСТ ТЕСТВІЧ
assert (
len(
list(
filter(
lambda x: x["description"] == "Від: Тест Тестовіч", result.values()
)
)
)
== 5
)
# SHOULD GET 1 TICKET FOR МОТО КРОСОВІЧ
assert (
len(
list(
filter(
lambda x: x["description"] == "Від: Мото Кроссовіч", result.values()
)
)
)
== 1
)
# SHOULD GET 2 TICKETS FOR Ямахус 250
assert (
len(
list(
filter(lambda x: x["description"] == "Від: Ямахус 250", result.values())
)
)
== 2
)
test_produce_tickets()
def random_choice(input_lottery_tickets):
tickets = list(input_lottery_tickets.keys())
lucky_number = int(
quantumrandom.randint(0, len(tickets))
) # py3 compatibility. quantumrandom returns float >.<
lucky_ticket_id = tickets[lucky_number]
lucky_ticket = input_lottery_tickets[lucky_ticket_id]
return lucky_ticket
def test_random_choice():
fixture_record = {
"time": 1677848456,
"description": "Від: Мото Кроссовіч",
"operationAmount": 15000,
"id": "iXGA0Coz9tYdW1beRDWQownlfDZvo3WZKCkOicd123",
}
fixture_record2 = {
"time": 1677848456,
"description": "Від: Багатий Мото Кроссовіч",
"operationAmount": 500000,
"id": "iXGA0Coz9tYdW1beRDWQownlfDZvo3WZKCkOicd456",
}
fixture_record3 = {
"time": 1677848456,
"description": "Від: Ямахус Кавовіч",
"operationAmount": 7000,
"id": "iXGA0Coz9tYdW1beRDWQownlfDZvo3WZKCkOicd789",
}
fixture_record4 = {
"time": 1677848456,
"description": "Від: Пан Ендуро",
"operationAmount": 20000,
"id": "iXGA0Coz9tYdW1beRDWQownlfDZvo3WZKCkOicd101",
}
# WHEN I PASS THOSE 4 RECORDS AS A LIST
fixture_lottery_tickets = produce_tickets(
[fixture_record, fixture_record2, fixture_record3, fixture_record4]
)
lucky_ticket = random_choice(fixture_lottery_tickets)
assert isinstance(lucky_ticket, dict)
assert "id" in lucky_ticket
assert "description" in lucky_ticket
test_random_choice()
tickets = produce_tickets(statement_list)
lucky_ticket = random_choice(tickets)
print(lucky_ticket)
import datetime
import time
import requests
import pprint
# Вставьте ваш токен з https://api.monobank.ua/
api_token = ""
test_connect_response = requests.get("https://api.monobank.ua/personal/client-info", headers={"X-Token": f"{api_token}"})
assert test_connect_response.status_code == 200, test.content
pprint.pprint(test_connect_response.json())
# !!! Пройдіться по виведеному в консоль і подивіться який id в банкє з якої ви хочете взяті виписку
account = ""
# Тут треба вставити початку дату з якої береться виписка
# Документація тут
# https://api.monobank.ua/docs/#tag/Kliyentski-personalni-dani/paths/~1personal~1statement~1{account}~1{from}~1{to}/get
from_ = int(time.mktime(datetime.datetime(2023, 3, 3, 0, 0).timetuple()))
to = ""
vipyska_response = requests.get(f"https://api.monobank.ua/personal/statement/{account}/{from_}", headers={"X-Token": f"{api_token}"})
if vipyska_response.ok:
with open(f"vipyska_{account}.json", "wb") as f:
f.write(vipyska_response.content)
else:
print(vipyska_response.content)
certifi==2022.12.7
charset-normalizer==3.1.0
idna==3.4
quantumrandom==1.9.0
requests==2.28.2
urllib3==1.26.14
@goliney
Copy link

goliney commented Mar 18, 2023

quantumrandom працює через раз:

urllib.error.HTTPError: HTTP Error 500: Internal Server Error

@Axik
Copy link
Author

Axik commented Mar 20, 2023

Нажаль так — ця бібліотека стара і використовує старий сервіс і не приймає апдейти

@Axik
Copy link
Author

Axik commented Mar 20, 2023

In [4]: requests.get(URL, params={'type': 'uint16', 'array_length': 1, 'block_size': 1})
Out[4]: <Response [200]>

In [5]: test = requests.get(URL, params={'type': 'uint16', 'array_length': 1, 'block_size': 1})

In [6]: test
Out[6]: <Response [500]>

In [7]: test.content
Out[7]: b'The QRNG API is limited to 1 requests per minute. For more requests, please visit https://quantumnumbers.anu.edu.au or contact [email protected].'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment