import os
import pandas as pd
from datetime import datetime, timedelta
import requests
import time
import argparse

# Funkcja pobierająca dane świecowe (candlesticks) z Binance API
def fetch_historical_klines(symbol, interval, start_time, end_time):
    url = "https://api.binance.com/api/v3/klines"
    params = {
        "symbol": symbol,
        "interval": interval,
        "startTime": int(start_time.timestamp() * 1000),
        "endTime": int(end_time.timestamp() * 1000),
        "limit": 1000
    }
    response = requests.get(url, params=params)
    if response.status_code == 200:
        data = response.json()
        df = pd.DataFrame(data, columns=[
            "Open time", "Open", "High", "Low", "Close", "Volume",
            "Close time", "Quote asset volume", "Number of trades",
            "Taker buy base asset volume", "Taker buy quote asset volume", "Ignore"
        ])
        df["Open time"] = pd.to_datetime(df["Open time"], unit="ms")
        df["Close time"] = pd.to_datetime(df["Close time"], unit="ms")
        return df
    else:
        print(f"Błąd: {response.status_code} dla {symbol}")
        return pd.DataFrame()

# Funkcja iteracyjnego pobierania danych dla miesiąca
def fetch_monthly_data(symbol, interval, year, month):
    start_date = datetime(year, month, 1)
    if month == 12:
        end_date = datetime(year + 1, 1, 1)
    else:
        end_date = datetime(year, month + 1, 1)

    all_data = []
    current_start = start_date
    delta = timedelta(days=10)

    while current_start < end_date:
        current_end = min(current_start + delta, end_date)
        print(f"Pobieranie danych: {symbol} {current_start} - {current_end}")
        df = fetch_historical_klines(symbol, interval, current_start, current_end)
        if not df.empty:
            all_data.append(df)
        current_start = current_end
        time.sleep(0.5)

    if all_data:
        return pd.concat(all_data, ignore_index=True)
    else:
        return pd.DataFrame()

# Funkcja zapisująca dane do plików CSV
def save_to_csv(data, symbol, year, month):
    output_dir = f"../data/{symbol}"
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    file_path = f"{output_dir}/{symbol}_{year}_{month:02d}.csv"
    data.to_csv(file_path, index=False)
    print(f"Zapisano dane do pliku: {file_path}")

# Główna funkcja iteracyjna
def main():
    parser = argparse.ArgumentParser(description="Pobieranie danych świecowych z Binance API.")
    parser.add_argument("--coins", nargs="+", required=True, help="Lista symboli monet np. BTCUSDT ETHUSDT")
    parser.add_argument("--start_year", type=int, required=True, help="Rok początkowy (np. 2020)")
    parser.add_argument("--end_year", type=int, required=True, help="Rok końcowy (np. 2024)")
    parser.add_argument("--interval", type=str, default="1h", help="Interwał czasowy np. 1h, 1d (domyślnie: 1h)")

    args = parser.parse_args()

    coins = args.coins
    start_year = args.start_year
    end_year = args.end_year
    interval = args.interval

    total_tasks = len(coins) * (end_year - start_year + 1) * 12
    completed_tasks = 0

    for coin in coins:
        for year in range(start_year, end_year + 1):
            for month in range(1, 13):
                print(f"Pobieranie: {coin}, rok: {year}, miesiąc: {month}")
                data = fetch_monthly_data(coin, interval, year, month)
                if not data.empty:
                    save_to_csv(data, coin, year, month)
                else:
                    print(f"Brak danych dla: {coin}, rok: {year}, miesiąc: {month}")

                completed_tasks += 1
                print(f"Postęp: {completed_tasks}/{total_tasks} ukończono")

if __name__ == "__main__":
    main()

