From da3fcc1ad3dcf0273484fab653af798fbeb553cf Mon Sep 17 00:00:00 2001 From: dgw Date: Wed, 18 Sep 2024 10:13:27 -0500 Subject: [PATCH] Trust the `sign_in()` method Documentation implies that you have to run `app.connect()` to reuse an existing saved session, but `app.sign_in()` calls the `connect()` method internally if the session exists and matches the passed-in `username`. This behavior should already exist in the plugin's current minimum `tweety-ns` version of 1.1.4, based on commit dates. Upstream doesn't maintain tags or other version metadata within the source repository, though, so this is just me taking an educated guess. --- sopel_twitter/__init__.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/sopel_twitter/__init__.py b/sopel_twitter/__init__.py index fc63aa1..7ec27b1 100644 --- a/sopel_twitter/__init__.py +++ b/sopel_twitter/__init__.py @@ -115,7 +115,7 @@ def format_tweet(tweet): :param tweet: the tweet object, as decoded JSON :return: the formatted tweet - :rtype: tuple + :rtype: str """ text = NEWLINE_RUN_REGEX.sub("\n", tweet.text) text = text.replace("\n", " \u23CE ") # Unicode symbol to indicate line-break @@ -216,12 +216,10 @@ def user_command(bot, trigger): def output_status(bot, trigger, id_): try: app = Twitter(_get_tweety_session_name(bot)) - # try to use saved session - app.connect() - - if not app.is_user_authorized: - # existing session not present - app.sign_in(bot.settings.twitter.username, bot.settings.twitter.password) + # sign_in() checks for an existing session, and tries to use it if + # present. It will sign in with username & password if the session + # doesn't exist or is expired/invalid. + app.sign_in(bot.settings.twitter.username, bot.settings.twitter.password) tweet = app.tweet_detail(id_) except tweety_errors.DeniedLogin: @@ -289,12 +287,10 @@ def output_status(bot, trigger, id_): def output_user(bot, trigger, sn): try: app = Twitter(_get_tweety_session_name(bot)) - # try to use saved session - app.connect() - - if not app.is_user_authorized: - # existing session not present - app.sign_in(bot.settings.twitter.username, bot.settings.twitter.password) + # sign_in() checks for an existing session, and tries to use it if + # present. It will sign in with username & password if the session + # doesn't exist or is expired/invalid. + app.sign_in(bot.settings.twitter.username, bot.settings.twitter.password) user = app.get_user_info(sn) except tweety_errors.DeniedLogin: