0

So I have this code I wrote to read from information from twitters REST API using tweepy and I'd like to collect a lot of information without exceeding the rate limit. This question is more of a conceptual one than tweepy-related. So far this is the only way i've thought of doing it:

for i, tweet in enumerate(tweepy.Cursor(api.user_timeline, screen_name = "@twitter").items(1400)):
    print(i, tweet.author.name)
    if i == 200:
        time.sleep(180)
    if i == 400:
        time.sleep(180)
    if i == 600:
        time.sleep(180)
    if i == 800:
        time.sleep(180)
    if i == 1000:
        time.sleep(180)
    if i == 1200:
        time.sleep(180)
    if i == 1400:
        sys.exit()

But rather than writing a bunch of if statements, is there a more pythonic way this can be written? Or if it isn't broken don't fix it?

e1v1s
  • 365
  • 6
  • 18

1 Answers1

1
for i, tweet in enumerate(...):
    if i % 200 == 0 and i > 0:
        time.sleep(180)

This will fire every multiple of 200 except for 0.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • So the % is helping to say "whenever idx is divided by 200 and the remainder is 0" and "idx has to be > 0" then sleep? And why does it grab 201 items and not 200 for each iteration? – e1v1s Jan 05 '17 at 02:07