-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
68 lines (54 loc) · 2.13 KB
/
Main.py
File metadata and controls
68 lines (54 loc) · 2.13 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
import requests
import time
def get_latest_block_height():
url = "https://blockstream.info/api/blocks/tip/height"
response = requests.get(url)
return int(response.text)
def get_block_hash(height):
url = f"https://blockstream.info/api/block-height/{height}"
response = requests.get(url)
return response.text
def get_block_transactions(block_hash):
url = f"https://blockstream.info/api/block/{block_hash}/txs"
response = requests.get(url)
return response.json()
def extract_addresses(transactions):
addresses = set()
for tx in transactions:
# Inputs (vin)
if 'vin' in tx:
for vin in tx['vin']:
if 'prevout' in vin and vin['prevout'] and 'scriptpubkey_address' in vin['prevout']:
addresses.add(vin['prevout']['scriptpubkey_address'])
# Outputs (vout)
if 'vout' in tx:
for vout in tx['vout']:
if 'scriptpubkey_address' in vout:
addresses.add(vout['scriptpubkey_address'])
return addresses
def save_addresses_to_file(addresses):
fileW=open("ADD-LIST.txt","a+")
for address in sorted(addresses):
fileW.writelines(address+"\n")
fileW.close()
def main():
print("Fetching latest Bitcoin block height...")
latest_height = get_latest_block_height()
print("Latest block height:", latest_height)
all_addresses = set()
for i in range(20000):
block_height = int(latest_height) - i
print(f"Processing block {block_height}...")
try:
block_hash = get_block_hash(block_height)
transactions = get_block_transactions(block_hash)
addresses = extract_addresses(transactions)
all_addresses=addresses
save_addresses_to_file(all_addresses)
time.sleep(1) # محدودیت نرخ درخواست
except Exception as e:
print("Error at block", block_height, ":", str(e))
continue
save_addresses_to_file(all_addresses)
if __name__ == "__main__":
main()