-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwitterStream.scala
65 lines (40 loc) · 1.46 KB
/
TwitterStream.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
object TwitterStreamer extends App {
// Begin by configuring necessary credentials
val conf = ConfigFactory.load()
//All the keys necessary to access Twitter
private val consumerKey
private val consumerSecret
private val accessToken
private val accessTokenSecret
private val url = "https://stream.twitter.com/1.1/statuses/filter.json"
// declare ActorSystem, Materializer and format
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val formats = DefaultFormats
//Create a header to load all the credentials declared earlier
val oauthHeader: Future[String] = consumer.createOauthenticatedRequest(
KoauthRequest())
map (_.header)
// Pass header into Success method
oauthHeader.onComplete {
case Success(header) =>
HttpHeader.parse("Authorization", header)
// returns ParsingResult
HttpHeader.parse("Accept", header
// returns ParsingResult
// Declare HttpResquest and all its specific needs
val httpRequest: HttpRequest = HttpRequest(
method = HttpMethods.POST,
uri = source,
headers = httpHeaders,
entity= (//this is main body of he tweet)
// Call request
val request = Http().singleRequest(httpRequest)
response.entity.dataBytes
.scan("")((acc, curr) => if (acc.contains("\r\n")) curr.utf8String else acc + curr.utf8String)
.filter(_.contains("\r\n"))
.map(json => Try(parse(json).extract[Tweet]))
.runForeach
Success()
Failure()
case Failure(failure) => println(failure.getMessage)