I am trying to run the code from https://github.com/CreepyD246/Twitter-Reply-Bot/blob/main/TwitterReplyBot.py on Replit.
#Importing modules/libraries
import tweepy
import time
import os
# Initialization code
auth = tweepy.OAuthHandler(os.getenv("key2"),os.getenv("skey2"))
auth.set_access_token(os.getenv("token2"),os.getenv("stoken2"))
api = tweepy.API(auth)
# Some important variables which will be used later
bot_id = int(api.me().id_str)
#Other things after
But I get this error:
Traceback (most recent call last):
File "main.py", line 12, in <module>
bot_id = int(api.me().id_str)
AttributeError: 'API' object has no attribute 'me'
I don't find anything in the Tweepy's documentation about API.me() so what's wrong?
The Tweepy version is 4.1.0.
Tweepy v4.0.0 removed API.me.
You can use API.verify_credentials instead.
Tweepy v4.0.0 removed API.me use API.get_user
That is the replacement for now
yes api.get_user is the replacement in tweepy v4.0.0
This worked for me api.get_user(screen_name="replaceWithYourTwitterUsername")
Related
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)
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.
I am trying to use the python twitter API but it can't find the Twitter class.
https://pypi.python.org/pypi/twitter
For instance I do the following without any errors:
from twitter import *
But when I do:
>>> t = Twitter(auth)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Twitter' is not defined
What am I missing? Is there a different python twitter library I need to install?
Try using the tweepy library, easy to use. Take a look at http://www.tweepy.org/
Here is a code example:
import tweepy
auth = tweepy.OAuthHandler('','')
auth.set_access_token('', '')
api = tweepy.API(auth)
public_tweets = api.followers_ids()
for tweet in public_tweets:
print len(public_tweets)
I am getting this error:
Traceback (most recent call last):
File "C:/Users/Shivam/Desktop/jsparse.py", line 13, in <module>
info = json.loads(str(data))
AttributeError: 'module' object has no attribute 'loads'
Any thoughts what wrong I am doing here?
This is my code:
import json
import urllib
url = ''
uh = urllib.urlopen(url)
data = uh.read()
info = json.loads(str(data))
The problem is that you're using Python 2.5.x, which doesn't have the json module. If possible, I recommend upgrading to Python 2.7.x, as 2.5.x is badly outdated.
If you need to stick with Python 2.5.x, you'll have to use the simplejson module (see here). This code will work for 2.5.x as well as newer Python versions:
try:
import json
except ImportError:
import simplejson as json
Or if you're only using Python 2.5, just do:
import simplejson as json
I am trying to develop a gtk3 desktop application using python to perform the basic twitter functions like accessing the home timeline of a user, making tweets etc.
I am using python-twitter library, but am unable to find the API call for the purpose. I checked and saw there were a few patches , but they dont seem to work. the rest of the functions I am able to accomplish using the library.
I need help!!!
[edit]
this is the error i am facing when i tried using a fork of the python-twitter library, as given on: http://github.com/jaytaylor/python-twitter-api
Error:
>>api.getUserTimeline('gaurav_sood91')
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "twitter.py", line 2646, in getUserTimeline
self._checkForTwitterError(data)
File "twitter.py", line 3861, in _checkForTwitterError
if data.has_key('next_cursor'):
AttributeError: 'list' object has no attribute 'has_key'
Using the python-twitter module from code.google.com, documentation here.
Accessing user timelines:
import twitter
api = twitter.Api()
statuses = api.GetUserTimeline('#gaurav_sood91')
print [s.text for s in statuses]
Posting tweets:
import twitter
api = twitter.Api(consumer_key='consumer_key',
consumer_secret='consumer_secret',
access_token_key='access_token',
access_token_secret='access_token_secret')
status = api.PostUpdate('This is my update text.')
Edit for applying GetHomeTimeline patch:
Disclaimer: I'm on Windows, so you may need to change these steps a bit.
Download python-twitter
Extract to folder
Download 0002-Support-for-home-timeline.patch file from issue 152
Copy/move patch file to root of extracted python-twitter directory (there should be a file named twitter.py in this dir)
Run command: patch twitter.py 0002-Support-for-home-timeline.patch, you should get a message that patch succeeded
In same directory, run command: python setup.py install
Run interactive python shell: import twitter, dir(twitter.Api)
You should see the GetHomeTimeline method listed.
Update for GetHomeTimeline:
Found patch in issue 152 that works well using OAuth and JSON parse method that is now part of Status class. Sample code:
import twitter
api = twitter.Api(consumer_key='consumer_key',
consumer_secret='consumer_secret',
access_token_key='access_token',
access_token_secret='access_token_secret')
statuses = api.GetHomeTimeline()
print [s.text for s in statuses]
I'm trying to use the python-twitter module, but still having problems initiating the twitter.Api(). I've checked and rechecked that no other file named twitter.py or twitter.pyc is on my system. On a clean install i first try to
>>> import twitter
and correctly get a response of 'module unknown'
I do a easy_install twitter, successfull.
Then do
>>> import twitter
>>> testapi = twitter.Api()
The response is
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
testapi = twitter.Api()
AttributeError: 'module' object has no attribute 'Api'
>>>
I'm exhausted trying to locate the problem here, please help.
It appears that you are trying to follow the documentation for one Python Twitter module when you are in fact using another Python Twitter module.
The Api() method call you mention is part of this Python Twitter module. However, when you use easy_install twitter, you actually get this other Python Twitter module.