-
Notifications
You must be signed in to change notification settings - Fork 1
/
mempool_ord.py
126 lines (109 loc) · 4.02 KB
/
mempool_ord.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
import json
import sys
import threading
import time
from decimal import Decimal
from http.client import CannotSendRequest
from pathlib import Path
from common import InscriptionContent, OrdinalTx, RawProxy, rpc_connection
from logger import get_logger
from mempool_listen import yield_new_txs
class DecimalEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Decimal):
return str(obj)
return super(DecimalEncoder, self).default(obj)
HERE = Path(__file__).parent
log_file_path = HERE / "mempool_ord.log"
logger = get_logger(__file__, log_file_path)
data_dir = HERE / "static" / "pictures"
ordinals_processed = 0
conn = rpc_connection()
def main_listening():
global conn
for index, tx in enumerate(yield_new_txs()):
if index % 100 == 0:
logger.info(f"index - {index} - {tx.tx_id}")
exception_there = False
while True:
try:
inscription = tx.get_inscription(conn)
if exception_there:
logger.info(f"Recovered from error {tx.tx_id}")
break
except CannotSendRequest:
logger.error("Cannot send request")
except Exception as e:
logger.exception(f"Exception main_listening {e}")
conn = rpc_connection()
exception_there = True
time.sleep(1)
if inscription is None:
continue
global ordinals_processed
ordinals_processed += 1
if ordinals_processed % 50 == 0:
logger.info(f"Ordinal processed - {ordinals_processed} - {tx.tx_id}")
if not inscription.content_type.startswith("image"):
continue
processing_thread = threading.Thread(
target=do_process_ordinal, args=(inscription, tx)
)
logger.info(f"Starting processing_thread for {tx.tx_id}")
processing_thread.start()
def do_process_ordinal(inscription: InscriptionContent, tx: OrdinalTx) -> None:
global conn
exception_there = False
while True:
try:
process_image_ordinal(inscription, tx, conn)
if exception_there:
logger.info(f"Recovered from error {tx.tx_id}")
break
except CannotSendRequest:
logger.error("Cannot send request")
except Exception as e:
logger.exception(f"Exception main_listening {e}")
conn = rpc_connection()
exception_there = True
time.sleep(1)
def process_image_ordinal(
inscription: InscriptionContent, tx: OrdinalTx, conn: RawProxy
) -> None:
logger.info(f"tx - {tx}")
logger.info(f"inscription - {inscription}")
file_suffix = inscription.content_type.split("/")[-1]
if file_suffix == "svg+xml":
file_suffix = "svg"
file_name = f"{tx.tx_id}.{file_suffix}"
data_file = data_dir / file_name
json_file = data_dir / f"{file_name}.json"
if not data_file.exists() or data_file.stat().st_size == 0:
with open(data_file, "wb") as f:
f.write(inscription.payload)
data = tx.to_dict_without_witness(conn)
data["content_type"] = inscription.content_type
data["content_hash"] = inscription.content_hash
data["content_length"] = inscription.content_length
data["timestamp"] = int(time.time())
data["datetime"] = time.strftime(
"%Y-%m-%d %H:%M:%S UTC", time.gmtime(int(time.time()))
)
if not json_file.exists() or json_file.stat().st_size == 0:
with open(json_file, "w") as f:
json.dump(data, f, indent=1, cls=DecimalEncoder)
logger.info(f"Files saved - {data_file}")
if __name__ == "__main__":
logger.info("Starting main_listening")
while True:
try:
main_listening()
except KeyboardInterrupt:
logger.info("Stopping...")
sys.exit(0)
except Exception as e:
logger.exception(f"Exception {e}")
logger.info("Recovering from error...")
conn = rpc_connection()
time.sleep(1)
continue