summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Adam <_@yuv.al>2025-03-06 14:38:30 +0100
committerYuval Adam <_@yuv.al>2025-03-06 14:38:30 +0100
commit1b9c6ab131224351d1dbaabcd359b8ac55a7b164 (patch)
tree7201c0487abf2fb9afe0c0ca4f5ad06805459f03
parent6b82841a7042fcd08ec328a0b52cff4e1c9479c9 (diff)
It fetches the bitcoin price, it does this whenever its told
-rw-r--r--strangeloop/capabilities/__init__.py2
-rw-r--r--strangeloop/capabilities/fetch_current_bitcoin_price.py53
2 files changed, 55 insertions, 0 deletions
diff --git a/strangeloop/capabilities/__init__.py b/strangeloop/capabilities/__init__.py
index a1c9c5a..d042b8a 100644
--- a/strangeloop/capabilities/__init__.py
+++ b/strangeloop/capabilities/__init__.py
@@ -6,3 +6,5 @@ This package contains functions that have been dynamically added at runtime.
from strangeloop.capabilities.generate_secure_password import generate_secure_password
from strangeloop.capabilities.get_public_ip_address import get_public_ip_address
+
+from strangeloop.capabilities.fetch_current_bitcoin_price import fetch_current_bitcoin_price
diff --git a/strangeloop/capabilities/fetch_current_bitcoin_price.py b/strangeloop/capabilities/fetch_current_bitcoin_price.py
new file mode 100644
index 0000000..fd7a76e
--- /dev/null
+++ b/strangeloop/capabilities/fetch_current_bitcoin_price.py
@@ -0,0 +1,53 @@
+"""
+Dynamically generated capability: fetch_current_bitcoin_price
+"""
+
+import requests
+from typing import Dict, Union, Tuple
+from datetime import datetime
+
+def fetch_current_bitcoin_price() -> Tuple[Union[float, str], str]:
+ """
+ Fetches the current Bitcoin spot price in USD from a reliable cryptocurrency API.
+
+ Returns:
+ Tuple[Union[float, str], str]: A tuple containing:
+ - The current BTC/USD exchange rate as a float or string
+ - A timestamp string of when the data was retrieved
+
+ Raises:
+ ConnectionError: If there's an issue connecting to the API
+ ValueError: If the API response cannot be parsed
+ Exception: For any other unexpected errors
+ """
+ try:
+ # Using CoinGecko API as a reliable source for Bitcoin price data
+ url = "https://api.coingecko.com/api/v3/simple/price"
+ params = {
+ "ids": "bitcoin",
+ "vs_currencies": "usd",
+ "include_last_updated_at": True
+ }
+
+ response = requests.get(url, params=params, timeout=10)
+ response.raise_for_status() # Raise exception for 4XX/5XX responses
+
+ data = response.json()
+
+ # Extract price and validate response format
+ if "bitcoin" not in data or "usd" not in data["bitcoin"]:
+ raise ValueError("Unexpected API response format")
+
+ btc_price = float(data["bitcoin"]["usd"])
+
+ # Generate timestamp for when we received the data
+ current_timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S UTC")
+
+ return btc_price, current_timestamp
+
+ except requests.exceptions.RequestException as e:
+ raise ConnectionError(f"Failed to connect to Bitcoin price API: {str(e)}")
+ except ValueError as e:
+ raise ValueError(f"Failed to parse Bitcoin price data: {str(e)}")
+ except Exception as e:
+ raise Exception(f"Unexpected error fetching Bitcoin price: {str(e)}") \ No newline at end of file