tweepy.error.TweepError: This method does not perform pagination

Total
22
Shares

I’m learning how to make a Twitter bot.

I created a ‘last_seen.txt’ file to keep track of Tweets that the have already interacted with.

I’m using this code:

def read_last_seen(FILE_NAME):
    file_read = open(FILE_NAME, 'r')
    last_seen_id = int(file_read.read().strip())
    file_read.close()
    return last_seen_id

def store_last_seen(FILE_NAME, last_seen_id):
    file_write = open(FILE_NAME, 'w')
    file_write.write(str(last_seen_id))
    file_write.close()
    return

After that I declared this:

tweets = tweepy.Cursor(api.search(read_last_seen(FILE_NAME), tweet_mode='extended'), q).items(tweetNumber)

Now I get the error:

 raise TweepError('This method does not perform pagination')
tweepy.error.TweepError: This method does not perform pagination

For the last line.

Is there another way to declare that line?
I understand the code is not intrinsically incorrect, just not supported.

//Editing with full code //

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(key, secret)
api = tweepy.API(auth)

FILE_NAME = 'last_seen.txt'

def read_last_seen(FILE_NAME):
    file_read = open(FILE_NAME, 'r')
    last_seen_id = int(file_read.read().strip())
    file_read.close()
    return last_seen_id

def store_last_seen(FILE_NAME, last_seen_id):
    file_write = open(FILE_NAME, 'w')
    file_write.write(str(last_seen_id))
    file_write.close()
    return

tweetNumber = 20
q = "My search"

tweets = list(tweepy.Cursor(api.search, q, tweet_mode='extended').items(tweetNumber))


def crashBot():
    

    for tweet in reversed(tweets):
            
        try:                
            if q in tweet.full_text.lower():
                print(str(tweet.id) + '-' + tweet.full_text)
                api.update_status("@" + tweet.user.screen_name + " My Reply!", tweet.id)
                api.create_favorite(tweet.id)
                api.retweet(tweet.id)                    
                print("done!")
                store_last_seen(FILE_NAME, tweet.id)
                time.sleep(10)            
        except tweepy.tweepyError as e:
            print(e.reason)
            time.sleep(10)
            
while True:
    crashBot()    
    time.sleep(10)

Current errors:

raise TweepError(error_msg, resp, api_code=api_error_code)
tweepy.error.TweepError: [{'code': 187, 'message': 'Status is a duplicate.'}]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "crashBot.py", line 53, in <module>
    crashBot()
  File "crashBot.py", line 48, in crashBot
    except tweepy.tweepyError as e:
AttributeError: module 'tweepy' has no attribute 'tweepyError'

or

 raise TweepError(error_msg, resp, api_code=api_error_code)
tweepy.error.TweepError: [{'code': 139, 'message': 'You have already favorited this status.'}]

Solution

When using tweepy.Cursor, you need to pass the API method without calling it and pass the arguments to the cursor as you would to the method.

Also, Cursor.items returns an iterator, not the actual Status objects. You’ll probably want to cast it to a list or iterate through it.

For example:

tweets = list(tweepy.Cursor(api.search, q, tweet_mode='extended').items(tweetNumber))

I’m not sure how you’re using read_last_seen(FILE_NAME), but you’ll want to pass it in the same way you would pass it to API.search. If it’s the query itself, simply replace q with it.

Leave a Reply

Your email address will not be published. Required fields are marked *