Streaming API statuses/filter.json?track not delivering JSON python


#1

Hello, I am outputing a Twitter stream (“https://stream.twitter.com/1.1/statuses/filter.json?track=term”) to a file using Python. However, the file is outputting an “instance” instead of a “json” (see relevant part in bold):

import oauth2 as oauth
import urllib2 as urllib
import json

# Filled correctly, no authentication problem
api_key = "XXX"
api_secret = "XXX"
access_token_key = "XXX"
access_token_secret = "XXX"


_debug = 0

oauth_token    = oauth.Token(key=access_token_key, secret=access_token_secret)
oauth_consumer = oauth.Consumer(key=api_key, secret=api_secret)

signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()

http_method = "GET"


http_handler  = urllib.HTTPHandler(debuglevel=_debug)
https_handler = urllib.HTTPSHandler(debuglevel=_debug)

'''
Construct, sign, and open a twitter request
using the hard-coded credentials above.
'''
def twitterreq(url, method, parameters):
  req = oauth.Request.from_consumer_and_token(oauth_consumer,
                                             token=oauth_token,
                                             http_method=http_method,
                                             http_url=url, 
                                             parameters=parameters)

  req.sign_request(signature_method_hmac_sha1, oauth_consumer, oauth_token)

  headers = req.to_header()

  if http_method == "POST":
    encoded_post_data = req.to_postdata()
  else:
    encoded_post_data = None
    url = req.to_url()

  opener = urllib.OpenerDirector()
  opener.add_handler(http_handler)
  opener.add_handler(https_handler)

  response = opener.open(url, encoded_post_data)

  return response


**def fetchcontinuousstream():
  # For streaming of tweets use
  url = "https://stream.twitter.com/1.1/statuses/filter.json?track=term"
  parameters = []
  response = twitterreq(url, "GET", parameters)
  print "Type of the response"
  print type(response)
  for line in response:
    print type(line)**


**if __name__ == '__main__':
  fetchcontinuousstream()**

The result is this:

Type of the response
<type ‘instance’>
<type ‘str’>

So basically the response is an instance, not a json/dict, each line is just a string…

How can I obtain a JSON instead?


#2

if I try with this code:

response = json.loads(twitterreq(url, "GET", parameters))

it doesn’t work:

File "print.py", line 64, in fetchcontinuousstream response = json.loads(twitterreq(url, "GET", parameters)) File "/usr/lib/python2.7/json/__init__.py", line 326, in loads return _default_decoder.decode(s) File "/usr/lib/python2.7/json/decoder.py", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) TypeError: expected string or buffer

Also loads() at line level doesn’t work either:

for line in response: 
  tweet = json.loads(line) 

It doesn’t work either:

File "print.py", line 67, in fetchcontinuousstream tweet = json.loads(line) File "/usr/lib/python2.7/json/__init__.py", line 326, in loads return _default_decoder.decode(s) File "/usr/lib/python2.7/json/decoder.py", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded

#3

It’s been a while since I’ve used urllib’s opener, but try this:

print response.read()

#4

You’re nearly there. Try

for line in response:
  if line != "":
    tweet= json.loads(line)
    print tweet["text"]

#5

That did it, thanks a lot Isaach!