From 3f58910c5328fe71335e93d946f10dc7ab2fc2d4 Mon Sep 17 00:00:00 2001 From: Andres Salomon Date: Fri, 3 Apr 2020 02:15:13 -0400 Subject: [PATCH] fix an infinite loop when the token dispenser is having issues When searching or downloading apks, gplaycli will happily keeping to get a token over and over from the token dispenser. If the token dispenser spits out tokens but google doesn't like them, it will create an infinite loop. This breaks out of that loop after 5 attempts, and also suggest to the user that they try using their own credentials. I'd like to point them to a manpage or README with instructions, but we're not there yet. --- gplaycli/gplaycli.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/gplaycli/gplaycli.py b/gplaycli/gplaycli.py index 718906c..6c3abdc 100755 --- a/gplaycli/gplaycli.py +++ b/gplaycli/gplaycli.py @@ -93,6 +93,7 @@ def __init__(self, args=None, config_file=None): self.api = None self.token_passed = False + self.token_attempts = 0 config = configparser.ConfigParser() @@ -343,7 +344,7 @@ def search(self, search_string, free_only=True, include_headers=True): """ try: results = self.api.search(search_string) - except IndexError: + except (IndexError, RequestError): results = [] if not results: logger.info("No result") @@ -424,11 +425,17 @@ def connect_token(self): else: logger.info("Using auto retrieved token to connect to API") try: + self.token_attempts += 1 self.api.login(authSubToken=self.token, gsfId=int(self.gsfid, 16)) except (ValueError, IndexError, LoginError, DecodeError, SystemError, RequestError): - logger.info("Token has expired or is invalid. Retrieving a new one...") - self.retrieve_token(force_new=True) - self.connect() + if self.token_attempts < 5: + logger.info("Token has expired or is invalid. Retrieving a new one...") + self.retrieve_token(force_new=True) + self.connect() + else: + logger.error("Multiple attempts to fetch anonymous token failed.") + logger.error("Try setting 'token=False' and adding Google credentials to gplaycli.conf") + return True, None def connect_credentials(self):