forked from revoxhere/duino-coin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duco_api.py
111 lines (84 loc) · 3.49 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
##########################################
# Duino-Coin Api Module
# https://github.com/revoxhere/duino-coin
# Distributed under MIT license
# © Duino-Coin Community 2021
##########################################
from urllib.request import urlopen
import socket
from requests import get
from json import loads
from threading import Timer
socket.setdefaulttimeout(10)
ducofiat = .003
def decode_soc(rec):
response = rec.decode("utf8")
response = response.split(",")
return response
def decode_soc_no_utf(rec):
response = rec.decode()
response = response.split(",")
return response
def GetDucoPrice():
global ducofiat
jsonapi = get("https://raw.githubusercontent.com/revoxhere/duco-statistics/master/api.json", data = None)
if jsonapi.status_code == 200:
content = jsonapi.content.decode()
contentjson = loads(content)
ducofiat = round(float(contentjson["Duco price"]), 6)
else:
ducofiat = .003
Timer(15, GetDucoPrice).start()
class api_actions():
def __init__(self):
"""
Initiate connection with socket server.
This is to initiate the connection with the server.
args: none
"""
with urlopen("https://raw.githubusercontent.com/revoxhere/duino-coin/gh-pages/serverip.txt") as self.content:
self.content = self.content.read().decode().splitlines()
self.pool_address = self.content[0]
self.pool_port = int(self.content[1])
socket.setdefaulttimeout(10)
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, send_email=False):
self.sock.send(bytes(f"REGI,{str(username)},{str(password)},{str(email)}", encoding="utf8"))
self.register_result = decode_soc(self.sock.recv(128))
if 'NO' in self.register_result:
raise Exception(self.register_result[1])
return self.register_result
def login(self, username, password):
self.username = username
self.password = password
self.sock.send(bytes(f"LOGI,{str(username)},{str(password)}", encoding="utf8"))
self.login_result = decode_soc(self.sock.recv(64))
if 'NO' in self.login_result:
raise Exception(self.login_result[1])
return self.login_result
def logout(self):
self.sock.close()
def balance(self):
if self.password == None or self.username == None:
raise Exception("User not logged in")
self.sock.send(bytes("BALA", encoding="utf8"))
self.user_balance = self.sock.recv(1024).decode()
return self.user_balance
def transfer(self, recipient_username, amount):
if self.password == None or self.username == None:
raise Exception("User not logged in")
self.sock.send(bytes(f"SEND,-,{str(recipient_username)},{str(amount)}", encoding="utf8"))
self.transfer_response = self.sock.recv(128).decode()
return self.transfer_response
def reset_pass(self, old_password, new_password):
if self.password == None or self.username == None:
raise Exception("User not logged in")
self.sock.send(bytes(f"CHGP,{str(old_password)},{str(new_password)}", encoding="utf8"))
self.reset_password_response = self.sock.recv(128).decode("utf8")
return self.reset_password_response
def close(self):
self.sock.close()