python - twitter stream JSON decoding -
so, have created geo map box gather tweets, , want more precise locations using long;lat.
i need out coordinates (long;lat separate) without rest of "coordinates" data.
im using tweepy , understand iam not decoding right can't seem understand why doesn't work.
and , how keep failing
input json
{ u'contributors':none, u'truncated':false, u'text': u'stundas tikai l\u012bdz 12.00 \u0001f64c\u0001f389\u0001f389\u0001f389 (@ r\u012bgas valsts v\u0101cu \u0123imn\u0101zija - @rvv_gimnazija in r\u012bga) https://t.co/xcp8ozqqgk', u'in_reply_to_status_id':none, u'id':599100313690320896, u'favorite_count':0, u'source': u'<a href="http://foursquare.com" rel="nofollow">foursquare</a>', u'retweeted':false, u'coordinates':{ u'type':u'point', u'coordinates':[ 24.062859, 56.94697 ] },
my code
class listener(streamlistener): def on_data(self, data): tweet = json.loads(data) #print time.time() text = tweet['text'] name = tweet['user']['name'] screenname = tweet['user']['screen_name'] location = tweet['coordinates']['coordinates'][0] print name.encode('utf-8') print text.encode('utf-8') print location print '\n' # data file open('mineddata', 'a') outfile: json.dump({ 'location':location, 'time': time.time(), 'screenname': screenname, 'text': text, 'name': name}, outfile, indent = 4, sort_keys=true) #outfile.write(',') outfile.write('\n') return true def on_error(self, status): print status auth = oauthhandler(ckey, csecret) auth.set_access_token(atoken, asecret) twitterstream = stream(auth, listener()) twitterstream.filter(locations=[23.47,56.66,25.148411,57.407558])
the error
traceback (most recent call last): file "loc3.py", line 45, in <module> twitterstream.filter(locations=[23.47,56.66,25.148411,57.407558]) file "/library/python/2.7/site-packages/tweepy/streaming.py", line 428, in filter self._start(async) file "/library/python/2.7/site-packages/tweepy/streaming.py", line 346, in _start self._run() file "/library/python/2.7/site-packages/tweepy/streaming.py", line 255, in _run self._read_loop(resp) file "/library/python/2.7/site-packages/tweepy/streaming.py", line 309, in _read_loop self._data(next_status_obj) file "/library/python/2.7/site-packages/tweepy/streaming.py", line 289, in _data if self.listener.on_data(data) false: file "loc3.py", line 23, in on_data location = tweet['coordinates']['coordinates'][0] typeerror: 'nonetype' object has no attribute '__getitem__'
by looking @ other examples, looks argument receive in on_data
parsed dict, not raw json. there no json read , therefore tweet
ends empty.
the quick , simple fix change
def on_data(self, data): tweet = json.loads(data)
into simply
def on_data(self, tweet):
and take there.
i note coordinates bounding box seem in wrong order -- location should specified southwest , northeast coordinates.
Comments
Post a Comment