Search code examples
python-3.xtwittertweepy

tweepy It won't follow some of the tweets


seems like for some of the tweets with the keyword 'follow' it will follow and for some of them it wont... other than that it works fine(I didn't notice something else) can someone pinpoint where is the problem?

class Listener():

def search(self, twts):
    global numoftwts
    for i in twts:
        names = ['follow', 'following']
        txt = i.text.lower()
        if not any(k in txt for k in keywords) or any(k in txt for k in bannedwords):
            continue
        if not self.is_user_bot_hunter(str(i.author.screen_name)):
            if not i.retweeted:
                try:
                    print("Trying to retweet status_id:{}".format(i.id))
                    res = api.retweet(i.id)
                    if res.retweeted:
                        api.create_favorite(i.id)
                        print('retweeted', numoftwts, 'times', '-',
                              str(datetime.datetime.fromtimestamp(time.time()).strftime('%d-%m-%Y %H:%M:%S')))

                        print(i.text)
                        print('\n')
                    else:
                        print("retweet failed")

                    if any(c in txt for c in names):
                        # print("Trying to follow something")
                        # if hasattr(i, 'retweeted_status'):
                        #     print("trying to fetch user_id")
                        user_id = i.retweeted_status.user.id_str
                        res = api.create_friendship(user_id)
                        res = api.get_user(user_id)
                        if res.following:
                            print("Successfully followed :{}".format(user_id))
                            print('\n')

                except Exception as e:
                    print("Exception:".format(str(e)))
                    continue
        sleep(600)

     def run(self):
            for eachkey in keywords:
            tweets = api.search(q=eachkey, result_type='mixed', lang='en')
            self.search(tweets)

if __name__ == '__main__':
    while True:
        r = Listener()
        r.run()

where did I go wrong?

AttributeError: 'Status' object has no attribute 'retweeted_status'
> c:\users\x\desktop\twitterbot\twtbotcopy.py(64)search()
-> user_id = i.retweeted_status.user.id_str
(Pdb) n
> c:\users\x\desktop\twitterbot\twtbotcopy.py(70)search()
-> except Exception as e:
(Pdb) n

Solution

  • If your getting any error where you are unable to get tweets from a particular user then use:

    try:
        specific_tweets = tweepy.Cursor(api.search, tweet_mode='extended', q= <some query>, lang='en').items(500)
    except tweepy.error.TweepError:
        pass
    

    And if you want to access the retweeted attribute of a tweet then do this:

    if hasattr(tweet, 'retweeted_status'):
                extracted_author = tweet.retweeted_status.user.screen_name
            else: extracted_author = tweet.user.screen_name
    

    basically check whether hasattr(tweet, 'retweeted_status') of a tweet is true or not. It checks whether the tweet has the attribute named "retweeted_status"