13

我目前正在使用 Twitter API 来检索某些用户发布的推文。为了这个问题,我们将使用@justinbieber 作为示例。

使用https://stream.twitter.com/1.1/statuses/filter.json资源时,将 follow 设置为所需的用户 ID (@justinbieber = 27260086),并允许它运行,而我只希望 @justinbieber 的推文,我最终得到了他数百万粉丝发给他的推文。显然,这意味着我获得的信息比我想要的要多得多,而且根据我的发现,我有时最终会错过用户自己的推文!

我尝试更改https://dev.twitter.com/docs/streaming-apis/parameters上的每个参数均无济于事。

以下参数说明:

For each user specified, the stream will contain:

   Tweets created by the user.
   Tweets which are retweeted by the user.
   Replies to any Tweet created by the user.
   Retweets of any Tweet created by the user.
   Manual replies, created without pressing a reply button (e.g. “@twitterapi I agree”).

因为它在文档中,我假设没有办法只获得该用户的推文而不必自己过滤结果(如前所述,这意味着我最终可能会错过用户自己的推文!),但我很想知道是否有人知道解决方法。

在有人建议使用诸如 statuses/user_timeline 之类的东西之前,我知道它能够做我想做的事,但是它有两个缺点让我一直使用流 API:

  • 每个请求都意味着我丢失了一个请求,并且由于 Twitter 是速率限制的,我想避免这种情况。
  • 每个请求都有 HTTP 协议的昂贵开销。太多时间花在谈判上。

我想做的事可能吗?@justinbieber 只是高开销 Twitter 帐户的一个示例。我想使用此代码来检索许多高开销帐户的推文,因此需要速度以及查看每个用户的每条推文的能力。

4

3 回答 3

6

使用 json_decode 后,可以使用以下 IF 语句来判断是什么类型的推文:

    // if it is a retweet        
    if (isset($data['retweeted_status']))
    {
         //TODO
    }

    // if it is a reply
    else if (isset($data['in_reply_to_status_id_str']))
    {
         //TODO
    }

    // if it is a mention
    else if (isset($data['in_reply_to_user_id_str']))
    {
         //TODO
    }

    // if it is an original tweet
    else
    {
         //TODO
    }
于 2014-05-01T20:28:46.693 回答
0

我有一个类似的问题,并用我从 arstechnica 提取的一小段代码解决了

如果您使用 python pycurl 将完成这项工作。它提供了一种为接收到的每一小块数据执行功能的方法。

import pycurl, json

STREAM_URL = "http://chirpstream.twitter.com/2b/user.json"

USER = "YOUR_USERNAME"
PASS = "XXXXXXXXX"


def on_receive(self, data):
    self.buffer += data
    if data.endswith("rn") and self.buffer.strip():
        content = json.loads(self.buffer)
        self.buffer = ""

        if "text" in content and content['user'] == 'justinbieber':
            print u"{0[user][name]}: {0[text]}".format(content)

conn = pycurl.Curl()
conn.setopt(pycurl.USERPWD, "%s:%s" % (USER, PASS))
conn.setopt(pycurl.URL, STREAM_URL)
conn.setopt(pycurl.WRITEFUNCTION, on_receive)
conn.perform()

您可以在此处找到更多信息实时 twitter 流 api

于 2017-03-14T22:58:47.357 回答
-1

如果我理解正确,您应该可以为此使用用户流

于 2014-01-01T00:15:36.493 回答