-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbtc_block_explorer.py
More file actions
92 lines (66 loc) · 2.71 KB
/
Copy pathbtc_block_explorer.py
File metadata and controls
92 lines (66 loc) · 2.71 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
import json
from urllib.request import urlopen
import time
# List of APIs for redundancy
API_LIST = [
"https://blockchain.info/rawaddr/{address}?offset={offset}",
"https://api.blockchair.com/bitcoin/dashboards/address/{address}?offset={offset}"
]
def fetch_data_with_pagination(api_url, address):
"""Fetch all transactions for an address, dynamically handling pagination."""
transactions = []
offset = 0
while True:
try:
# Format the URL with the address and offset
url = api_url.format(address=address, offset=offset)
print(f"Fetching data: {url}")
# Fetch data from the API
response = urlopen(url, timeout=30)
data = json.loads(response.read().decode('utf-8'))
# Extract transactions from API response
if "blockchain.info" in api_url:
# Blockchain.info response structure
txs = data.get('txs', [])
transactions.extend(txs)
if not txs: # Stop if no more transactions are returned
break
elif "blockchair.com" in api_url:
# Blockchair response structure
txs = data.get('data', {}).get(address, {}).get('transactions', [])
transactions.extend(txs)
if not txs: # Stop if no more transactions are returned
break
else:
print(f"Unsupported API format for {api_url}")
break
# Increment offset for the next page
offset += len(txs)
except Exception as e:
print(f"Error fetching data: {e}")
break
return transactions
def get_all_transactions(address):
"""Fetch transactions from multiple APIs, ensuring full coverage."""
for api in API_LIST:
try:
transactions = fetch_data_with_pagination(api, address)
if transactions:
return transactions
except Exception as e:
print(f"Error with API {api}: {e}")
continue # Try the next API
return []
def main():
address = input("Enter Bitcoin address: ")
transactions = get_all_transactions(address)
if transactions:
print(f"All transactions for address {address}:")
for tx in transactions:
tx_hash = tx.get('hash') or tx.get('transaction_hash')
block_height = tx.get('block_height') or tx.get('block_id', "Unconfirmed")
print(f"Transaction Hash: {tx_hash}, Block Height: {block_height}")
else:
print(f"No transactions found for address {address}. It might not have any transactions.")
if __name__ == "__main__":
main()