Blank space in Twitter direct messages with tweepy from JSON API - python

So I have the following python code which receives notification of received direct messages via Tweepy:
#!/usr/bin/env python
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import simplejson as json
consumer_key = "secret"
consumer_secret = "secret"
access_token = "secret"
access_token_secret = "secret"
class StdOutListener(StreamListener):
def __init__(self):
print "init"
def on_connect(self):
print "Connected"
def on_disconnect(self, status):
print ("Disconnected", status)
def on_direct_message(self, status):
print ("on_direct_message", status)
def on_data(self, status):
# print ("on_data", status)
decoded = json.loads(status)
## grab the direct message
directMessage = decoded['direct_message']
message = directMessage.get('text', None)
message.strip()
print "message:*", message, "*"
return True
def on_error(self, status):
print ("on_error", status)
if __name__ == '__main__':
## Connect to Twitter
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
## Init the StreamListener
print ("Init Stream")
l = StdOutListener()
stream = Stream(auth, l)
stream.userstream()
When it runs and I send the direct message "test message" I get the following printed to the console:
message:* text message *
ie the message was received and parsed but padded with space at either end which message.strip() didn't even correct. If I uncomment the print directMessage line and view the json sent by Twitter there is no space.
I cannot work out if there is a problem with my JSON editing or usage of tweepy or something else.
I've also tried using the json package as well as simplejson.

Related

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.

Collecting URI's From Tweets

I am currently writing a python program that utilizes Tweepy & the Twitter API, and extracts URI links from tweets on twitter.
This is currently my code. How do I modify it so that it only outputs the URIs from tweets(if there is one included)?
#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 Twitter API
access_token = "-"
access_token_secret = ""
consumer_key = ""
consumer_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 keyword: '#NFL'
twitterator = stream.filter(track=[ '#NFL' ])
for tweet in twitterator:
print "(%s) #%s %s" % (tweet["created_at"], tweet["user"]["screen_name"], tweet["text"])
for url in tweet["entities"]["urls"]:
print " - found URL: %s" % url["expanded_url"]
I've modified your code to only print URLs if present:
#Import the necessary methods from tweepy library
import json
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
#Variables that contains the user credentials to access Twitter API
access_token = "-"
access_token_secret = ""
consumer_key = ""
consumer_secret = ""
#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):
def on_data(self, data):
tweet = json.loads(data)
for url in tweet["entities"]["urls"]:
print " - found URL: %s" % url["expanded_url"]
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 keyword: '#NFL'
stream.filter(track=[ '#NFL' ])

Twitter Streaming API not giving any results

I'm running the following code to get tweets related to the below keywords.
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
access_token = "K"
access_token_secret = ""
consumer_key = ""
consumer_secret = ""
class StdOutListener(StreamListener):
def on_data(self, data):
print data
return True
def on_error(self, status):
print status.text
if __name__ == '__main__':
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
stream.filter(track=["flyspicejet","jetairways","airindiain","goairlinesindia","airvistara","spicejet","airindia","goairlines","vistara"])
However I ran this for a few hours but got no results!How can it be possible that not even a single tweet with these keywords(popular airlines in India) was published?
What am I doing wrong?
Thanks a lot in advance.
You're not handing the error cases properly. Try the following code:
Python 3 version:
import time
import argparse
import os
import sys
import json
#Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from http.client import IncompleteRead
access_token = "acc token here"
access_token_secret = "acc token secret"
consumer_key = "consumer key"
consumer_secret = "consumer secret here"
class StdOutListener(StreamListener):
'''A basic listener that just writes received tweets to file.'''
def __init__(self, outputfile):
self.file = outputfile
def on_data(self, data):
with open(self.file, 'a') as ofile:
#simply write everything to file as raw json
ofile.write(data)
return True
def on_error(self, status):
print(status)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
requiredArgs = parser.add_argument_group('must need arguments')
requiredArgs.add_argument('-o', '--output', help='Output txt file to write tweets', required=True)
args = parser.parse_args()
filepath = os.getcwd() + os.path.sep + args.output
if os.path.exists(filepath):
sys.exit("output file already exists; Give new filename!")
else:
#create an empty file
open(args.output,'a').close()
l = StdOutListener(args.output)
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
keyword_list = ['Computer Science', 'Cancer', 'Germany', 'Paris']
while True:
try:
stream = Stream(auth, l)
stream.filter(track=keyword_list, stall_warnings=True)
except IncompleteRead as e:
# Oh well, sleep sometime & reconnect and keep trying again
time.sleep(15)
continue
except KeyboardInterrupt:
stream.disconnect()
break

Tweepy Stream error: __init__() takes exactly 3 arguments (4 given)

I'm trying to use the Tweepy python api to create a twitter search stream but I'm facing an error. Here is the code I am trying to execute and the error I'm getting ->
File "code.py", line 28, in <module>
stream = Stream(auth, x, "microsoft")
__init__() takes exactly 3 arguments (4 given)
(Thanks for helping. Sorry, I'm a beginner)
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
# Twitter Credentials
access_token_key = "*****"
access_token_secret = "*****"
consumer_key = "*****"
consumer_secret = "*****"
class StdOutListener(StreamListener):
""" A listener handles tweets that are the received from the stream.
This is a basic listener that just prints received tweets to stdout.
"""
def on_data(self, data):
print data
return True
def on_error(self, status):
print status
if __name__ == '__main__':
x = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token_key, access_token_secret)
stream = Stream(auth, x, "microsoft")
response = stream.filter(track=['microsoft'])
print response
Problem lies in stream intitialization line:
stream = Stream(auth, x, "microsoft")
that should be
stream = Stream(auth, x)
tracked words are passed not with constructor but in filter method.
Stream() only takes 2 arguments (plus self for bound methods):
stream = Stream(auth, x)
stream.filter(track=['microsoft'])
See the streaming example given by the Tweepy project.
The 307 redirect should probably be handled as not an error; you can tell Tweepy to continue by returning True from the on_error handler:
def on_error(self, status):
print status
if status == 307:
return True

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