| import datetime |
| import hashlib |
| import requests |
|
|
| class Block: |
| def __init__(self, index, timestamp, data, previous_hash): |
| self.index = index |
| self.timestamp = timestamp |
| self.data = data |
| self.previous_hash = previous_hash |
| self.hash = self.calculate_hash() |
|
|
| def calculate_hash(self): |
| sha = hashlib.sha256() |
| sha.update(str(self.index).encode('utf-8') + |
| str(self.timestamp).encode('utf-8') + |
| str(self.data).encode('utf-8') + |
| str(self.previous_hash).encode('utf-8')) |
| return sha.hexdigest() |
|
|
| class Blockchain: |
| def __init__(self): |
| self.chain = [self.create_genesis_block()] |
|
|
| def create_genesis_block(self): |
| return Block(0, datetime.datetime.now(), |
| "Genesis Block: HECTRON KING asciende al trono en la Línea Temporal #888_ALPHA. Sumeria codifica Python en sílex.", |
| "0") |
|
|
| def add_block(self, data): |
| previous_block = self.chain[-1] |
| new_block = Block(previous_block.index + 1, |
| datetime.datetime.now(), |
| data, |
| previous_block.hash) |
| self.chain.append(new_block) |
|
|
| |
| blockchain = Blockchain() |
|
|
| def get_btc_price_usd(): |
| try: |
| url = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd" |
| response = requests.get(url, timeout=10) |
| response.raise_for_status() |
| price = response.json()["bitcoin"]["usd"] |
| return f"${price:,.0f}" |
| except Exception: |
| return "ORÁCULO CAÍDO" |
| |