i want to stream my own twitter timeline with python and tweepy and use code in below but it just print me some numbers and i dosent print my timeline twitts. Can you help me?
import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
consumer_key = 'abc'
consumer_secret = 'abc'
access_token = 'abc'
access_secret = 'abc'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
class listener(StreamListener):
def on_data(self, data):
print (data)
return True
def on_error(self, status):
print (status)
twitterStream = Stream(auth, listener())
twitterStream.userstream(encoding='utf8')
If you take a look at the documentation it says:
The on_data method of Tweepy’s StreamListener conveniently passes data from statuses to the on_status method.
The numbers you are seeing are the object IDs of the Tweets. You will need to do something like:
def on_status(self, status):
print(status.text)
Related
Im trying to stream specific user's tweets and return them as a variable for an event based trigger afterwards. I am using the following code, but it does not seem to return any tweets for me.
I am interested in their tweets only and not retweets and want to skip tweets not posted by the specified user.
Any idea where I am going wrong:
import tweepy
#API login for Twitter
consumer_key= CONSUMER_KEY
consumer_secret= CONSUMER_SECRET
access_key= ACCESS_TOKEN
access_secret= ACCESS_TOKEN_SECRET
class StreamListener(tweepy.StreamListener):
def on_status(self, status):
if status.user.id_str != '111111111':
return
tweet = status.text
return tweet
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
# initialize stream
streamListener = StreamListener()
stream = tweepy.Stream(auth=api.auth, listener=streamListener)
stream.filter(follow=['111111111'])
print(tweet)
here is the code to stream specific user's tweets:
import tweepy
import sys
#API login for Twitter
consumer_key= CONSUMER_KEY
consumer_secret= CONSUMER_SECRET
access_key= ACCESS_TOKEN
access_secret= ACCESS_TOKEN_SECRET
# StreamListener class inherits from tweepy.StreamListener and overrides on_status/on_error methods.
class StreamListener(tweepy.StreamListener):
def on_status(self, status):
if status.user.id_str != '11111111':
return
text = status.text
print(text)
def on_error(self, status_code):
print("Encountered streaming error (", status_code, ")")
sys.exit()
if __name__ == "__main__":
# complete authorization and initialize API endpoint
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
# initialize stream
streamListener = StreamListener()
stream = tweepy.Stream(auth=api.auth, listener=streamListener,tweet_mode='extended')
stream.filter(follow=['11111111'])
I am in the processing of creating a bot that reads all mentions in userstream Twitter timeline and reply accordingly, however I cannot seem to get the users details who posted the mention in order to reply back, all I receive is the mention status/text.
I am using Python and Tweepy API to achieve this:
import tweepy
from tweepy import Stream
consumer_key = '********'
consumer_secret = '********'
access_token = '***********'
access_secret = '*********'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
class MyStreamListener (tweepy.StreamListener):
def on_status(self, status):
print(status.text)
def on_error(self, status):
print(status)
def on_direct_message(self, status):
print(status)
twitterStream = Stream(auth, MyStreamListener())
userStream = twitterStream.userstream()
Any help would be greatly appreciated
It needed
print(status.author.screen_name)
I am looking for a pythonic way to get a stream of tweets by hashtag.
What I found so far is the tweepy package using this code:
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
class listener(StreamListener):
def on_data(self, data):
print(data)
return(True)
def on_error(self, status):
print "error"
print status
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["#iPhone"])
The problem is that I don't understand how can I get the consumer_key, consumer_secret, access_token, access_secret arguments.
I am doing the right thing? Is this the best way to collect tweets by hashtag using python? If so, how to get the that 4 arguments?
I am able to extract the mentioned details about a twitter user using Tweepy API.
I want to do it for a list of users. Can anyone help me to this?
import tweepy
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
CONSUMER_KEY = 'ABC'
CONSUMER_SECRET = 'ABC'
ACCESS_KEY = 'ABC'
ACCESS_SECRET = 'ABC'
class TweetListener(StreamListener):
# A listener handles tweets are the received from the stream.
#This is a basic listener that just prints received tweets to standard output
def on_data(self, data):
print data
return True
def on_error(self, status):
print status
auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
api = tweepy.API(auth)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
twitterStream = Stream(auth,TweetListener())
user = api.get_user('User Name')
print user.screen_name
print user.description
print user.followers_count
print user.statuses_count
print user.url
This code is ready to use anyone can use it with his/her own credentials for a single user profile.
Finally exercising and reading a lot I get the answer to my question.you can try this
import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
CONSUMER_KEY = 'ABC'
CONSUMER_SECRET = 'ABC'
ACCESS_KEY = 'ABC'
ACCESS_SECRET = 'ABC'
auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
api = tweepy.API(auth)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
class TweetListener(StreamListener):
# A listener handles tweets are the received from the stream.
#This is a basic listener that just prints received tweets to standard output
def on_data(self, data):
print data
return True
def on_error(self, status):
print status
#search
api = tweepy.API(auth)
twitterStream = Stream(auth,TweetListener())
test = api.lookup_users(user_ids=['17006157','59145948','157009365'])
for user in test:
print user.screen_name
print user.name
print user.description
print user.followers_count
print user.statuses_count
print user.url
This code is ready to use just put your valid keys in place of ABC & get the users profile.you need to get the IDs first.
Your code simply interacts with your twitter account; to find information on a specific user or group of users you should look them up using the api.lookup_users(user_ids=[]) query.
You'd do it like this:
#boring auth you already have
import tweepy
from tweepy import OAuthHandler
CONSUMER_KEY = 'ABC'
CONSUMER_SECRET = 'ABC'
ACCESS_KEY = 'ABC'
ACCESS_SECRET = 'ABC'
auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
api = tweepy.API(auth)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
#search
api = tweepy.API(auth)
test = api.lookup_users(user_ids=['1123728482,5539932'])
This gives you a list of two tweepy.models.User objects:
[<tweepy.models.User object at 0x103995090>, <tweepy.models.User object at 0x1039950d0>]
You can replace the list in user_ids with a list of up to 100 ids, twitter won't let you search any more than that at once, though. Once you have your list of User objects, you can access different properties (for a list, check out the tweepy doc for the User class, line 113).
I need to develop an app that lets me track tweets and save them in a mongodb for a research project (as you might gather, I am a noob, so please bear with me). I have found this piece of code that sends tweets streaming through my terminal window:
import sys
import tweepy
consumer_key=""
consumer_secret=""
access_key = ""
access_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
print status.text
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True # Don't kill the stream
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True # Don't kill the stream
sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
sapi.filter(track=['Gandolfini'])
Is there a way I can modify this piece of code so that instead of having tweets streaming over my screen, they are sent to my mongodb database?
Thanks
Here's an example:
import json
import pymongo
import tweepy
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
class CustomStreamListener(tweepy.StreamListener):
def __init__(self, api):
self.api = api
super(tweepy.StreamListener, self).__init__()
self.db = pymongo.MongoClient().test
def on_data(self, tweet):
self.db.tweets.insert(json.loads(tweet))
def on_error(self, status_code):
return True # Don't kill the stream
def on_timeout(self):
return True # Don't kill the stream
sapi = tweepy.streaming.Stream(auth, CustomStreamListener(api))
sapi.filter(track=['Gandolfini'])
This will write tweets to the mongodb test database, tweets collection.
Hope that helps.
I have developed a simple command line tool that does exactly this.
https://github.com/janezkranjc/twitter-tap
It allows using the streaming API or the search API.