caution you may get a json decoding error this works for some of us but fails for others calculates age from given unix time format returns age as string from datetime import datetime utc yearssincecreate datetime nowtzutc year 2022 intcalculateage657244800000 yearssincecreate 73 intcalculateage46915200000 yearssincecreate 51 convert date from milliseconds to seconds handle timestamp before epoch get top 10 realtime billionaires using forbes api returns list of top 10 realtime billionaires data display forbes real time billionaires in a rich table args forbesbillionaires list forbes top 10 real time billionaires calculates age from given unix time format returns age as string from datetime import datetime utc years_since_create datetime now tz utc year 2022 int calculate_age 657244800000 years_since_create 73 int calculate_age 46915200000 years_since_create 51 convert date from milliseconds to seconds handle timestamp before epoch get top 10 realtime billionaires using forbes api returns list of top 10 realtime billionaires data display forbes real time billionaires in a rich table args forbes_billionaires list forbes top 10 real time billionaires
from datetime import UTC, datetime, timedelta

import requests
from rich import box
from rich import console as rich_console
from rich import table as rich_table

LIMIT = 10
TODAY = datetime.now()

API_URL = (
    "https://www.forbes.com/forbesapi/person/rtb/0/position/true.json"
    "?fields=personName,gender,source,countryOfCitizenship,birthDate,finalWorth"
    f"&limit={LIMIT}"
)


def calculate_age(unix_date: float) -> str:
    
    
    unix_date /= 1000

    if unix_date < 0:
        
        epoch = datetime.fromtimestamp(0, tz=UTC)
        seconds_since_epoch = (datetime.now(tz=UTC) - epoch).seconds
        birthdate = (
            epoch - timedelta(seconds=abs(unix_date) - seconds_since_epoch)
        ).date()
    else:
        birthdate = datetime.fromtimestamp(unix_date, tz=UTC).date()
    return str(
        TODAY.year
        - birthdate.year
        - ((TODAY.month, TODAY.day) < (birthdate.month, birthdate.day))
    )


def get_forbes_real_time_billionaires() -> list[dict[str, str]]:
    
    response_json = requests.get(API_URL).json()
    return [
        {
            "Name": person["personName"],
            "Source": person["source"],
            "Country": person["countryOfCitizenship"],
            "Gender": person["gender"],
            "Worth ($)": f"{person['finalWorth'] / 1000:.1f} Billion",
            "Age": calculate_age(person["birthDate"]),
        }
        for person in response_json["personList"]["personsLists"]
    ]


def display_billionaires(forbes_billionaires: list[dict[str, str]]) -> None:
    

    table = rich_table.Table(
        title=f"Forbes Top {LIMIT} Real Time Billionaires at {TODAY:%Y-%m-%d %H:%M}",
        style="green",
        highlight=True,
        box=box.SQUARE,
    )
    for key in forbes_billionaires[0]:
        table.add_column(key)

    for billionaire in forbes_billionaires:
        table.add_row(*billionaire.values())

    rich_console.Console().print(table)


if __name__ == "__main__":
    display_billionaires(get_forbes_real_time_billionaires())