Permalink
Browse files

Add wordpress endpoint, rename Gu endpoint

  • Loading branch information...
1 parent da84233 commit d046219f0663b55dac400d17e431608e07a4bda4 @bruntonspall committed May 23, 2012
@@ -6,7 +6,7 @@ import libs.json.Reads
import play.api.mvc._
import org.joda.time.DateTime
import play.api.libs.json.Json._
-import model.{FTStoryCollector, AbstractStory, GuImporter, NYTStoryCollector}
+import model._
object Application extends Controller {
implicit val reads = Reads
@@ -18,7 +18,7 @@ object Application extends Controller {
def stats = Action {
val dt = DateTime.now.minusHours(24)
val allStories : Promise[Seq[AbstractStory]] = for {
- gustory <- GuImporter.storiesSince(dt)
+ gustory <- GUStoryCollector.storiesSince(dt)
ftstory <- FTStoryCollector.storiesSince(dt)
nytstory <- NYTStoryCollector.storiesSince(dt)
} yield gustory ++ ftstory ++ nytstory
@@ -33,12 +33,14 @@ object Application extends Controller {
}
def stats_gu = Action {
- Ok(toJson(GuImporter.storiesSince(DateTime.now.minusHours(24)).await.get))
+ 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_wp = Action {
+ Ok(toJson(WPStoryCollector.storiesSince(DateTime.now.minusHours(24)).await.get))
+ }
}
@@ -5,8 +5,8 @@ import org.joda.time.format.{ISODateTimeFormat, DateTimeFormat}
import play.api.libs.json.{Writes, JsArray}
import play.api.Logger
-object GuImporter extends StoryImporter {
- val log = Logger("GuImporter")
+object GUStoryCollector extends StoryImporter {
+ val log = Logger("GUStoryCollector")
def storiesSince(dt: DateTime) = {
val url = "http://content.guardianapis.com/search.json?show-tags=keyword&from-date="+
ISODateTimeFormat.dateTimeNoMillis.withZoneUTC().print(dt)
@@ -0,0 +1,30 @@
+package model
+
+import play.api.libs.ws.WS
+import org.joda.time.DateTime
+import org.joda.time.format.ISODateTimeFormat
+import play.api.libs.json.JsArray
+import play.api.Logger
+
+object WPStoryCollector extends StoryImporter {
+ val log = Logger("WPStoryCollector")
+
+ def storiesSince(dt: DateTime) = {
+ val url = "https://public-api.wordpress.com/rest/v1/freshly-pressed/?number=50&after="+
+ ISODateTimeFormat.dateTimeNoMillis.withZoneUTC().print(dt.minusHours(24))
+
+ log.error("Fetching from " + url)
+ WS.url(url).get.map {
+ response => {
+ log.warn("Got response "+response)
+ (response.json \ "posts").asInstanceOf[JsArray].value.map { result =>
+ AbstractStory(
+ (result \ "title").as[String],
+ Nil,
+ parseDate((result \ "date").as[String])
+ )
+ }
+ }
+ }
+ }
+}
View
@@ -7,6 +7,7 @@ GET / controllers.Application.index
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
# Map static resources from the /public folder to the /assets URL path

0 comments on commit d046219

Please sign in to comment.