Permalink
Browse files

Merge branch 'master' of https://github.com/bruntonspall/fthackday

Conflicts:
	app/controllers/Application.scala
  • Loading branch information...
2 parents d046219 + ad0a51e commit 4783543eb3c19f8e35246ca52dd758293a79f50b @bruntonspall committed May 23, 2012
Showing with 43 additions and 6 deletions.
  1. +7 −3 app/controllers/Application.scala
  2. +34 −2 app/model/NYTStoryCollector.scala
  3. +2 −1 conf/routes
View
10 app/controllers/Application.scala
@@ -20,7 +20,7 @@ object Application extends Controller {
val allStories : Promise[Seq[AbstractStory]] = for {
gustory <- GUStoryCollector.storiesSince(dt)
ftstory <- FTStoryCollector.storiesSince(dt)
- nytstory <- NYTStoryCollector.storiesSince(dt)
+ nytstory <- NYTNewsStreamStoryCollector.storiesSince(dt)
} yield gustory ++ ftstory ++ nytstory
val sortedStories = allStories map { stories => stories.sortWith((a,b) => a.publicationDate.isBefore(b.publicationDate))}
@@ -36,8 +36,12 @@ object Application extends Controller {
Ok(toJson(GUStoryCollector.storiesSince(DateTime.now.minusHours(24)).await.get))
}
- def stats_nyt = Action {
- Ok(toJson(NYTStoryCollector.storiesSince(DateTime.now.minusHours(24)).await.get))
+ def stats_nyt_news = Action {
+ Ok(toJson(NYTNewsStreamStoryCollector.storiesSince(DateTime.now.minusHours(24)).await.get))
+ }
+
+ def stats_nyt_search = Action {
+ Ok(toJson(NYTSearchStoryCollector.storiesSince(DateTime.now.minusHours(24)).await.get))
}
def stats_wp = Action {
View
36 app/model/NYTStoryCollector.scala
@@ -6,7 +6,7 @@ import org.joda.time.format.ISODateTimeFormat
import org.joda.time.{Interval, DateTime}
-object NYTStoryCollector {
+object NYTNewsStreamStoryCollector extends StoryImporter {
val key = "9a82cf24873041c9afa57ae3d264cc00:1:66152292"
@@ -28,7 +28,39 @@ object NYTStoryCollector {
case ts: JsArray => ts.value.map{v => v.as[String] }
case _ => Nil
}
- val pubTime = DateTime.parse((result \ "updated_date").as[String], ISODateTimeFormat.dateTimeNoMillis())
+ val pubTime = parseDate((result \ "updated_date").as[String])
+
+ AbstractStory(
+ title,
+ tags,
+ pubTime
+ )
+ }
+ }
+ }
+}
+
+object NYTSearchStoryCollector extends StoryImporter {
+
+ val key = "e9cc8b063f5a3b39a4e92319e1da45c2:4:66152292"
+
+ implicit val reads = Reads
+
+ def storiesSince(dt: DateTime) = { // actually stories on (and only one page at that...)
+
+ dt.toString(ISODateTimeFormat.basicDate())
+
+ WS.url("http://api.nytimes.com/svc/search/v1/article?format=json&query=+date:%s&fields=date,title,des_facet&api-key=%s".format(dt.toString(ISODateTimeFormat.basicDate()), key)).get.map {
+ response =>
+ val results = (response.json \ "results").asInstanceOf[JsArray]
+ results.value map { result =>
+
+ val title = (result \ "title").as[String]
+ val tags = (result \ "des_facet") match {
+ case ts: JsArray => ts.value.map{v => v.as[String] }
+ case _ => Nil
+ }
+ val pubTime = DateTime.parse((result \ "date").as[String], ISODateTimeFormat.basicDate())
AbstractStory(
title,
View
3 conf/routes
@@ -4,8 +4,9 @@
# Home page
GET / controllers.Application.index
+GET /stories/nyt/news controllers.Application.stats_nyt_news
+GET /stories/nyt/search controllers.Application.stats_nyt_search
GET /stories/ft controllers.Application.stats_ft
-GET /stories/nyt controllers.Application.stats_nyt
GET /stories/gu controllers.Application.stats_gu
GET /stories/wp controllers.Application.stats_wp
GET /stories controllers.Application.stats

0 comments on commit 4783543

Please sign in to comment.