tweepy Streaming API integration with Django

2019-03-10 20:37发布

I am trying to create a Django webapp that utilizes the Twitter Streaming API via the tweepy.Stream() function. I am having a difficult time conceptualizing the proper implementation.

The simplest functionality I would like to have is to count the number of tweets containing a hashtag in real time. So I would open a stream, filtering by keywords, every time a new tweet comes over the connection i increment a counter. That counter is then displayed on a webpage and updated with AJAX or otherwise.

The problem is that the tweepy.Stream() function must be continuously running and connected to twitter (thats the point). How can I have this stream running in the background of a Django app while incrementing counters that can be displayed in (near) real time?

Thanks in advance!

1条回答
别忘想泡老子
2楼-- · 2019-03-10 21:12

There are various ways to do this, but using a messaging lib (celery) will probably be the easiest.

1) Keep a python process running tweepy. Once an interesting message is found, create a new celery task

2) Inside this carrot task persist the data to the database (the counter, the tweets, whatever). This task can well run django code (e.g the ORM).

3) Have a regular django app displaying the results your task has persisted.

As a precaution, it's probably a good ideal to run the tweepy process under supervision (supervisord might suit your needs). If anything goes wrong with it, it can be restarted automatically.

查看更多
登录 后发表回答