-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.py
109 lines (94 loc) · 3.24 KB
/
operations.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
from urllib import request as urlrequest
import ssl
import json
import sys
import sqlite3
import time
import mysql.connector
mysqldb = mysql.connector.connect(
host="x",
user="xx",
password="Xxx",
database="xxxx"
)
mysqlcursor = mysqldb.cursor()
connection=sqlite3.connect("progress.db")
cursor = connection.cursor()
def get_json(url):
req = urlrequest.Request(url)
req.set_proxy('10.77.32.83:3128', 'http')
gcontext = ssl.SSLContext()
error_hap=False
while True:
try:
response = urlrequest.urlopen(req,context=gcontext)
except:
print(f"Error sleeping - URL={url}")
error_hap=True
time.sleep(60)
pass
else:
if error_hap:
print("Continuing...")
break
res=response.read().decode('utf8')
jres=json.loads(res)
return jres
jurl=get_json("https://api.testnet.minepi.com/")
latest_ledger=jurl['core_latest_ledger']
def initialize_database():
print("Initalizaing Database")
cursor.execute("""CREATE TABLE IF NOT EXISTS age (
id INTEGER,
cnt INTEGER DEFAULT 0);""")
cursor.execute("insert into age (id,cnt) values (455,0)")
connection.commit()
#check if table exists
cursor.execute("SELECT count(name) FROM sqlite_master WHERE type='table' AND name='age'")
if cursor.fetchone()[0] !=1:
initialize_database()
#read values
cursor.execute("select * from age")
db_res=cursor.fetchone()
start_ledger=db_res[0]
count=db_res[1]
print(f"start:{start_ledger} count:{count}")
for ledger_id in range(start_ledger+1,latest_ledger):
#fetch ledger
ledger=get_json(f"https://api.testnet.minepi.com/ledgers/{ledger_id}/operations")
basenet=get_json("https://api.testnet.minepi.com/")
latest=basenet['core_latest_ledger']
if len(ledger['_embedded']['records']) > 0:
#not empty
record_array=ledger['_embedded']['records']
for record in record_array:
if record['type'] == "create_account":
count=count+1
cursor.execute(f"update age set id='{ledger_id}', cnt={count}")
print(f"Ledger {ledger_id} Op_id:{record['id']} cnt={count}")
#sys.exit()
if int(count)%100 == 0:
connection.commit()
print(f"committed at {count}")
try:
mysqlcursor = mysqldb.cursor()
except Exception as e:
print(f" ERROR: {e}")
else:
mysqlcursor.execute(f"update testnet set ledger='{ledger_id}',wallets='{count}', latest='{latest}' where id='1'")
mysqldb.commit()
else:
#just update ledgerid
cursor.execute(f"update age set id='{ledger_id}'")
#print(f"{ledger_id} not empty")
#sys.exit()
if int(ledger_id)%100 == 0:
connection.commit()
print(f"committed at Ledger:{ledger_id}")
try:
mysqlcursor = mysqldb.cursor()
except Exception as e:
print(f" ERROR: {e}")
else:
mysqlcursor.execute(f"update testnet set ledger='{ledger_id}',wallets='{count}', latest='{latest}' where id='1'")
mysqldb.commit()