Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stream twitter mentions with tweepy?

I am currently using the following code, this gets the tweet and then passes it to a function that processes. This is not in realtime though.

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
mentions = api.mentions_timeline(count=1)
for mention in mentions:
   processText()

I have also looked in to streaming tweets but I have yet to find a way to stream mentions.

like image 968
Bossman759 Avatar asked Nov 24 '14 22:11

Bossman759


People also ask

How do I stream Twitter in Python?

Now you need an access token, so scroll down and click on "create my access token." After a few moments, refresh, and you should be able to see the access key and access token. Once you have that, you're going to need to get Tweepy, which is a Python module for streaming Twitter tweets.

What is Tweepy streaming?

Tweepy Stream. Tweepy is a Python library to access the Twitter API. You'll need to set up a twitter application at dev.twitter.com to attain a set of authentication keys to use with the API. Streaming with Tweepy comprises of three objects; Stream, StreamListener, OAuthHandler.

What is Twitter streaming API?

The Twitter API allows you to stream public Tweets from the platform in real-time so that you can display them and basic metrics about them.


1 Answers

You can use the Streaming API to filter on tweets containing @mentions. When you filter the stream using the track parameter, filter by the username you're interested in by including the username in your track parameter like so:

stream.filter(track=['twitterapi'])

This will return to you all tweets containing the string "twitterapi" in real-time, including @mentions (i.e. "@twitterapi"). See the Twitter Streaming API track parameter for how this works. Then, you can use the result JSON to only take the tweets that actually @mention the user you're interested in. The result JSON looks like this:

{"retweet_count":0,"text":"Man I like me some @twitterapi","entities":{"urls":[],"hashtags":[],"user_mentions":[{"indices":[19,30],"name":"Twitter API","id":6253282,"screen_name":"twitterapi","id_str":"6253282"}]},"retweeted":false,"in_reply_to_status_id_str":null,"place":null,"in_reply_to_user_id_str":null,"coordinates":null,"source":"web","in_reply_to_screen_name":null,"in_reply_to_user_id":null,"in_reply_to_status_id":null,"favorited":false,"contributors":null,"geo":null,"truncated":false,"created_at":"Wed Feb 29 19:42:02 +0000 2012","user":{"is_translator":false,"follow_request_sent":null,"statuses_count":142,"profile_background_color":"C0DEED","default_profile":false,"lang":"en","notifications":null,"profile_background_tile":true,"location":"","profile_sidebar_fill_color":"ffffff","followers_count":8,"profile_image_url":"http:\/\/a1.twimg.com\/profile_images\/1540298033\/phatkicks_normal.jpg","contributors_enabled":false,"profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/365782739\/doof.jpg","description":"I am just a testing account, following me probably won't gain you very much","following":null,"profile_sidebar_border_color":"C0DEED","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1540298033\/phatkicks_normal.jpg","default_profile_image":false,"show_all_inline_media":false,"verified":false,"profile_use_background_image":true,"favourites_count":1,"friends_count":5,"profile_text_color":"333333","protected":false,"profile_background_image_url":"http:\/\/a3.twimg.com\/profile_background_images\/365782739\/doof.jpg","time_zone":"Pacific Time (US & Canada)","created_at":"Fri Sep 09 16:13:20 +0000 2011","name":"fakekurrik","geo_enabled":true,"profile_link_color":"0084B4","url":"http:\/\/blog.roomanna.com","id":370773112,"id_str":"370773112","listed_count":0,"utc_offset":-28800,"screen_name":"fakekurrik"},"id":174942523154894848,"id_str":"174942523154894848"}

That's a mess, but if you look in the "entities" object, you'll see "user_mentions":

"user_mentions":[{"indices":[19,30],"name":"Twitter API","id":6253282,"screen_name":"twitterapi","id_str":"6253282"}]

Then, under "screen_name", you see "twitterapi", showing you that the user @twitterapi was @mentioned.

like image 88
Luigi Avatar answered Sep 19 '22 23:09

Luigi