import yfinance as yf import pandas as pd import time from datasets import Dataset import os # Hugging Face Credentials HF_USERNAME = "KavinduHansaka" HF_DATASET_REPO = f"{HF_USERNAME}/btc-minute-data" HF_TOKEN = os.getenv("Token") # Function to Fetch BTC Prices from Yahoo Finance (Last 24h, 1-min interval) def fetch_btc_price(): try: btc = yf.Ticker("BTC-USD") df = btc.history(period="1d", interval="1m") # Fetch last 24 hours with 1-min intervals df = df.tail(240) # Keep only the last 4 hours (240 minutes) df.reset_index(inplace=True) df = df[["Datetime", "Close"]].rename(columns={"Datetime": "timestamp", "Close": "price"}) df["timestamp"] = df["timestamp"].astype(str) return df except Exception as e: print(f"❌ Failed to fetch BTC price: {e}") return None # Convert & Upload Dataset def create_hf_dataset(df): return Dataset.from_pandas(df) def upload_to_huggingface(dataset): dataset.push_to_hub(HF_DATASET_REPO, token=HF_TOKEN) print(f"✅ Dataset updated at: https://huggingface.co/datasets/{HF_USERNAME}/btc-minute-data") def update_dataset(): print("Fetching latest BTC minute data...") df = fetch_btc_price() if df is None or df.empty: print("❌ Failed to fetch BTC price.") return print("Uploading BTC dataset to Hugging Face...") dataset = create_hf_dataset(df) upload_to_huggingface(dataset) print("✅ BTC Dataset updated successfully!") if __name__ == "__main__": while True: update_dataset() time.sleep(3600) # Update dataset every hour