-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
cclauss
merged 9 commits into
TheAlgorithms:master
from
girisagar46:top-10-realtime-billionaires
Oct 23, 2022
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e43c528
Add web program to fetch top 10 realtime billioners using forbes API.
girisagar46 e9baf8f
Provide return type to function.
girisagar46 c85adac
Use rich for tables and minor refactors.
girisagar46 b89040d
Fix tiny typo.
girisagar46 a4b2df8
Add the top {LIMIT} in rich table title.
girisagar46 ac5e839
Update web_programming/get_top_billioners.py
girisagar46 cc8a460
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e05ef42
Change the API path.
girisagar46 05c8a6c
Update get_top_billioners.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ pillow | |
projectq | ||
qiskit | ||
requests | ||
rich | ||
scikit-fuzzy | ||
sklearn | ||
statsmodels | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from datetime import datetime | ||
|
||
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}" | ||
) | ||
Comment on lines
+15
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
|
||
def calculate_age(unix_date: int) -> str: | ||
"""Calculates age from given unix time format. | ||
|
||
Returns: | ||
Age as string | ||
|
||
>>> calculate_age(-657244800000) | ||
'73' | ||
>>> calculate_age(46915200000) | ||
'51' | ||
""" | ||
birthdate = datetime.fromtimestamp(unix_date / 1000).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]]: | ||
"""Get top 10 realtime billionaires using forbes API. | ||
|
||
Returns: | ||
List of top 10 realtime billionaires data. | ||
""" | ||
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: | ||
"""Display Forbes real time billionaires in a rich table. | ||
|
||
Args: | ||
forbes_billionaires (list): Forbes top 10 real time billionaires | ||
""" | ||
|
||
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()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isort sorts your imports so you don't have to. ;-)