本文介绍了使用Django tweepy流API集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Django的web应用程序是通过tweepy.Stream()函数使用Twitter的流API。我有困难的时候,概念化的正确实施。

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.

我想有最简单的功能是计算包含在实时的hashtag鸣叫的次数。所以,我会打开一个流,通过关键词过滤,每次新推过来我递增计数器的连接。该计数器然后显示在网页上,并更新了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?

在此先感谢!

推荐答案

有不同的方法可以做到这一点,但使用消息LIB(芹菜)很可能将是最容易的。

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

1)保持一个python进程在运行tweepy。一旦一个有趣的消息被找到,创建一个新的芹菜任务

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

2)在这个胡萝卜任务持久化数据到数据库(柜台,微博,等等)。这个任务可以同时运行的Django code(如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.

作为precaution,它可能是一个美好的理想下运行监督tweepy过程(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 tweepy流API集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 04:39