Dictionary key error when parsing tweets with twitter api - python

from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import pw
import json
access_token = pw.access_token
access_token_secret = pw.access_token_secret
consumer_key = pw.consumer_key
consumer_secret = pw.consumer_secret
class StdOutListener(StreamListener):
def on_data(self, data):
dicto = json.loads(data)
print(dicto['user'])
return True
def on_error(self, status):
print(status)
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, StdOutListener())
stream.filter(track="music")
So I just followed a simple tutorial to process tweets. I'm just trying to parse the tweets, however when I use print(dicto['user']) it prints some and then throws a key error. The weird thing is that it's always after the 48th one. If I simply print the whole dictionary then it happily prints away. Right after the 48th piece of information is says traceback (most recent call last).
I'm a little confused what's going on here.

Related

Stream with Tweepy : 'str' object has no attribute 'entities'

I tried to get any tweet that contain images. But when I get tweet data in line if media in data.entities:, I get error AttributeError: str object has no attribute entities.
I tried adding to the line
twitterStream = Stream (auth, listener (), include_entities = 1)
but it does not work either
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import pprint
consumer_key = "xxxxxxxxxxxxx"
consumer_secret = "xxxxxxxxxxxxxxxxx"
access_token = "xxxxxxxxxxxxxxxxxx"
access_secret = "xxxxxxxxxxxx"
class listener(StreamListener):
def on_data(self, data):
if 'media' in data.entities:
print(data)
#for image in data.extended_entities['media']:
#print(image['media_url'])
#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())
tweets=twitterStream.filter(track=["#picture"])
Tweepy passes the raw text data to tweepy.StreamListener's on_data() method, which is used for handling the raw data from API (so you need to parse JSON string and construst tweepy.Status object).
If you handle normal status objects, you'd better use on_status() method but on_data(). This method takes Tweepy's normal Status object as an argument, so you can use this status object as usual.
So following code
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
print('#on_status')
print(type(status))
print(status.text)
def on_error(self, error_code):
print('#on_error')
print(error_code)
if error_code == 420:
return False
stream = tweepy.Stream(api.auth, MyStreamListener())
stream.filter(track=["#picture"])
will print like this:
#on_status
<class 'tweepy.models.Status'>
test1! #picture
#on_status
<class 'tweepy.models.Status'>
This is test picture tweet2! #picture
See also: Streaming With Tweepy — tweepy 3.6.0 documentation

How to get access token secret for StockTwit API?

I've already got the consumer key, consumer secret, and access token, but I don't know how to get the access token secret. This code works but I just need the access token secret. Thanks in Advance!
#!/usr/bin/python
#Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
#Variables that contains the user credentials to access stocktwits API
consumer_key = "something"
consumer_secret = "something"
access_token = "something"
#access_token_secret = ""
#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):
def on_data(self, data):
print data
return True
def on_error(self, status):
print status
if __name__ == '__main__':
#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
#This line filter Twitter Streams to capture data by the keywords
stream.filter(track=['Hillary Clinton', '#Hillary', 'Donald Trump', '#Trump'])
StockTwits does not provide an access_token_secret. Once you have an access token, that token can be used on all requests to the API. You'll want to see of your OAuthHandler class allows only setting an access_token. It should.

Retrieve tweets tweepy

