Washington Gama
Washington Gama

Reputation: 29

My tweepy Twitter bot is not working, what am I doing wrong?

It returns several errors and most of them show: <framework not available>

I'm new at coding and I'm just trying making a bot for fun with the Twitter API but had a lot of errors and I don't know what to do.

The main error that appears is this one:

(from collections import namedtuple, Mapping ImportError: cannot import name 'Mapping' from 'collections')

can someone help me please?

import tweepy

# Authenticate to Twitter
auth = tweepy.OAuthHandler("", "")
auth.set_access_token("", "")
api = tweepy.API(auth)

# Pegar ID do bot
bot_id = int(api.me().id_str)


class MyStreamListener(tweepy.StreamListener):
    def on_status(self, tweet):
        print("tweet encontrado!")
        print(f"TWEET: {tweet.author.scream_name} - {tweet.text}")
        if tweet.author.id != bot_id and tweet.in_reply_to_status_id is None:
            if not tweet.retweeted:
                try:
                    print("Tentando retuite...")
                    api.retweet(tweet.id)
                    print("Retuitado com sucesso!")
                except Exception as err:
                    print(err)


stream_listener = MyStreamListener()
stream = tweepy.Stream(auth=api.auth, Listener=stream_listener)
stream.filter(track=['Squirrel', 'Squirrels', 'squirrel', 'squirrels', 'Esquilo', 'Esquilos'], languages=['en', 'pt'])

The erros that appeers: Traceback (most recent call last): File "C:\Users\wgama\PycharmProjects\botesquilo\botesquilo.py", line 1, in import tweepy File "C:\Users\wgama\AppData\Local\Programs\Python\Python310\lib\site-packages\tweepy_init_.py", line 12, in from tweepy.api import API File "C:\Users\wgama\AppData\Local\Programs\Python\Python310\lib\site-packages\tweepy\api.py", line 15, in import requests File "C:\Users\wgama\AppData\Local\Programs\Python\Python310\lib\site-packages\requests_init_.py", line 43, in import urllib3 File "C:\Users\wgama\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3_init_.py", line 8, in from .connectionpool import ( File "C:\Users\wgama\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 29, in from .connection import ( File "C:\Users\wgama\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connection.py", line 39, in from .util.ssl_ import ( File "C:\Users\wgama\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util_init_.py", line 3, in from .connection import is_connection_dropped File "C:\Users\wgama\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\connection.py", line 3, in from .wait import wait_for_read File "C:\Users\wgama\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\wait.py", line 1, in from .selectors import ( File "C:\Users\wgama\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\selectors.py", line 14, in from collections import namedtuple, Mapping ImportError: cannot import name 'Mapping' from 'collections' (C:\Users\wgama\AppData\Local\Programs\Python\Python310\lib\collections_init_.py)

Upvotes: 2

Views: 897

Answers (1)

Micka&#235;l Martinez
Micka&#235;l Martinez

Reputation: 1843

As investigated in the comments, the problem comes from the fact that you are using an old version of Tweepy which is incompatible with Python 3.10.

So an update to the lastest version of Tweepy (currently 4.8.0) should solve your problem.

But please note that the API.me() method have been removed since the 4.0.0 version of Tweepy, so you will have to replaced it by the API.verify_credentials() method.

Edit : I just read your code more carefully and I could have found other mistakes: there is not author field in a tweet object but a user field, and I guess you meant screen_name instead of scream_name (!). Finally, after your update to the lastest version of Tweepy, you will also have to replace tweepy.StreamListener by tweepy.Stream.

Upvotes: 1

Related Questions