如何自定义和使用 Phirehose 功能? [英] How do I customize and use Phirehose functions?

查看:47
本文介绍了如何自定义和使用 Phirehose 功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查 Phirehose 在 10 秒或 100 条推文后停止运行......基本上,我希望能够停止脚本.

I'm trying to put in a check for Phirehose to stop running after 10 seconds or 100 tweets...basically, I want to be able to stop the script.

有人告诉我可以自定义 statusUpdate() 函数或 heartBeat() 函数,但我不确定如何做到这一点.现在,我只是用 filter-track.php 示例进行测试.

I was told I could customize the statusUpdate() function or the heartBeat() function, but I'm uncertain how to do that. Right now, I'm just testing with the filter-track.php example.

如何自定义函数,应该在类中的什么地方调用它们?

How do I customize the functions, and where should I call them in the class?

class FilterTrackConsumer extends OauthPhirehose
{
  /**
   * Enqueue each status
   *
   * @param string $status
   */

  public function enqueueStatus($status)
  {

    /*
     * In this simple example, we will just display to STDOUT rather than enqueue.
     * NOTE: You should NOT be processing tweets at this point in a real application, instead they should be being
     *       enqueued and processed asyncronously from the collection process.
     */
    $data = json_decode($status, true);
    if (is_array($data) && isset($data['user']['screen_name'])) {
      print $data['user']['screen_name'] . ': ' . urldecode($data['text']) . "\n";
    }


  }

  public function statusUpdate()
  {
    return "asdf";
  }

}

// The OAuth credentials you received when registering your app at Twitter
define("TWITTER_CONSUMER_KEY", "");
define("TWITTER_CONSUMER_SECRET", "");


// The OAuth data for the twitter account
define("OAUTH_TOKEN", "");
define("OAUTH_SECRET", "");

// Start streaming
$sc = new FilterTrackConsumer(OAUTH_TOKEN, OAUTH_SECRET, Phirehose::METHOD_FILTER);
$sc->setLang('en');
$sc->setTrack(array('love'));
$sc->consume();

推荐答案

要在 100 条推文后停止,在接收推文的函数中设置一个计数器,并在完成后调用退出:

To stop after 100 tweets, have a counter in that function receiving the tweets, and call exit when done:

class FilterTrackConsumer extends OauthPhirehose
{
  private $tweetCount = 0; 
  public function enqueueStatus($status)
  {
    //Process $status here
    if(++$this->tweetCount >= 100)exit;
  }
...

(代替 exit,您可以抛出异常,并在 $sc->consume(); 行周围放置一个 try/catch.)

(Instead of exit you could throw an exception, and put a try/catch around your $sc->consume(); line.)

对于在 10 秒后"关闭,这很容易,如果它可以是大约 10 秒(即在 enqueueStatus() 中进行时间检查,如果已经超过 10 秒,则退出)程序已启动),但如果您希望它恰好是 10 秒,则很难.这是因为 enqueueStatus() 仅在一条推文进来时被调用.所以,作为一个极端的例子,如果你在前 9 秒内收到 200 条推文,但随后它变得安静,第 201 条推文会没有到达 80 秒以上,您的程序将不会退出,直到程序运行 89 秒.

For shutdown "after 10 seconds", this is easy if it can be roughly 10 seconds (i.e. put a time check in enqueueStatus(), and exit if it has been more than 10 seconds since the program started), but hard if you want it to be exactly 10 seconds. This is because enqueueStatus() is only called when a tweet comes in. So, as an extreme example, if you get 200 tweets in the first 9 seconds, but then it goes quiet and the 201st tweet does not arrive for 80 more seconds, your program would not exit until the program has been running 89 seconds.

退后一步,想要阻止 Phirehose 通常表明它是错误的工作工具.如果您只想一次又一次地轮询最近的 100 条推文,那么 REST API 进行简单搜索会更好.流 API 更适用于打算 24/7 全天候运行的应用程序,并希望在推文发布后立即对推文做出反应.(更重要的是,如果您过于频繁地连接,Twitter 会限制或关闭您的帐户.)

Taking a step back, wanting to stop Phirehose is normally a sign it is the wrong tool for the job. If you just want to poll 100 recent tweets, every now and again, then the REST API, doing a simple search, is better. The streaming API is more for applications that intend to run 24/7, and want to react to tweets as soon as they are, well, tweeted. (More critically, Twitter will rate-limit, or close, your account if you connect too frequently.)

这篇关于如何自定义和使用 Phirehose 功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