From e43c528df72ce89298fcfcc1801ba9dddd9bcb40 Mon Sep 17 00:00:00 2001 From: girisagar46 Date: Sun, 23 Oct 2022 14:27:03 +0900 Subject: [PATCH 1/9] Add web program to fetch top 10 realtime billioners using forbes API. --- requirements.txt | 1 + web_programming/get_top_billioners.py | 41 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 web_programming/get_top_billioners.py diff --git a/requirements.txt b/requirements.txt index 25d2b4ef93d5..ece10d123941 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,6 +14,7 @@ scikit-fuzzy sklearn statsmodels sympy +tabulate tensorflow texttable tweepy diff --git a/web_programming/get_top_billioners.py b/web_programming/get_top_billioners.py new file mode 100644 index 000000000000..9919bb76c892 --- /dev/null +++ b/web_programming/get_top_billioners.py @@ -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(): + """Get top 10 realtime billionaires using forbes API + Returns: + Top 10 realtime billionaires date in tabulated string. + """ + 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)), + } + ) + 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()) From e9baf8fdff75c094b056d73507271ea094ab86d4 Mon Sep 17 00:00:00 2001 From: girisagar46 Date: Sun, 23 Oct 2022 14:35:59 +0900 Subject: [PATCH 2/9] Provide return type to function. --- web_programming/get_top_billioners.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/get_top_billioners.py b/web_programming/get_top_billioners.py index 9919bb76c892..b86ed4ee9de4 100644 --- a/web_programming/get_top_billioners.py +++ b/web_programming/get_top_billioners.py @@ -10,7 +10,7 @@ ) -def real_time_billionaires(): +def real_time_billionaires() -> str: """Get top 10 realtime billionaires using forbes API Returns: Top 10 realtime billionaires date in tabulated string. From c85adac284316da8c1277403fd233a42dc33133f Mon Sep 17 00:00:00 2001 From: girisagar46 Date: Sun, 23 Oct 2022 21:06:40 +0900 Subject: [PATCH 3/9] Use rich for tables and minor refactors. --- requirements.txt | 2 +- web_programming/get_top_billioners.py | 93 +++++++++++++++++++-------- 2 files changed, 67 insertions(+), 28 deletions(-) diff --git a/requirements.txt b/requirements.txt index ece10d123941..9ffe784c945d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,11 +10,11 @@ pillow projectq qiskit requests +rich scikit-fuzzy sklearn statsmodels sympy -tabulate tensorflow texttable tweepy diff --git a/web_programming/get_top_billioners.py b/web_programming/get_top_billioners.py index b86ed4ee9de4..fa926c74819f 100644 --- a/web_programming/get_top_billioners.py +++ b/web_programming/get_top_billioners.py @@ -1,41 +1,80 @@ -from datetime import date, datetime +from datetime import datetime import requests -import tabulate +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/-estWorthPrev/true.json" - "?fields=personName,gender,source,countryOfCitizenship," - "birthDate,finalWorth&limit=10" + "?fields=personName,gender,source,countryOfCitizenship,birthDate,finalWorth" + f"&limit={LIMIT}" ) -def real_time_billionaires() -> str: - """Get top 10 realtime billionaires using forbes API +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: - Top 10 realtime billionaires date in tabulated string. + List of top 10 realtime billionaires data. """ 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)), - } - ) - header = tuple(final_response[0].keys()) - rows = [x.values() for x in final_response] - return tabulate.tabulate(rows, header) + 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 (dict): Forbes top 10 real time billionaires + """ + + table = rich_table.Table( + title=f"Forbes 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__": - print(real_time_billionaires()) + display_billionaires(get_forbes_real_time_billionaires()) From b89040d20e462ceee22f77d6fd4de53d0d4aed3f Mon Sep 17 00:00:00 2001 From: girisagar46 Date: Sun, 23 Oct 2022 21:11:55 +0900 Subject: [PATCH 4/9] Fix tiny typo. --- web_programming/get_top_billioners.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/get_top_billioners.py b/web_programming/get_top_billioners.py index fa926c74819f..750102cb2985 100644 --- a/web_programming/get_top_billioners.py +++ b/web_programming/get_top_billioners.py @@ -58,7 +58,7 @@ def display_billionaires(forbes_billionaires: list[dict[str, str]]) -> None: """Display Forbes real time billionaires in a rich table. Args: - forbes_billionaires (dict): Forbes top 10 real time billionaires + forbes_billionaires (list): Forbes top 10 real time billionaires """ table = rich_table.Table( From a4b2df80f81d1dbb6a3dbc7c2fb3875d26f899fa Mon Sep 17 00:00:00 2001 From: girisagar46 Date: Sun, 23 Oct 2022 21:15:41 +0900 Subject: [PATCH 5/9] Add the top {LIMIT} in rich table title. --- web_programming/get_top_billioners.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/get_top_billioners.py b/web_programming/get_top_billioners.py index 750102cb2985..d1608cedd54e 100644 --- a/web_programming/get_top_billioners.py +++ b/web_programming/get_top_billioners.py @@ -62,7 +62,7 @@ def display_billionaires(forbes_billionaires: list[dict[str, str]]) -> None: """ table = rich_table.Table( - title=f"Forbes Real Time Billionaires at {TODAY:%Y-%m-%d %H:%M}", + title=f"Forbes Top {LIMIT} Real Time Billionaires at {TODAY:%Y-%m-%d %H:%M}", style="green", highlight=True, box=box.SQUARE, From ac5e839053224f868c4d69416af0ad0e28df55b9 Mon Sep 17 00:00:00 2001 From: Sagar Giri Date: Sun, 23 Oct 2022 21:27:30 +0900 Subject: [PATCH 6/9] Update web_programming/get_top_billioners.py Co-authored-by: Caeden Perelli-Harris --- web_programming/get_top_billioners.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/web_programming/get_top_billioners.py b/web_programming/get_top_billioners.py index d1608cedd54e..b723e41d6d02 100644 --- a/web_programming/get_top_billioners.py +++ b/web_programming/get_top_billioners.py @@ -1,9 +1,7 @@ from datetime import datetime import requests -from rich import box -from rich import console as rich_console -from rich import table as rich_table +from rich import (box, console as rich_console, table as rich_table) LIMIT = 10 TODAY = datetime.now() From cc8a460630efcd184434945a3bfc44bcd2f0a264 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 23 Oct 2022 12:28:28 +0000 Subject: [PATCH 7/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/get_top_billioners.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/web_programming/get_top_billioners.py b/web_programming/get_top_billioners.py index b723e41d6d02..d1608cedd54e 100644 --- a/web_programming/get_top_billioners.py +++ b/web_programming/get_top_billioners.py @@ -1,7 +1,9 @@ from datetime import datetime import requests -from rich import (box, console as rich_console, table as rich_table) +from rich import box +from rich import console as rich_console +from rich import table as rich_table LIMIT = 10 TODAY = datetime.now() From e05ef42e303d5bca810320d05d1a5060924996a0 Mon Sep 17 00:00:00 2001 From: girisagar46 Date: Sun, 23 Oct 2022 22:09:57 +0900 Subject: [PATCH 8/9] Change the API path. --- web_programming/get_top_billioners.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/get_top_billioners.py b/web_programming/get_top_billioners.py index d1608cedd54e..acc6789d5ab6 100644 --- a/web_programming/get_top_billioners.py +++ b/web_programming/get_top_billioners.py @@ -9,7 +9,7 @@ TODAY = datetime.now() API_URL = ( - "https://www.forbes.com/forbesapi/person/rtb/0/-estWorthPrev/true.json" + "https://www.forbes.com/forbesapi/person/rtb/0/position/true.json" "?fields=personName,gender,source,countryOfCitizenship,birthDate,finalWorth" f"&limit={LIMIT}" ) From 05c8a6c92591aae2b21dfdb85bd50af496b321db Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 23 Oct 2022 16:35:07 +0200 Subject: [PATCH 9/9] Update get_top_billioners.py --- web_programming/get_top_billioners.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/web_programming/get_top_billioners.py b/web_programming/get_top_billioners.py index acc6789d5ab6..514ea1db9789 100644 --- a/web_programming/get_top_billioners.py +++ b/web_programming/get_top_billioners.py @@ -1,3 +1,7 @@ +""" +CAUTION: You may get a json.decoding error. This works for some of us but fails for others. +""" + from datetime import datetime import requests