-
Notifications
You must be signed in to change notification settings - Fork 153
/
Tweet.scala
75 lines (66 loc) · 2.33 KB
/
Tweet.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
66
67
68
69
70
71
72
73
74
75
package models
import org.joda.time.DateTime
import scala.concurrent.Future
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.json.{ Json, JsObject, JsValue }
import reactivemongo.api.Cursor
import reactivemongo.core.commands.Count
import play.modules.reactivemongo.json.collection.JSONCollection
import birdwatchUtils.Mongo
import play.api.libs.iteratee.Enumerator
/** Simple Tweet representation */
case class Tweet(
tweet_id: Long,
screen_name: String,
text: String,
wordCount: Int,
charCount: Int,
profile_image_url: String,
created_at: DateTime
)
/** holds the state for GUI updates (list of recent tweets and a word frequency map), used for Json serialization */
case class TweetState(
tweetList: List[Tweet],
wordMap: Map[String, Int],
charCountMean: Double,
charCountStdDev: Double,
wordCountMean: Double,
wordCountStdDev: Double,
n: Int
)
/** Data Access Object for Tweets*/
object Tweet {
def rawTweets: JSONCollection = Mongo.db.collection[JSONCollection]("rawTweets")
def insertJson(json: JsValue) = rawTweets.insert[JsValue](json)
/** get collection size from MongoDB (fast) */
def count: Future[Int] = Mongo.db.command(Count("rawTweets"))
/** Query latest tweets as List */
def jsonLatestN(n: Int): Future[List[JsObject]] = {
val cursor: Cursor[JsObject] = rawTweets
.find(Json.obj("text" -> Json.obj("$exists" -> true)))
.sort(Json.obj("_id" -> -1))
.cursor[JsObject]
cursor.toList(n)
}
/** Enumerate latest Tweets (descending order). Usage example:
*
* val dbTweetIteratee = Iteratee.foreach[JsObject] {
* json => TweetReads.reads(json) match {
* case JsSuccess(t: Tweet, _) => tweetChannel.push(WordCount.wordsChars(t)) // word and char count for each t
* case JsError(msg) => println(json)
* }
* }
* Tweet.enumJsonLatestN(500)(dbTweetIteratee)
*
* Not currently usable: enumerates in wrong order, but there is no good way to pick latest n tweets otherwise,
* without going from newest backwards
* @param n number of results to enumerate over
**/
def enumJsonLatestN(n: Int): Enumerator[JsObject] = {
val cursor: Cursor[JsObject] = rawTweets
.find(Json.obj("text" -> Json.obj("$exists" -> true)))
.sort(Json.obj("_id" -> -1))
.cursor[JsObject]
cursor.enumerate(n)
}
}