Skip to content
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

Rate Limit Countdown for the API Module #137

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

johnbumgarner
Copy link

@johnbumgarner johnbumgarner commented Apr 27, 2023

Created a static method to provide a visual countdown in minutes remaining when a rate limit threshold has been reached.

UPDATED this request on 04.29.2023 with new code.

Added the staticmethod rate_limit_countdown.
@johnbumgarner johnbumgarner changed the title ADDED 04.27.2023 Rate Limit Countdown Apr 27, 2023
Changed rate_limit_countdown timer to minutes instead of seconds.
Updated the Rate Limit Countdown function to display in minutes remaining and not seconds remaining.
Reworded display message for the function rate_limit_countdown
@MerleLiuKun
Copy link
Member

Cloud you change the print to logger?

@johnbumgarner
Copy link
Author

johnbumgarner commented May 5, 2023

I can change the print statement to console log messages.

I noted that you aren't really using logging in your package. I would have to add this to api.py

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s:%(name)s:%(levelname)s: %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S')

The output would look like this:

2023-05-05 09:09:16:pytwitter.api:INFO: Twitter Rate Limit threshold encountered.
2023-05-05 09:09:16:pytwitter.api:INFO: API connection sleeping for 15 minutes
2023-05-05 09:09:24:pytwitter.api:INFO: Remaining time until rate limit threshold resets is: 15 minutes

Is this what you want?

P.S. I updated the code linked to my pull request to send the information to the console using logging.

Updated countdown to use logging.
@MerleLiuKun
Copy link
Member

logging.basicConfig(level=logging.INFO,
format='%(asctime)s:%(name)s:%(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')

this library should not change the logging level or format, This need removed.

@johnbumgarner
Copy link
Author

johnbumgarner commented May 6, 2023

This pull request has gotten off track with trying to use logging as you requested.

According to Python 3.x official documentation for logging the best way to "Display console output for ordinary usage of a command line script or program" is using print()

These print statements would provide the user clear feedback about the rate limit threshold status and the time to reset this threshold. This is what the user would see:

Rate limit threshold encountered.
Connection sleeping for 15.1333 minutes
Remaining time until rate limit threshold resets is: 15 minutes
Remaining time until rate limit threshold resets is: 14 minutes
Remaining time until rate limit threshold resets is: 13 minutes

If we use logging then the user would get useless data, such the asctime, name of the module and the log type. This is what the user would see:

2023-05-07 10:11:09:pytwitter.api:INFO: Rate limit threshold encountered.
2023-05-07 10:11:09:pytwitter.api:INFO: Connection sleeping for 15.1333 minutes
2023-05-07 10:11:17:pytwitter.api:INFO: Remaining time until rate limit threshold resets is: 15 minutes
2023-05-07 10:12:17:pytwitter.api:INFO: Remaining time until rate limit threshold resets is: 14 minutes
2023-05-07 10:13:17:pytwitter.api:INFO: Remaining time until rate limit threshold resets is: 13 minutes

To accomplish the latter the logging code within pytwitter needs to be modified.

@MerleLiuKun
Copy link
Member

MerleLiuKun commented May 8, 2023

Yes, you are right.

But the users need to control whether to display logs outside the library.

If you use print, this will always show the log.

@johnbumgarner
Copy link
Author

johnbumgarner commented May 8, 2023

Can you please provide me an example on how users are controlling logging while using pytwitter?

FYI The reason that I created this pull request was to get some visual feedback when pytwitter started sleeping. I was sitting at my desk with a countdown application running to tell me when the app would restart.

I could potentially add the switch 'notify_on_rate_limitto the API module, which could be set toFalse, which would suppress the print statements. If the user set this variable to True` then the statements would be shown.

sleep_on_rate_limit: bool = False,
notify_on_rate_limit: bool = False,

:param sleep_on_rate_limit: If token reach the limit, will sleep.
:param notify_on_rate_limit: Provides visual feedback on remaining rate limit sleep period.

I'm updated the code linked to my pull request to use the param notify_on_rate_limit. I have also tested the code and it works.

updated this module with the parameter `notify_on_rate_limit`
@johnbumgarner johnbumgarner changed the title Rate Limit Countdown Rate Limit Countdown for the API Module May 8, 2023
Updated code to include the param `notify_on_rate_limit`
@MerleLiuKun
Copy link
Member

This parameter notify_on_rate_limit is not necessary.

the logger would be controled by the logging level.

For example

....
logger.debug('Twitter rate limit threshold encountered.')
sleeping_period = int("%02d" % (seconds)) / 60
logger.debug(f"Connection sleeping for {'%g' % sleeping_period} minutes")
....

In another file example.py

import logging
from pytwitter import Api

api = Api(xxxx)

# If you want show the log, use `setLevel` to print the `debug` and above level  log.
logging.getLogger().setLevel(logging.DEBUG)


# following your code.

Then,the log will displayed.

DEBUG:pytwitter.api:Twitter rate limit threshold encountered.
DEBUG:pytwitter.api:Connection sleeping for 1 minutes

@johnbumgarner
Copy link
Author

Personally, I would except a Python Package, such as yours to have logging enabled by default and written out to a YAML file. Also how would a user know what items that you are logging without looking at the code? And you are currently only logging twice in the entire code base. I know this because I looked at the code.

# request function in api.py
logger.debug( f"Rate limited requesting [{url}], sleeping for [{s_time}]")

The parameter notify_on_rate_limit is necessary, because it provides the user with a simple and reliable way to get feedback when a rate limit is encountered.

Api(bearer_token=token, sleep_on_rate_limit=True, notify_on_rate_limit=True)

Your logging way requires the user TO KNOW that a notification exists in the code and that they have to wrap extra code around the API to see this notification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants