-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrypto.py
200 lines (167 loc) · 7.01 KB
/
Crypto.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import os
import requests
from bs4 import BeautifulSoup
import asyncio
import time
import msvcrt
import sys
class Crypto:
Coins=['bitcoin','ethereum' ,'litecoin' ,'dogecoin','cardano','xrp', 'solana' ]
url :str
Cryptos= dict()
def __init__(self, *args , **kwargs) :
pass
async def request(self):
self.HTML= requests.get(self.url)
async def Dump(self):
for i in self.Coins:
self.url = f'https://arzdigital.com/coins/{i}'
await self.request()
soup = BeautifulSoup(self.HTML.text , 'html.parser')
temp= soup.find('div' , attrs={'class':'coinPrice'}).text
self.Cryptos[i] = temp
await asyncio.sleep(0.01)
class Calculate(Crypto):
Assets: any
def __init__(self, Assets, *args , **kwargs) -> None:
self.Assets= Assets
asyncio.run(self.main())
async def showUserAssets(self):
"""
changing user crypto assets type from a string to a dictionary
show user current price of cryptos and his assets
"""
if type(self.Assets)!=dict:
Temp_user_Dict= dict()
self.Assets=self.Assets.split(',')
for i in self.Assets:
Value, Key= i.split()
Temp_user_Dict[Key]= Value
self.Assets= Temp_user_Dict
temp_asset= str()
while True:
await asyncio.sleep(0.01)
j=0
os.system('cls')
for i in self.Cryptos.keys():
for asset in self.Assets.keys():
if i==asset:
temp_asset= asset
print(f'{j+1}. {i} : {self.Cryptos[i]} , You own {self.Assets[i]} {i} that worth ${self.calculate(self.Cryptos[i], self.Assets[i]):.2f}')
j+=1
if temp_asset!= i:
print(f'{j+1}. {i} : {self.Cryptos[i]}')
j+=1
if j==7:
break
def calculate(self, CryptoValue, Asset):
Asset= float(Asset)
return (float(''.join(CryptoValue.split('$').pop(1).split(','))) * Asset )
async def main(self):
start= time.time()
task3 = asyncio.create_task(self.showUserAssets())
task1= asyncio.create_task(self.Dump())
task2= asyncio.create_task(self.request())
await task1
await task3
end = time.time()
print(end-start)
class Market(Calculate):
User_Money= int
def __init__(self, money , assets , *args , **kwargs):
super().__init__(assets)
self.User_Money= money
self.PlayerChoose()
@property
def money(self):
return self.User_Money
@money.setter
def money(self, value):
if value ==int:
self.User_Money= value
def PlayerChoose(self):
while True:
self.Player_Choose= str()
while True:
sys.stdout.write("\x1b[2K")
sys.stdout.write("\x1b[1A")
print("\nPlease choose : ", self.Player_Choose,end='')
temp_Choose= msvcrt.getch()
temp_Choose= temp_Choose.decode('ASCII')
if temp_Choose== "\x1b":
break
elif temp_Choose== "\x08":
self.Player_Choose= self.Player_Choose[:-1]
elif temp_Choose.isnumeric() :
self.Player_Choose+= temp_Choose
else:
break
try:
if temp_Choose=="\x1b":
break
if 0<int(self.Player_Choose)<8:
self.Player_Choose=int(self.Player_Choose)
self.ShoppingCrypto()
break
else:
print("\nPlease choose a number between 1 and 7")
except:
print("\nPlease choose a number between 1 and 7")
def ShoppingCrypto(self):
The_Crypto= self.Coins[self.Player_Choose-1]
os.system('cls')
print(f'{The_Crypto} : {self.Cryptos[The_Crypto]}')
print(f'You have : ${self.User_Money}')
The_bool= False
for i in self.Assets.keys():
if The_Crypto== i:
The_bool= True
while True:
os.system('cls')
if The_bool:
print("What do you want to do?\n1.Buy\n2.sell")
else:
print("What do you want to do?\n1.Buy")
temp_Choose= msvcrt.getch().decode('ASCII')
if temp_Choose=='1':
while True:
os.system('cls')
print(f'{The_Crypto} : {self.Cryptos[The_Crypto]}\nhow much do you want to buy? ')
The_Amount= input()
if The_Amount.isnumeric():
The_Amount=float(The_Amount)
break
os.system('cls')
print(f'You are going to buy {The_Amount} {The_Crypto} for { self.calculate(self.Cryptos[The_Crypto],The_Amount)}\nAre You Sure?\n1.Yes\n2.No')
temp_Choose1= msvcrt.getch().decode('ASCII')
if temp_Choose1=='1':
Cost= self.calculate(self.Cryptos[The_Crypto],The_Amount)
self.User_Money-= Cost
if The_Crypto in self.Assets.keys():
self.Assets[The_Crypto]= float(self.Assets[The_Crypto])+The_Amount
break
else:
self.Assets[The_Crypto]=str(The_Amount)
break
else:break
if temp_Choose=='2':
while True:
os.system('cls')
print(f'{The_Crypto} : {self.Cryptos[The_Crypto]} (You have {self.Assets[The_Crypto]}) \nhow much do you want to sell? ')
The_Amount= input()
if The_Amount.isnumeric():
The_Amount=float(The_Amount)
break
os.system('cls')
print(f'You are going to sell {The_Amount} {The_Crypto} for { self.calculate(self.Cryptos[The_Crypto],The_Amount)}\nAre You Sure?\n1.Yes\n2.No')
temp_Choose1= msvcrt.getch().decode('ASCII')
if temp_Choose1=='1':
Cost= self.calculate(self.Cryptos[The_Crypto],The_Amount)
self.User_Money+= Cost
if The_Crypto in self.Assets.keys():
self.Assets[The_Crypto]= float(self.Assets[The_Crypto])-The_Amount
break
else:break
if temp_Choose=="\x1b":
break
self.__init__(self.User_Money, self.Assets)