File size: 1,670 Bytes
c8943df | 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 | 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)
# Instancia global de la cadena eterna
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"
|