与Django的Streaming API集成 [英] tweepy Streaming API integration with Django
问题描述
我正在尝试创建一个通过tweepy.Stream()函数利用Twitter Streaming API的Django webapp。我有困难的时间概念化正确的实现。
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.
我想要的最简单的功能是计数包含主题标签的tweet的数量。所以我会打开一个流,按关键字过滤,每当一个新的tweet通过连接我增加一个计数器。然后,该计数器显示在网页上,并以AJAX或其他方式更新。
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.
问题是tweepy.Stream()函数必须连续运行并连接到twitter(这才是重点)。如何在Django应用程序的背景下运行这个流,同时增加可以在(近)实时显示的计数器?
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?
提前感谢!
推荐答案
有多种方法可以执行此操作,但使用消息传递库(芹菜)可能是最简单的。
There are various ways to do this, but using a messaging lib (celery) will probably be the easiest.
1)保持一个python进程运行tw。。一旦找到一个有趣的消息,创建一个新的芹菜任务
1) Keep a python process running tweepy. Once an interesting message is found, create a new celery task
2)在这个胡萝卜任务内部将数据保存到数据库(计数器,推文,无论如何)。这个任务可以很好地运行django代码(例如ORM)。
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)有一个常规的django应用程序显示您的任务已经持续的结果。
3) Have a regular django app displaying the results your task has persisted.
作为一个预防措施,在监督下运行二维过程可能是一个很好的理想(supervisord可能适合您的需要)。如果出现问题,可以自动重新启动。
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.
这篇关于与Django的Streaming API集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!