This repository was archived by the owner on Jan 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.py
More file actions
114 lines (96 loc) · 3.35 KB
/
Copy paththread.py
File metadata and controls
114 lines (96 loc) · 3.35 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
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
import mysql.connector
from mysql.connector import errorcode
from time import sleep
import datetime
from decimal import Decimal
currencies = ["botcoin", "esterium", "binguscoin", "floppacoin", "beans"]
def make_connection():
try:
cnx = mysql.connector.connect(
pool_name="mypool",
pool_size=1,
host="blsuvxgq3bvwh8qw4ah7-mysql.services.clever-cloud.com",
user="uf7gxtzihchkojup",
password="K1bhziQq9KnSPAVSnFdH",
database="blsuvxgq3bvwh8qw4ah7",
)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
return cnx, cnx.cursor()
def del_records(currency, cnx, cursor):
query1 = f"SELECT COUNT(*) from {currency}"
cursor.execute(query1)
result = cursor.fetchone()[0]
if result > 50:
query2 = f"DELETE FROM {currency} ORDER BY dates ASC LIMIT {result - 50}"
cursor.execute(query2)
cnx.commit()
def exch_r8_refresh(currency, cnx, cursor):
query1 = f"SELECT {currency} , ratio FROM {currency} ORDER BY dates DESC LIMIT 1"
cursor.execute(query1)
results = cursor.fetchone()
curr_exch_r8 = results[0]
curr_ratio = results[1]
query2 = f"SELECT {currency} FROM economy_data"
cursor.execute(query2)
n1 = 0
tot_crypto = 0
for i in cursor.fetchall():
tot_crypto += i[0]
n1 += 1
avg_crypto = tot_crypto / n1
query3 = "SELECT money FROM economy_data"
cursor.execute(query3)
n2 = 0
tot_money = 0
for i in cursor.fetchall():
tot_money += i[0]
n2 += 1
avg_money = tot_money / n2
if avg_money == 0:
avg_money = 1
ratio = round(avg_crypto / avg_money, 2)
if ratio > 5.00:
ratio = 5.00
if Decimal(curr_ratio) == Decimal(ratio):
return curr_exch_r8, curr_ratio
elif round(curr_exch_r8 * ratio) <= 1:
new_exch_r8 = 1
elif round(curr_exch_r8 * ratio) >= 9223372036854775807:
new_exch_r8 = 9223372036854775807
else:
new_exch_r8 = round(curr_exch_r8 * ratio)
return new_exch_r8, ratio
def exch_r8_loop():
while True:
for i in currencies:
cnx, cursor = make_connection()
cursor.execute(
f"SELECT dates FROM {i} ORDER BY dates DESC LIMIT 1")
results = cursor.fetchone()[0]
dt = datetime.datetime.strptime(results, "%Y-%m-%d %H:%M:%S")
print(i, dt)
if datetime.datetime.utcnow() >= (dt + datetime.timedelta(seconds=600)):
curr_exch_r8, ratio = exch_r8_refresh(i, cnx, cursor)
print(curr_exch_r8, ratio)
query = f"INSERT INTO {i}(dates, {i} , ratio) VALUES(%s,%s,%s)"
cursor.execute(
query,
(
datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"),
curr_exch_r8,
ratio,
),
)
cnx.commit()
del_records(i, cnx, cursor)
cnx.close()
else:
cnx.close()
sleep(1)
exch_r8_loop()