How to retrieve user details from mention tweets? - python

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)

Related

How to stream tweets from private accounts using Tweepy

I have been having problems with the MyStreamListener from tweepy, in where I am unable to fetch tweets from private accounts I follow. I know these tweets can be accessed with the API, because I can fetch them with api.user_timeline. Is there any way to be able to stream my whole timeline? Could I just continuously fetch my timeline in a while loop? Thank you for any help!
import tweepy
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
webhooklink = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
print('starting to monitor!')
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
print(status.text)
if __name__ == '__main__':
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener)
myStream.filter(
follow='25073877', is_async=True)

how can I stream a specific user's tweets and return them as a variable

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'])

twitter timeline streaming with tweepy

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)

Python - how to get tweets in real time by hashtag

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?

How can I consume tweets from Twitter's streaming api and store them in mongodb

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.

Categories

Resources