salty_coffee
salty_coffee

Reputation: 631

Tweepy JSON: 'NoneType' object has no attribute '__getitem__'

Hey My code is currently streaming live tweets to a database. The code will run for a while, 5-10 min, however; it will eventually give me the following error and quit:

File "twittergeo.py", line 198, in

File "/Library/Python/2.7/site-packages/tweepy/streaming.py", line 445, in filter self._start(async) File "/Library/Python/2.7/site-packages/tweepy/streaming.py", line 361, in _start self._run() File "/Library/Python/2.7/site-packages/tweepy/streaming.py", line 294, in _run raise exception TypeError: 'NoneType' object has no attribute 'getitem'

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
import MySQLdb

canada =[-141.0,41.7,-51.0,83.7]

consumer_key = '????????????'
consumer_secret = '??????????????' 
access_token = '????????????????????????'
access_secret = '??????????????'

class TweetListener(StreamListener):




   def on_data(self, data):
      alldata = json.loads(data)
      newdata = json.dumps(data)

      created_at =        alldata["created_at"] #primary key
      tweetId =           alldata["id"]
      text =              alldata["text"]#must be above the dictlists

      userId =            alldata["user"]["id"] #primarykey
      twitterHandle =     alldata["user"]["screen_name"]
      name =              alldata["user"]["name"]
      location =          alldata["user"]["location"]
      url =               alldata["user"]["url"]
      bio =               alldata["user"]["description"]
      protected =         alldata["user"]["protected"]
      followers_count =   alldata["user"]["followers_count"]
      friends_count =     alldata["user"]["friends_count"]
      geo_enabeled =      alldata["user"]["geo_enabled"]
      lang =              alldata["user"]["lang"]
      profile_image_url = alldata["user"]["profile_image_url"]

      placeId =           alldata["place"]["id"]#primarykey
      cityName =          alldata["place"]["name"]
      fullName =          alldata["place"]["full_name"]
      country_code =      alldata["place"]["country_code"]
      country =           alldata["place"]["country"]
      bounding_box =      alldata["place"]["bounding_box"]  #bug

      hashtags =          alldata["entities"]["hashtags"]  #bug
      user_mentions =     alldata["entities"]["user_mentions"]

  return True

   def on_error(self, status):

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

stream = Stream(auth, TweetListener())
stream.filter(locations=canada)

Iv looked through StackOverflow and tried some solutions, however; none seem to work.

Upvotes: 0

Views: 2288

Answers (1)

Mike JS Choi
Mike JS Choi

Reputation: 1151

Well, I'm going to assume that line 198 is

bounding_box =      alldata["place"]["bounding_box"]  #bug

or any line that is trying to 'get an item' from the dictionary alldata.

The error TypeError: 'NoneType' object has no attribute 'getitem' means that you are trying to access an object from a NoneType object. The reason your code is crashing after a couple of minutes is probably because one of the many requests you are making is returning an empty or a partially empty dictionary.

It's as if you are trying to do this...

alldata = None
bounding_box = alldata["place"]["whatever"]

To solve this, I would put a huge try-catch block around on_data like so

try:
    res = on_data(data)
except Exception as e:
    print(e)    # Just for debuggin purposes

Upvotes: 2

Related Questions