|
| 1 | +from datetime import date, datetime |
| 2 | + |
| 3 | +import requests |
| 4 | +import tabulate |
| 5 | + |
| 6 | +API_URL = ( |
| 7 | + "https://www.forbes.com/forbesapi/person/rtb/0/-estWorthPrev/true.json" |
| 8 | + "?fields=personName,gender,source,countryOfCitizenship," |
| 9 | + "birthDate,finalWorth&limit=10" |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +def real_time_billionaires(): |
| 14 | + """Get top 10 realtime billionaires using forbes API |
| 15 | + Returns: |
| 16 | + Top 10 realtime billionaires date in tabulated string. |
| 17 | + """ |
| 18 | + 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) |
| 38 | + |
| 39 | + |
| 40 | +if __name__ == "__main__": |
| 41 | + print(real_time_billionaires()) |
0 commit comments