Skip to content

Commit e43c528

Browse files
committed
Add web program to fetch top 10 realtime billioners using forbes API.
1 parent ed12703 commit e43c528

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ scikit-fuzzy
1414
sklearn
1515
statsmodels
1616
sympy
17+
tabulate
1718
tensorflow
1819
texttable
1920
tweepy

web_programming/get_top_billioners.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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

Comments
 (0)