Skip to content

Add web program to fetch top 10 real time billionaires using the forbes API. #7538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ scikit-fuzzy
sklearn
statsmodels
sympy
tabulate
tensorflow
texttable
tweepy
Expand Down
41 changes: 41 additions & 0 deletions web_programming/get_top_billioners.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from datetime import date, datetime

import requests
import tabulate

API_URL = (
"https://www.forbes.com/forbesapi/person/rtb/0/-estWorthPrev/true.json"
"?fields=personName,gender,source,countryOfCitizenship,"
"birthDate,finalWorth&limit=10"
)


def real_time_billionaires() -> str:
"""Get top 10 realtime billionaires using forbes API
Returns:
Top 10 realtime billionaires date in tabulated string.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Returns:
Top 10 realtime billionaires date in tabulated string.
Returns the Top 10 realtime billionaires date in tabulated string.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a comment syntax that some editors (PyCharm, I think) prefer so we should leave it as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this was automatically done by PyCharm. However, I've modified it a bit. c85adac

"""
response_json = requests.get(API_URL).json()
today = date.today()
final_response = []
for person in response_json["personList"]["personsLists"]:
birthdate = datetime.fromtimestamp(person["birthDate"] / 1000).date()
final_response.append(
{
"Name": person["personName"],
"Source": person["source"],
"Country": person["countryOfCitizenship"],
"Gender": person["gender"],
"Worth": f"{person['finalWorth'] / 1000:.1f} Billion",
"Age": today.year
- birthdate.year
- ((today.month, today.day) < (birthdate.month, birthdate.day)),
Copy link
Contributor

@CaedenPH CaedenPH Oct 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Age": today.year
- birthdate.year
- ((today.month, today.day) < (birthdate.month, birthdate.day)),
"Age": (
today.year - birthdate.year -
((today.month, today.day) < (birthdate.month, birthdate.day))
),

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that psf/black will reformat this back to the original solution (terms on separate lines).

For rich, we may need to convert age to a str.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. This has been modified here: c85adac

}
)
header = tuple(final_response[0].keys())
rows = [x.values() for x in final_response]
return tabulate.tabulate(rows, header)


if __name__ == "__main__":
print(real_time_billionaires())