Repository navigation
Expand file tree
/
Copy pathmonitor_sync.py
More file actions
174 lines (145 loc) · 5.93 KB
/
Copy pathmonitor_sync.py
File metadata and controls
174 lines (145 loc) · 5.93 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import json
from websocket import create_connection
import os
from datetime import datetime
import time
from tqdm import tqdm
import random
import argparse
# Relay URLs (same as sync script)
RELAY_URL = "wss://relay.mostr.pub"
NEWS_URL = "wss://relay.nos.social"
MATCHING_NHEX_FILE = "matching_nhex.txt"
def parse_arguments():
"""Parse command line arguments"""
parser = argparse.ArgumentParser(description='Monitor nostr sync status')
parser.add_argument('--interactive', action='store_true',
help='Run in interactive mode with detailed output')
return parser.parse_args()
def check_file_growth():
"""Monitor the growth of matching_nhex.txt"""
if not os.path.exists(MATCHING_NHEX_FILE):
return {"status": "error", "message": "matching_nhex.txt not found"}
# Get current stats
current_size = os.path.getsize(MATCHING_NHEX_FILE)
with open(MATCHING_NHEX_FILE, "r") as file:
current_count = sum(1 for _ in file)
# Store stats for next comparison
stats = {
"timestamp": datetime.now().isoformat(),
"size_bytes": current_size,
"line_count": current_count
}
return stats
def verify_event_sync(pubkey):
"""Check if events from a pubkey exist on both relays"""
results = {"source": 0, "destination": 0, "matching": 0}
try:
# Check source relay with timeout
ws_source = create_connection(RELAY_URL, timeout=10)
source_events = set()
request = json.dumps(["REQ", "source_check", {
"authors": [pubkey],
"limit": 10
}])
ws_source.send(request)
timeout = time.time() + 10 # 10 second timeout
while time.time() < timeout:
response = ws_source.recv()
data = json.loads(response)
if data[0] == "EOSE":
break
if data[0] == "EVENT":
source_events.add(data[2]["id"])
ws_source.close()
results["source"] = len(source_events)
# Check destination relay with timeout
if source_events:
ws_dest = create_connection(NEWS_URL, timeout=10)
dest_events = set()
request = json.dumps(["REQ", "dest_check", {
"ids": list(source_events)
}])
ws_dest.send(request)
timeout = time.time() + 10
while time.time() < timeout:
response = ws_dest.recv()
data = json.loads(response)
if data[0] == "EOSE":
break
if data[0] == "EVENT":
dest_events.add(data[2]["id"])
ws_dest.close()
results["destination"] = len(dest_events)
results["matching"] = len(source_events.intersection(dest_events))
except Exception as e:
error_msg = str(e)
if "timed out" in error_msg.lower():
if "mostr.pub" in error_msg:
return {"error": "relay.mostr.pub offline"}
elif "nos.social" in error_msg:
return {"error": "relay.nos.social offline"}
return {"error": str(e)}
return results
def main():
start_time = time.time() # Add timer at start
args = parse_arguments()
is_interactive = args.interactive
# Check file growth
file_stats = check_file_growth()
total_users = file_stats['line_count']
# Sample random pubkeys for verification
with open(MATCHING_NHEX_FILE, "r") as file:
pubkeys = file.readlines()
sample_size = min(100, len(pubkeys))
sample_pubkeys = random.sample(pubkeys, sample_size)
# Always show progress bar, but with different formats for interactive/non-interactive
if is_interactive:
progress_bar = tqdm(sample_pubkeys, desc="Checking pubkeys",
bar_format='{l_bar}{bar}| {n_fmt}/{total_fmt}')
else:
progress_bar = tqdm(sample_pubkeys, desc="Progress",
bar_format='{percentage:3.0f}% |{bar}| {n_fmt}/{total_fmt}')
missing_events = []
successful_checks = 0
for pubkey in progress_bar:
pubkey = pubkey.strip()
results = verify_event_sync(pubkey)
if "error" in results:
if is_interactive:
print(f"\nError: {results['error']}")
else:
print(f"ERROR:{results['error']}")
break # Stop checking if a relay is offline
elif results["matching"] == results["source"]:
successful_checks += 1
else:
missing_events.append({
"pubkey": pubkey,
"source": results["source"],
"destination": results["destination"],
"matching": results["matching"]
})
# Update the final output section
duration = time.time() - start_time
if is_interactive:
print("\nSync Status Report:")
print(f"- Number of Mastodon users fetched: {total_users}")
print(f"- Number of notes copied to relay.nos.social: {successful_checks}")
print(f"- Duration: {duration:.1f} seconds")
if missing_events:
print("\nMissing Events Detected:")
for event in missing_events:
print(f"\nPubkey: {event['pubkey'][:8]}...")
print(f"Source events: {event['source']}")
print(f"Destination events: {event['destination']}")
print(f"Matching events: {event['matching']}")
else:
# Simple output for non-interactive mode
print(f"- Number of Mastodon users fetched: {total_users}")
print(f"- Number of notes copied to relay.nos.social: {successful_checks}")
print(f"- Duration: {duration:.1f} seconds")
if missing_events:
print(f"SYNC_MISSING:{len(missing_events)}/{sample_size}")
if __name__ == "__main__":
main()