forked from revoxhere/duino-coin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duco_api.py
133 lines (117 loc) · 4.21 KB
/
duco_api.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
##########################################
# Duino-Coin Api Module
# https://github.com/revoxhere/duino-coin
# Distributed under MIT license
# © Duino-Coin Community 2021
##########################################
import socket
from requests import get
from threading import Timer
API_URL = "http://51.15.127.80/api.json"
SERVER_URL = "https://raw.githubusercontent.com/revoxhere/duino-coin/gh-pages/serverip.txt"
duco_price = 0.003
socket.setdefaulttimeout(10)
def decode_response(rec):
"""
Decodes a response from the server
"""
return rec.decode().split(",")
def start_duco_price_timer(tkinter_label=None, interval=15):
"""
A function that starts a timer with a specified interval and updates duco_price variable with the current price.
Arguments:
tkinter_label: Tkinter label that will be updated with the price (optional)
interval: Interval between price updates (default: 15)
"""
global duco_price
api_response = get(API_URL)
if api_response.status_code == 200:
duco_price = round(api_response.json()["Duco price"], 6)
else:
duco_price = .003
if tkinter_label:
tkinter_label.set(f"1 Duco = ${duco_price}")
Timer(interval, start_duco_price_timer, args=(tkinter_label, interval)).start()
def get_duco_price():
"""
A function for getting the current price of DUCO
"""
api_response = get(API_URL)
if api_response.status_code == 200:
duco_price = round(api_response.json()["Duco price"], 6)
else:
duco_price = .003
return duco_price
class api_actions:
"""
A class that provides an interface for interacting with the DUCO server
"""
def __init__(self):
"""
A class constructor that initiates the connection with the server.
"""
serverinfo = get(SERVER_URL).text.splitlines()
self.pool_address = serverinfo[0]
self.pool_port = int(serverinfo[1])
self.sock = socket.socket()
self.sock.connect((self.pool_address, self.pool_port))
self.sock.recv(3)
self.username = None
self.password = None
def register(self, username, password, email):
"""
A function for registering an account
"""
self.sock.send(f"REGI,{username},{password},{email}".encode())
register_result = decode_response(self.sock.recv(128))
if 'NO' in register_result:
raise Exception(register_result[1])
return register_result
def login(self, username, password):
"""
A function for logging into an account
"""
self.username = username
self.password = password
self.sock.send(f"LOGI,{username},{password}".encode())
login_result = decode_response(self.sock.recv(64))
if 'NO' in login_result:
raise Exception(login_result[1])
return login_result
def logout(self):
"""
A function for disconnecting from the server
"""
self.sock.close()
def balance(self):
"""
A function for getting account balance
"""
if not self.password or not self.username:
raise Exception("User not logged in")
self.sock.send("BALA".encode())
user_balance = self.sock.recv(1024).decode()
return user_balance
def transfer(self, recipient_username, amount):
"""
A function for transfering balance between two accounts
"""
if not self.password or not self.username:
raise Exception("User not logged in")
self.sock.send(f"SEND,-,{recipient_username},{amount}".encode())
transfer_response = self.sock.recv(128).decode()
return transfer_response
def reset_pass(self, old_password, new_password):
"""
A function for resetting the password of an account
"""
if not self.password or not self.username:
raise Exception("User not logged in")
self.sock.send(f"CHGP,{old_password},{new_password}".encode())
reset_password_response = self.sock.recv(128).decode()
return reset_password_response
def close(self):
"""
A function for disconnecting from the server
"""
self.sock.close()