how to get the tweets from the stopped stream using twitter4s

73 Views Asked by At

i am using twitter4s https://github.com/DanielaSfregola/twitter4s here it shows how to we get the twitter stream and stops it

def simulateNextActionAfterMillis(millis: Long): Future[Unit] = Future {
      Thread.sleep(millis); println()
    }

    for {
      streamA <- streamingClient.sampleStatuses(stall_warnings = true)(printTweetText)
      _ <- simulateNextActionAfterMillis(10000)
    } yield streamA.close()

def printTweetText: PartialFunction[StreamingMessage, Unit] = {
      case tweet: Tweet =>
        println(tweet.text)
    }

all i want is the total tweets object after the stream closes how can i get that ?

1

There are 1 best solutions below

0
sarveshseri On

I could not really understand your issue by looking at the code, so can not answer the real question.

But I thought to point out that you should never simulate a future by "sleeping" on a thread.

You can create a non-blocking future using following,

import java.util.{Timer, TimerTask}

import scala.concurrent.{Future, Promise}
import scala.concurrent.duration.Duration
import scala.util.Try

object FutureUtils {

  val timer = new Timer();

  def createDelayedFuture[T](delayInMills: Long, creator: () => T): Future[T] = {
    val promise = Promise[T]()
    timer.schedule(
      new TimerTask {
        override def run(): Unit = promise.complete(Try(creator()))
      },
      delayInMills
    )
    promise.future
  }
}