nevillepark / help-why-cant-i-hold-all-these-mutuals

find all your mutuals and throw them on a list so you can all be miserable together on twitter dot com
Other
1 stars 0 forks source link

RuntimeError: generator raised StopIteration #2

Closed nevillepark closed 2 years ago

nevillepark commented 2 years ago

Program fails with this:

Traceback (most recent call last):
  File "mutuals.py", line 53, in handleCursorLimit
    yield cursor.next()
  File "~/.local/lib/python3.8/site-packages/tweepy/cursor.py", line 286, in next
    self.current_page = next(self.page_iterator)
  File "~/.local/lib/python3.8/site-packages/tweepy/cursor.py", line 86, in __next__
    return self.next()
  File "~/.local/lib/python3.8/site-packages/tweepy/cursor.py", line 109, in next
    raise StopIteration
StopIteration

File "mutuals.py", line 162, in <module>
    mutuals(api)
  File "mutuals.py", line 81, in mutuals
    for fran in handleCursorLimit(ty.Cursor(api.get_friends).items()):
RuntimeError: generator raised StopIteration

Seems to be due to Python 3.7+ behaviour:

I do not know enough about programming to fix this, but maybe Future Me will, so I'm assigning it to them.

spikelynch commented 2 years ago

I think the StopIteration exception is being raised when there's no more items in the cursor - you can trap it with a second except clause and just return:

    while True:
        try:
            yield cursor.next()
        except StopIteration:
            return
        except ty.TooManyRequests:
            print("you must have a lot of mutuals or something because we have"
                  " have officially just hit twitter's api limit. give it some"
                  " some time to reset, ~15 mins,  and the script will pickup")
            print("sleeping! {}".format(time.localtime()))
            # got fucked by jack's api once again, press F please
            time.sleep(60 * 15)
nevillepark commented 2 years ago

That works, thanks!

In fact it seems that the TooManyRequests exceptions are now totally unnecessary thanks to tweepy's wait_on_rate_limit option.