【问题标题】:How to get streaming tweets using twitter-api in clojure?如何在 clojure 中使用 twitter-api 获取流式推文?
【发布时间】:2015-08-28 07:39:37
【问题描述】:

我是clojure的新手。我正在尝试通过 clojure 获取流式推文。我正在使用 twitter-api 库来获取流式推文,代码 sn-p 如下。

(ns hello-twitter
  (:use
   [twitter.oauth]
   [twitter.callbacks]
   [twitter.callbacks.handlers]
   [twitter.api.streaming])
  (:require
   [clojure.data.json :as json]
   [http.async.client :as ac])
  (:import
   (twitter.callbacks.protocols AsyncStreamingCallback)))

(def my-creds (make-oauth-creds "app consumer key"
                                "app consumer secret"
                                "user-access-token"
                                "user-access-token-secret"))

(def ^:dynamic
  *custom-streaming-callback*
     (AsyncStreamingCallback. (comp println #(:text %) json/read-json #(str %2))
                      (comp println response-return-everything)
                  exception-print))

(statuses-filter :params {:track "Cricket"}
         :oauth-creds my-creds
         :callbacks *custom-streaming-callback*)

返回的 json 中的 :body 部分应该会不断更新。我得到 :body 的价值就像 <core$promise$reify__6363@32cdfb83: :pending> 一样,我无法弄清楚如何获取推文。

如何建立不间断连接以获取流式推文作为输出?

【问题讨论】:

    标签: twitter clojure


    【解决方案1】:

    此值为promise。在通过 Web 发送请求时并没有阻塞线程,而是创建了一个 Promise 并将其返回给您,以便您的程序可以继续执行。稍后需要使用 deref@ 取消对 promise 的引用以获取值。

    从链接文档复制的示例:

    ;; Create a promise
    user> (def p (promise))
    #'user/p ; p is our promise
    
    ;; Check if was delivered/realized
    user> (realized? p)
    false ; No yet
    
    ;; Delivering the promise
    user> (deliver p 42)
    #<core$promise$reify__5727@47122d: 42>
    
    ;; Check again if it was delivered
    user> (realized? p)
    true ; Yes!
    
    ;; Deref to see what has been delivered
    user> @p
    42
    
    ;; Note that @ is shorthand for deref
    user> (deref p)
    42
    

    【讨论】:

    • Promise 被退回。仅通过取消对承诺的引用,我无法获得推文。那么,如何兑现承诺,以便我能够获得流式推文?
    • 抱歉,我现在知道了。我想如果这是一个无限流,那么承诺永远不会有最终交付的价值。也许您需要在取消引用的 Promise 上使用 doseq 以在每条推文发生时执行副作用?
    猜你喜欢
    • 1970-01-01
    • 2014-02-15
    • 2016-07-06
    • 2014-10-23
    • 2018-06-13
    • 2016-05-18
    • 2021-09-05
    • 2020-03-02
    • 2014-09-19
    相关资源
    最近更新 更多