-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathTweetImplicits.scala
More file actions
98 lines (87 loc) · 3.34 KB
/
TweetImplicits.scala
File metadata and controls
98 lines (87 loc) · 3.34 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package models
import org.joda.time.DateTime
import play.api.libs.functional.syntax._
import play.api.libs.json._
import play.api.libs.json.Reads.jodaDateReads
import reactivemongo.bson.{ BSONDocument, BSONLong, BSONInteger, BSONObjectID, BSONString, BSONDateTime}
import reactivemongo.bson.handlers.{ BSONWriter, BSONReader }
import utils.TimeInterval
object TweetImplicits {
implicit val DefaultJodaDateReads = jodaDateReads("EEE MMM dd HH:mm:ss Z YYYY")
// Fields specified because of hierarchical json. Otherwise:
// implicit val streamTweetReads = Json.reads[StreamTweet]
implicit val TweetReads = (
(__ \ "id").read[Long] and
(__ \ "user" \ "screen_name").read[String] and
(__ \ "text").read[String] and
(__ \ "user" \ "location").read[String] and
(__ \ "user" \ "profile_image_url").read[String] and
(__ \ "geo").read[Option[String]] and
(__ \ "created_at").read[DateTime])(Tweet(_, _, _, 0, 0, _, _, _, _, None))
implicit object TweetBSONWriter extends BSONWriter[Tweet] {
def toBSON(t: Tweet) = {
BSONDocument(
"_id" -> t.id.getOrElse(BSONObjectID.generate),
"tweet_id" -> BSONLong(t.tweet_id),
"screen_name" -> BSONString(t.screen_name),
"text" -> BSONString(t.text),
"wordCount" -> BSONInteger(t.wordCount),
"charCount" -> BSONInteger(t.charCount),
"location" -> BSONString(t.location),
"profile_image_url" -> BSONString(t.profile_image_url),
"geo" -> BSONString(t.geo.getOrElse("")),
"created_at" -> BSONDateTime(t.created_at.getMillis)
)
}
}
implicit object TweetBSONReader extends BSONReader[Tweet] {
def fromBSON(document: BSONDocument): Tweet = {
val doc = document.toTraversable
Tweet(
doc.getAs[BSONLong]("tweet_id").get.value,
doc.getAs[BSONString]("screen_name").get.value,
doc.getAs[BSONString]("text").get.value,
doc.getAs[BSONInteger]("wordCount").get.value,
doc.getAs[BSONInteger]("charCount").get.value,
doc.getAs[BSONString]("location").get.value,
doc.getAs[BSONString]("profile_image_url").get.value,
None,
new DateTime(doc.getAs[BSONDateTime]("created_at").get.value),
doc.getAs[BSONObjectID]("_id")
)
}
}
implicit val TweetJsonWriter = new Writes[Tweet] {
def writes(t: Tweet): JsValue = {
Json.obj(
"tweet_id" -> t.tweet_id,
"img_url" -> ("/images/" + t.tweet_id.toString + ".png"),
"screen_name" -> t.screen_name,
"text" -> t.text,
"location" -> t.location,
"timestamp" -> t.created_at.getMillis,
"timeAgo" -> TimeInterval(DateTime.now.getMillis - t.created_at.getMillis).toString)
}
}
implicit val stringIntTupleWriter = new Writes[(String, Int)] {
def writes(tuple: (String, Int)): JsValue = {
Json.obj(
"key" -> tuple._1,
"value"-> tuple._2
)
}
}
implicit val tweetStateJsonWriter = new Writes[TweetState] {
def writes(ts: TweetState): JsValue = {
Json.obj(
"tweetList" -> Json.toJson(ts.tweetList),
"topWords" -> Json.toJson(ts.wordMap.toList),
"charCountMean" -> ts.charCountMean,
"charCountStdDev" -> ts.charCountStdDev,
"wordCountMean" -> ts.wordCountMean,
"wordCountStdDev" -> ts.wordCountStdDev,
"n" -> ts.n
)
}
}
}