-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
140 lines (106 loc) · 4.64 KB
/
main.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
import requests
import json
import mysql.connector # pip install mysql-connector-python
from pycoingecko import CoinGeckoAPI
from config import config
def get_price():
cg = CoinGeckoAPI('')
bitsong_price = cg.get_price(ids='bitsong', vs_currencies='usd')
price = bitsong_price['bitsong']['usd']
return price
def get_height():
abci_info_url = 'https://bitsong-archive.validatrium.club/abci_info'
try:
response = requests.get(abci_info_url, timeout=5)
response.raise_for_status()
clean_data = json.loads(response.text)
height = clean_data['result']['response']['last_block_height']
print(f'Current height is {height}')
except requests.exceptions.HTTPError as errh:
print(errh)
except requests.exceptions.ConnectionError as errc:
print(errc)
except requests.exceptions.Timeout as errt:
print(errt)
except requests.exceptions.RequestException as err:
print(err)
return height
def get_validators_list():
validators_list_url = 'https://api.bitsong.interbloc.org/cosmos/staking/v1beta1/validators'
all_validators_data = {}
all_validators_list = []
count = 1
try:
response = requests.get(validators_list_url, timeout=5)
response.raise_for_status()
clean_data = json.loads(response.text)
for validator in clean_data['validators']:
valoper = validator['operator_address']
jailed = validator['jailed']
tokens = validator['tokens']
delegator_shares = validator['delegator_shares']
moniker = validator['description']['moniker']
website = validator['description']['website']
commission = validator['commission']['commission_rates']['rate']
validators_data = {
'valoper': valoper,
'jailed': jailed,
'tokens': tokens,
'moniker': moniker,
'website': website,
'commission': commission,
'delegator_shares': delegator_shares,
}
all_validators_data[count] = validators_data
all_validators_list.append((valoper, jailed, tokens, moniker, website, commission, delegator_shares))
count = count+1
print(f'{all_validators_data}')
return all_validators_list
except requests.exceptions.HTTPError as errh:
print(errh)
except requests.exceptions.ConnectionError as errc:
print(errc)
except requests.exceptions.Timeout as errt:
print(errt)
except requests.exceptions.RequestException as err:
print(err)
def add_to_database():
height = get_height()
price = get_price()
validators_list = get_validators_list()
try:
connection = mysql.connector.connect(**config)
mysql_insert_height_price = """INSERT INTO general_info (Height, Price)
VALUES
(%s, %s) """
mysql_insert_validators = """INSERT INTO validators (valoper, jailed, tokens, moniker, website, commission, delegator_shares)
VALUES
(%s, %s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE \
valoper = VALUES(valoper), \
moniker = VALUES(moniker), \
jailed = VALUES(jailed), \
commission = VALUES(commission), \
tokens = VALUES(tokens), \
website = VALUES(website), \
delegator_shares = VALUES(delegator_shares)
"""
record_1 = (height, price)
# record_2 = (validators_list['valoper'], validators_list['moniker'], validators_list['jailed'],
# validators_list['tokens'], validators_list['website'], validators_list['commission'])
record_2 = validators_list
cursor = connection.cursor()
cursor.execute(mysql_insert_height_price, record_1)
cursor.executemany(mysql_insert_validators, record_2)
connection.commit()
print(cursor.rowcount, "Record inserted successfully into Laptop table")
cursor.close()
except mysql.connector.Error as error:
print("Failed to insert record into table {}".format(error))
finally:
if connection.is_connected():
connection.close()
print("MySQL connection is closed")
if __name__ == '__main__':
# get_price_height()
# get_validators_list()
add_to_database()