import marimo

__generated_with = "0.15.1"
app = marimo.App(width="medium")


@app.cell
def _():
    import marimo as mo
    return (mo,)


@app.cell
def _(mo):
    mo.md(r"""# DataSamples: Datos de apis gratuitas""")
    return


@app.cell
def _(mo):
    mo.md(r"""Datos del tiempo De Openmeteo""")
    return


@app.cell
def _():
    import requests
    import pandas as pd

    # Ejemplo: clima horario en Madrid (lat=40.4168, lon=-3.7038)
    url = "https://api.open-meteo.com/v1/forecast"
    params = {
        "latitude": 40.4168,
        "longitude": -3.7038,
        "hourly": "temperature_2m,relative_humidity_2m,precipitation"
    }

    response = requests.get(url, params=params)
    data = response.json()

    # Convertir a DataFrame
    _df = pd.DataFrame({
        "time": data["hourly"]["time"],
        "temperature_C": data["hourly"]["temperature_2m"],
        "humidity_%": data["hourly"]["relative_humidity_2m"],
        "precipitation_mm": data["hourly"]["precipitation"]
    })

    _df
    return pd, requests


@app.cell
def _(mo):
    mo.md(r"""Datos de COVID-19""")
    return


@app.cell
def _(pd, requests):
    # import requests
    # import pandas as pd

    # Ejemplo: datos históricos de COVID en España
    _url = "https://disease.sh/v3/covid-19/historical/Spain?lastdays=30"
    _response = requests.get(_url)
    _data = _response.json()

    cases = _data["timeline"]["cases"]
    deaths = _data["timeline"]["deaths"]
    recovered = _data["timeline"]["recovered"]

    # Convertir a DataFrame
    _df = pd.DataFrame({
        "date": list(cases.keys()),
        "cases": list(cases.values()),
        "deaths": list(deaths.values()),
        "recovered": list(recovered.values())
    })

    _df
    return


@app.cell
def _(mo):
    mo.md(r"""Datos financieros de Yahoo Finance""")
    return


@app.cell
def _():
    import yfinance as yf
    # import pandas as pd

    # Ejemplo: descargar datos de Apple
    ticker = yf.Ticker("AAPL")
    hist = ticker.history(period="1mo")

    _df = hist.reset_index()[["Date", "Open", "High", "Low", "Close", "Volume"]]
    _df
    return


if __name__ == "__main__":
    app.run()
