1
0
mirror of https://github.com/osmarks/random-stuff synced 2024-12-28 02:50:33 +00:00
random-stuff/computercraft/kristdump.py

41 lines
1.3 KiB
Python
Raw Normal View History

2023-11-13 19:37:05 +00:00
import sqlite3
import time
import requests
from datetime import datetime, timezone
CHUNK = 1000
def do_query(offset):
return requests.get("https://krist.dev/transactions", params={"excludeMined": True, "limit": CHUNK, "offset": CHUNK * offset}).json()
conn = sqlite3.connect("krist.sqlite3")
conn.row_factory = sqlite3.Row
conn.executescript("""CREATE TABLE IF NOT EXISTS tx (
id INTEGER PRIMARY KEY,
fromaddr TEXT,
toaddr TEXT NOT NULL,
value INTEGER NOT NULL,
time INTEGER NOT NULL,
name TEXT,
meta TEXT,
sent_metaname TEXT,
sent_name TEXT
);""")
i = 0
while True:
results = do_query(i)
print(list(results.keys()))
if results.get("count") == 0:
print("done")
break
elif results["ok"] == False and "rate limit" in results.get("error", ""):
print(results.get("error"))
time.sleep(90)
elif results["ok"] == False:
print(results.get("error"))
else:
conn.executemany("INSERT INTO tx VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", [ (x["id"], x["from"], x["to"], x["value"], int(datetime.strptime(x["time"], "%Y-%m-%dT%H:%M:%S.%f%z").astimezone(timezone.utc).timestamp() * 1000), x["name"], x["metadata"], x["sent_metaname"], x["sent_name"]) for x in results["transactions"] ])
conn.commit()
i += 1