|
1 |
| -from datetime import date, datetime |
| 1 | +from datetime import datetime |
2 | 2 |
|
3 | 3 | import requests
|
4 |
| -import tabulate |
| 4 | +from rich import box |
| 5 | +from rich import console as rich_console |
| 6 | +from rich import table as rich_table |
| 7 | + |
| 8 | +LIMIT = 10 |
| 9 | +TODAY = datetime.now() |
5 | 10 |
|
6 | 11 | API_URL = (
|
7 | 12 | "https://www.forbes.com/forbesapi/person/rtb/0/-estWorthPrev/true.json"
|
8 |
| - "?fields=personName,gender,source,countryOfCitizenship," |
9 |
| - "birthDate,finalWorth&limit=10" |
| 13 | + "?fields=personName,gender,source,countryOfCitizenship,birthDate,finalWorth" |
| 14 | + f"&limit={LIMIT}" |
10 | 15 | )
|
11 | 16 |
|
12 | 17 |
|
13 |
| -def real_time_billionaires() -> str: |
14 |
| - """Get top 10 realtime billionaires using forbes API |
| 18 | +def calculate_age(unix_date: int) -> str: |
| 19 | + """Calculates age from given unix time format. |
| 20 | +
|
| 21 | + Returns: |
| 22 | + Age as string |
| 23 | +
|
| 24 | + >>> calculate_age(-657244800000) |
| 25 | + '73' |
| 26 | + >>> calculate_age(46915200000) |
| 27 | + '51' |
| 28 | + """ |
| 29 | + birthdate = datetime.fromtimestamp(unix_date / 1000).date() |
| 30 | + return str( |
| 31 | + TODAY.year |
| 32 | + - birthdate.year |
| 33 | + - ((TODAY.month, TODAY.day) < (birthdate.month, birthdate.day)) |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def get_forbes_real_time_billionaires() -> list[dict[str, str]]: |
| 38 | + """Get top 10 realtime billionaires using forbes API. |
| 39 | +
|
15 | 40 | Returns:
|
16 |
| - Top 10 realtime billionaires date in tabulated string. |
| 41 | + List of top 10 realtime billionaires data. |
17 | 42 | """
|
18 | 43 | response_json = requests.get(API_URL).json()
|
19 |
| - today = date.today() |
20 |
| - final_response = [] |
21 |
| - for person in response_json["personList"]["personsLists"]: |
22 |
| - birthdate = datetime.fromtimestamp(person["birthDate"] / 1000).date() |
23 |
| - final_response.append( |
24 |
| - { |
25 |
| - "Name": person["personName"], |
26 |
| - "Source": person["source"], |
27 |
| - "Country": person["countryOfCitizenship"], |
28 |
| - "Gender": person["gender"], |
29 |
| - "Worth": f"{person['finalWorth'] / 1000:.1f} Billion", |
30 |
| - "Age": today.year |
31 |
| - - birthdate.year |
32 |
| - - ((today.month, today.day) < (birthdate.month, birthdate.day)), |
33 |
| - } |
34 |
| - ) |
35 |
| - header = tuple(final_response[0].keys()) |
36 |
| - rows = [x.values() for x in final_response] |
37 |
| - return tabulate.tabulate(rows, header) |
| 44 | + return [ |
| 45 | + { |
| 46 | + "Name": person["personName"], |
| 47 | + "Source": person["source"], |
| 48 | + "Country": person["countryOfCitizenship"], |
| 49 | + "Gender": person["gender"], |
| 50 | + "Worth ($)": f"{person['finalWorth'] / 1000:.1f} Billion", |
| 51 | + "Age": calculate_age(person["birthDate"]), |
| 52 | + } |
| 53 | + for person in response_json["personList"]["personsLists"] |
| 54 | + ] |
| 55 | + |
| 56 | + |
| 57 | +def display_billionaires(forbes_billionaires: list[dict[str, str]]) -> None: |
| 58 | + """Display Forbes real time billionaires in a rich table. |
| 59 | +
|
| 60 | + Args: |
| 61 | + forbes_billionaires (dict): Forbes top 10 real time billionaires |
| 62 | + """ |
| 63 | + |
| 64 | + table = rich_table.Table( |
| 65 | + title=f"Forbes Real Time Billionaires at {TODAY:%Y-%m-%d %H:%M}", |
| 66 | + style="green", |
| 67 | + highlight=True, |
| 68 | + box=box.SQUARE, |
| 69 | + ) |
| 70 | + for key in forbes_billionaires[0]: |
| 71 | + table.add_column(key) |
| 72 | + |
| 73 | + for billionaire in forbes_billionaires: |
| 74 | + table.add_row(*billionaire.values()) |
| 75 | + |
| 76 | + rich_console.Console().print(table) |
38 | 77 |
|
39 | 78 |
|
40 | 79 | if __name__ == "__main__":
|
41 |
| - print(real_time_billionaires()) |
| 80 | + display_billionaires(get_forbes_real_time_billionaires()) |
0 commit comments