How can I retrieve only my tweets with a stream? I test that but I don't see my tweets.
My first attempt:
streamingAPI = tweepy.streaming.Stream(auth, CustomStreamListener())
streamingAPI.userstream(_with='followings')
streamingAPI.filter()
My second attempt:
streamingAPI = tweepy.streaming.Stream(auth, CustomStreamListener())
streamingAPI.filter(follow= ['2466458114'])
Thanks a lot.
If you want stream only tweets on your user, you can use the following lines:
from tweepy import StreamListener
from tweepy import Stream
import tweepy
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
class CustomStreamListener(StreamListener):
def on_data(self, data):
print(data)
def on_error(self, status):
print(status)
if __name__ == '__main__':
listener = CustomStreamListener()
twitterStream = Stream(auth, listener)
twitterStream.filter(follow=['2466458114'])
In your question, you said that you can't see your tweets. I don't know if is clear or not but just to be sure, with streaming you can see only the "real time" tweets. So also with my code, if you don't tweet nothing, you don't see nothing.
UPDATE AFTER CHAT IN COMMENTS
Since Twitter Official API has the bother limitation of time constraints, you can't get older tweets than a week.
For this task I suggest you to use this great python library.
It allows to get how many tweets you want and wrote when you want.
As documentation says, you can simply use it in this way:
tweetCriteria = got.manager.TweetCriteria().setUsername('<user_without_#>').setSince("2015-05-01").setUntil("2015-09-30")
If you are using python2.X you can use got, instead if you are using python3.X you can use got3.
I prepare an example in Python3:
from getOldTweets import got3
tweetCriteria = got3.manager.TweetCriteria().setUsername('barackobama').setSince("2015-09-01").setUntil("2015-09-30")
tweets_list = got3.manager.TweetManager.getTweets(tweetCriteria)
for tweet in tweets_list:
print(tweet.text)
Let me know.

Newest tweets on tweepy for python?

I'm using tweepy to find tweets containing a certain word, but I want to just get the newest tweets from the last five minutes up. How would I go about this? This is my code at the moment.
import tweepy
consumer_key = "**********"
consumer_secret = "**********"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token("**********", "**********")
api = tweepy.API(auth)
public_tweets = api.search(q = "", since = "2015-09-26", language = "EN")
for tweet in public_tweets:
print(tweet.text)
First of all: I edited your post to remove your credentials, I would suggest you get new ones from twitter and never share them again.
Also change your api.search (Rest API) to the Streaming API. This will give you a portion of tweets that match your criteria for the moment you open that connection.
For example
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
consumer_key = '****'
consumer_secret = '****'
access_token = '****'
access_secret = '****'
class Listener(StreamListener):
def on_status(self, status):
try:
print(str(status.text.encode('utf-8')))
except Exception as e:
print(e)
def on_error(self, status_code):
print(status_code)
while True:
try:
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
twitterStream = Stream(auth, Listener())
twitterStream.filter(q=['python'])
except Exception as e:
print(e)

Twitter Streaming API with Tweepy rejects oauth

I'm trying to access the Twitter stream which I had working previously while improperly using Tweepy. Now that I understand how Tweepy is intended to be used I wrote the following Stream.py module. When I run it, I get error code 401 which tells me my auth has been rejected. But I had it working earlier with the same consumer token and secret. Any ideas?
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy import TweepError
from tweepy import error
#Removed. I have real keys and tokens
consumer_key = "***"
consumer_secret = "***"
access_token="***"
access_token_secret="***"
class CustomListener(StreamListener):
""" A listener handles tweets are the received from the stream.
This is a basic listener that just prints received tweets to stdout."""
def on_status(self, status):
# Do things with the post received. Post is the status object.
print status.text
return True
def on_error(self, status_code):
# If error thrown during streaming.
# Check here for meaning:
# https://dev.twitter.com/docs/error-codes-responses
print "ERROR: ",; print status_code
return True
def on_timeout(self):
# If no post received for too long
return True
def on_limit(self, track):
# If too many posts match our filter criteria and only a subset is
# sent to us
return True
def filter(self, track_list):
while True:
try:
self.stream.filter(track=track_list)
except error.TweepError as e:
raise TweepError(e)
def go(self):
listener = CustomListener()
auth = OAuthHandler(consumer_key, consumer_secret)
self.stream = Stream(auth,listener,timeout=3600)
listener.filter(['LOL'])
if __name__ == '__main__':
go(CustomListener)
For anyone who happens to have the same issue, I should have added this line after auth was initialized:
auth.set_access_token(access_token, access_token_secret)

Categories