1

What am I trying to achive?

I'm trying to build a Flask REST Api that's going to get a search_text from the user and then listen for tweets containing that search_text using Tweepy and return the only relevant information I need like username, tweet_content corresponding to each tweet and then disconnect after a minute.

The problem

When I test the API using Postman, it shows Loading.. for sometime and then returns null. Apparently MyStreamListener is not behaving the way I was expecting it to.
The on_status method should get called everytime some data is received from twitter.
I suppose I have to return it inside my post function to work, which is infact returning nothing right now so I think I kind of know what the problem is. How can I implement this in Flask?

My code

class MyStreamListener(tweepy.StreamListener):

    def on_status(self, status):
        return jsonify({'user_name': status.user.name, "user_screen_name": status.user.screen_name, 'tweet_content': status.text})


class FetchReturn(Resource):
    parser = reqparse.RequestParser()
    parser.add_argument('search_text', type=str, required=True)

    def post(self):
        search_text = FetchReturn.parser.parse_args()['search_text']  
        myStream = tweepy.Stream(auth=api.auth, listener=MyStreamListener())
        myStream.filter(track=[search_text], async=True, languages=["en"])
        time.sleep(60)
        myStream.disconnect()
Dipanshu Juneja
  • 1,204
  • 14
  • 29

0 Answers0