-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathUserMentionStream.scala
32 lines (24 loc) · 1.02 KB
/
UserMentionStream.scala
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
package streaming
import com.danielasfregola.twitter4s.entities.Tweet
import com.danielasfregola.twitter4s.entities.enums.WithFilter
import com.danielasfregola.twitter4s.{TwitterRestClient, TwitterStreamingClient}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
object UserMentionStream extends App {
// TODO - Make sure to define your consumer and access tokens!
val restClient = TwitterRestClient()
val streamingClient = TwitterStreamingClient()
def fetchSelfUserId(): Future[Long] =
restClient.verifyCredentials().map(_.data.id)
def tweetContainsUserMention(tweet: Tweet, userId: Long): Boolean =
tweet.entities.exists(_.user_mentions.exists(_.id == userId))
def selfMentionStream(selfUserId: Long) =
streamingClient.userEvents(`with` = WithFilter.User) {
case t: Tweet if tweetContainsUserMention(t, selfUserId) =>
println(t.text)
}
for {
selfUserId <- fetchSelfUserId()
stream <- selfMentionStream(selfUserId)
} yield stream
}