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

Conversation

girisagar46
Copy link
Contributor

@girisagar46 girisagar46 commented Oct 23, 2022

Describe your change:

Using the Forbes API to get the top ten real-time billionaires and present in a rich table.

Here's the sample output:
output

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@girisagar46 girisagar46 requested a review from cclauss as a code owner October 23, 2022 05:32
@algorithms-keeper algorithms-keeper bot added enhancement This PR modified some existing files require type hints https://docs.python.org/3/library/typing.html labels Oct 23, 2022
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Oct 23, 2022
@algorithms-keeper algorithms-keeper bot removed the require type hints https://docs.python.org/3/library/typing.html label Oct 23, 2022
@cclauss
Copy link
Member

cclauss commented Oct 23, 2022

I like this one. Instead of Tabulate, would you be willing to do the table in Rich instead?
I want to use Rich to do some other color-in-the-terminal things and would rather add just one import instead of two.

def display_billionaires(forbes_billionaires: dict) -> None:
    """
    Display Forbes real time billionaires in a rich table
    Args:
        forbes_billionaires (dict): Forbes top 10 real time billionaires
    """
    from rich.console import Console  # Move these imports to the top of the file.
    from rich.table import Table
    table = Table(title="Forbes Real Time Billionaires", style="green")
    for key in forbes_billionaires[0]:
        table.add_column(key)
    for billionaire in forbes_billionaires:
        table.add_row(*billionaire.values())
    Console().print(table)


if __name__ == "__main__":
    display_billionaires(get_forbes_real_time_billionaires())

Comment on lines 30 to 32
"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

Comment on lines 15 to 16
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

@girisagar46
Copy link
Contributor Author

I like this one. Instead of Tabulate, would you be willing to do the table in Rich instead?

Thank you, @cclauss for reviewing it. Rich is awesome. I've modified it here: c85adac

@girisagar46 girisagar46 requested review from CaedenPH and removed request for cclauss October 23, 2022 12:18
Copy link
Contributor

@CaedenPH CaedenPH left a comment

Choose a reason for hiding this comment

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

Small change

Comment on lines +4 to +6
from rich import box
from rich import console as rich_console
from rich import table as rich_table
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
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)

Copy link
Member

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. ;-)

@CaedenPH
Copy link
Contributor

Oh, apparently the pre-commit automatically reverts it. No problem, @cclauss, can we get your review?

@cclauss
Copy link
Member

cclauss commented Oct 23, 2022

It does not work on my Mac. I get json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 1)

But if I go to the URL with my browser, it looks like all the right stuff.

@cclauss
Copy link
Member

cclauss commented Oct 23, 2022

Does it work on your machine @CaedenPH ?

@cclauss cclauss added hacktoberfest hacktoberfest-accepted Accepted to be counted towards Hacktoberfest labels Oct 23, 2022
@CaedenPH
Copy link
Contributor

CaedenPH commented Oct 23, 2022

Does it work on your machine @CaedenPH ?

I also get the JSONDecodeError. The url seems to return a valid json response though...

@cclauss
Copy link
Member

cclauss commented Oct 23, 2022

Often we can solve these issues with fake_useragent but that did not fix it for me.

@girisagar46
Copy link
Contributor Author

Often we can solve these issues with fake_useragent but that did not fix it for me.

Oh!! That's weird. It's working as expected on my mac.
Can you try by passing headers = {'Accept': 'application/json'} on requests.get to increase the likelihood of receiving a JSON response. @cclauss

@cclauss
Copy link
Member

cclauss commented Oct 23, 2022

headers = {'Accept': 'application/json'} did NOT fix it.

Do you have a Forbes login that you are using?

@girisagar46
Copy link
Contributor Author

Do you have a Forbes login that you are using?

No, I am not using it. Hitting the API in Incognito mode is giving me the correct response.
I've changed the API path to https://www.forbes.com/forbesapi/person/rtb/0/position/true.json in this commit e05ef42.
Can you try it again? @cclauss

@CaedenPH
Copy link
Contributor

Do you have a Forbes login that you are using?

No, I am not using it. Hitting the API in Incognito mode is giving me the correct response. I've changed the API path to https://www.forbes.com/forbesapi/person/rtb/0/position/true.json in this commit e05ef42. Can you try it again? @cclauss

I can hit the api successfully when I visit the url manually, but requests automates this process, which triggers forbes's anti-bot protection software, blocking us.

@cclauss
Copy link
Member

cclauss commented Oct 23, 2022

@dhruvmanila Any ideas on how to make this work? Should we use httpx instead of requests? Other ideas?

@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Oct 23, 2022
@cclauss cclauss merged commit 0f06a0b into TheAlgorithms:master Oct 23, 2022
Comment on lines +15 to +19
API_URL = (
"https://www.forbes.com/forbesapi/person/rtb/0/position/true.json"
"?fields=personName,gender,source,countryOfCitizenship,birthDate,finalWorth"
f"&limit={LIMIT}"
)
Copy link
Member

Choose a reason for hiding this comment

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

requests can automatically encode the URL parameters: https://requests.readthedocs.io/en/latest/user/quickstart/#passing-parameters-in-urls. Please use this instead.

@dhruvmanila
Copy link
Member

dhruvmanila commented Oct 23, 2022

Oh, already merged. Also, checks are failing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This PR modified some existing files hacktoberfest hacktoberfest-accepted Accepted to be counted towards Hacktoberfest
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants