Pergunta

So far I have the following code that works and inserts the tweets into my mongodb but I had a few questions.

class CustomStreamListener(tweepy.StreamListener):
def __init__(self, api):
    self.api = api
    super(tweepy.StreamListener, self).__init__()

    self.db = pymongo.MongoClient().test

def on_data(self, tweet):
    self.db.tweets.insert(json.loads(tweet))

def on_error(self, status_code):
    return True # Don't kill the stream

def on_timeout(self):
    return True # Don't kill the stream


sapi = tweepy.streaming.Stream(auth, CustomStreamListener(api))
sapi.filter(track=['arsenal'] , languages = ['en'])
  1. Could someone explain how I can get only certain parts of the tweet inserted into the database ie. just the tweet text and location.

  2. Does the twitter streaming api allow displaying just tweets no @ reply tweets?

Foi útil?

Solução

json.loads(tweet) is just a dictionary, you can freely choose what parts of its key-values you process.

You can filter tweets by conditioning them either way you like:

tweet_obj = json.loads(tweet)
if not tweet_obj['in_reply_to_user_id']: # replies has `None` in this field
    pass                                 # add some processing here
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top