-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathSearchAndSaveTweets.scala
More file actions
42 lines (34 loc) · 1.54 KB
/
SearchAndSaveTweets.scala
File metadata and controls
42 lines (34 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package rest
import com.danielasfregola.twitter4s.TwitterRestClient
import com.danielasfregola.twitter4s.entities.Tweet
import com.danielasfregola.twitter4s.entities.enums.ResultType
import com.typesafe.config.ConfigFactory
import rest.utils.FileSupport
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
object SearchAndSaveTweets extends App with FileSupport {
// TODO - Make sure to define your consumer and access tokens!
val client = TwitterRestClient()
def searchTweets(query: String, max_id: Option[Long] = None): Future[Seq[Tweet]] = {
def extractNextMaxId(params: Option[String]): Option[Long] = {
//example: "?max_id=658200158442790911&q=%23scala&include_entities=1&result_type=mixed"
params.getOrElse("").split("&").find(_.contains("max_id")).map(_.split("=")(1).toLong)
}
client.searchTweet(query, count = 100, result_type = ResultType.Recent, max_id = max_id).flatMap { ratedData =>
val result = ratedData.data
val nextMaxId = extractNextMaxId(result.search_metadata.next_results)
val tweets = result.statuses
if (tweets.nonEmpty) searchTweets(query, nextMaxId).map(_ ++ tweets)
else Future(tweets.sortBy(_.created_at))
} recover { case _ => Seq.empty }
}
val filename = {
val config = ConfigFactory.load()
config.getString("tweets.scalax")
}
val result = searchTweets("#scalax").map { tweets =>
println(s"Downloaded ${tweets.size} tweets")
toFileAsJson(filename, tweets)
println(s"Tweets saved to file $filename")
}
}