Permalink
Browse files

Website now fully functional

  • Loading branch information...
1 parent 24be3f1 commit f2a58831f3ea622e0338496e4b209ca505b20e99 @francisdb committed Jun 12, 2012
View
@@ -1,20 +1,22 @@
+import akka.actor.ActorRef
+import akka.actor.Props
+import akka.util.duration.intToDurationInt
import play.api.GlobalSettings
-import play.api.Application
-
+import play.api.Logger
import play.libs.Akka
-
-import akka.actor.Props
-import akka.util.duration._
-
-import services.akka.{Start, Master}
+import scala.annotation.implicitNotFound
+import services.akka.Master
+import services.akka.Start
+import controllers.Application
object Global extends GlobalSettings{
- override def onStart(app: Application){
- //Akka.system.scheduler.schedule(0 seconds, 10 minutes, masterActorRef, Start)
-
- val masterActorRef = Akka.system.actorOf(Props[Master], name = "masterActor")
-
- masterActorRef ! Start
+
+
+ override def onStart(app: play.api.Application){
+ Logger.info("Scheduling actor trigger")
+ Application.masterActorRef = Akka.system.actorOf(Props[Master], name = "masterActor")
+ Akka.system.scheduler.schedule(0 seconds, 12 hours, Application.masterActorRef, Start)
}
+
}
@@ -0,0 +1,37 @@
+@gray-blue: #505D6B;
+
+body{
+ font-family: sans-serif;
+}
+
+li{
+ margin-bottom:10px;
+ height:60px;
+ marks: none;
+ list-style: none;
+}
+
+a {
+ text-decoration: none;
+}
+
+small{
+ color: @gray-blue;
+}
+
+.year{
+ color: @gray-blue;
+}
+
+img.poster{
+ float:left;
+ max-width:60px;
+ max-height:60px;
+ margin-right:20px;
+}
+
+footer{
+ text-align: center;
+ font-size: x-small;
+ margin: 3em;
+}
@@ -3,6 +3,8 @@ package controllers
import play.api._
import play.api.mvc._
import play.api.libs.ws.WS
+import play.api.Play.current
+import play.api.libs.concurrent._
import java.io.StringReader
import java.io.ByteArrayOutputStream
import org.jsoup.Jsoup
@@ -11,44 +13,43 @@ import services.HumoReader
import org.joda.time.DateMidnight
import org.joda.time.format.DateTimeFormatter
import org.joda.time.format.DateTimeFormat
-import services.YeloReader2
-import play.api.libs.concurrent.Promise
+import services.YeloReader
import models.Movie
+import models.Broadcast
+import akka.actor.Props
+import services.akka.Master
+import services.akka.Start
+import models.helper.BroadcastInfo
+import org.joda.time.Interval
+import akka.actor.ActorRef
+import org.joda.time.DateTimeZone
object Application extends Controller {
- val channelFilter = List("Canvas", "VTM", "2BE", "VT4", "VIJFtv", "Ketnet", "Ned 3", "Vitaya")
+ val channelFilter = List("Canvas", "VTM", "2BE", "VT4", "VIJFtv", "Ketnet", "Acht", "Ned 1", "Ned 2", "Ned 3", "Vitaya", "BBC 1", "BBC 2", "BBC 3")
+ val timezone = DateTimeZone.forID("Europe/Brussels")
+
+ var masterActorRef: ActorRef = null
def index = Action { implicit request =>
- val movies = Movie.findAll();
-
- Ok(views.html.index(movies))
- }
-
- def humo = Action {
val date = new DateMidnight()
-
- Async {
- HumoReader.fetchDay(date, channelFilter).map { movies =>
- Redirect(routes.Application.index).flashing("message" -> (movies.length + " movies found on humo"))
- }
+
+ val broadcasts = Broadcast.findByInterval(new Interval(date, date.plusDays(7)))
+
+ val infos = broadcasts.map{ broadcast =>
+ val movie = broadcast.imdbId.flatMap(Movie.findByImdbId(_))
+ BroadcastInfo(broadcast, movie)
}
+
+ val sorted = infos.sortWith(_.movie.map(_.imdbRating.toDouble).getOrElse(0.0) > _.movie.map(_.imdbRating.toDouble).getOrElse(0.0))
+
+ Ok(views.html.index(sorted))
}
- def yelo = Action {
- val date = new DateMidnight()
- val url = YeloReader2.urlForDay(date)
- println(url);
-
- Async {
- WS.url(url).get().map { response =>
-
- val movies = YeloReader2.parseDay(date, response.json)
- //movies.foreach(movie => println(movie))
- Redirect(routes.Application.index).flashing("message" -> (movies.length + " events found on yelo"))
- }
- }
+ def scan = Action {
+ masterActorRef ! Start
+ Redirect(routes.Application.index).flashing("message" -> "Started database update...")
}
}
View
@@ -0,0 +1,80 @@
+package models
+
+import scala.collection.JavaConversions._
+import play.api.Play.current
+import play.modules.mongodb.jackson.MongoDB
+import net.vz.mongodb.jackson.{Id, ObjectId}
+import org.codehaus.jackson.annotate.JsonProperty
+import reflect.BeanProperty
+import com.mongodb.DBObject
+import play.api.Logger
+import org.joda.time.DateMidnight
+import org.joda.time.DateTime
+import net.vz.mongodb.jackson.DBUpdate
+import org.joda.time.format.DateTimeFormat
+import org.joda.time.DateTimeZone
+import org.joda.time.Interval
+import controllers.Application
+
+class Broadcast(
+ @ObjectId @Id val id: String,
+ @BeanProperty @JsonProperty("name") val name: String,
+ @BeanProperty @JsonProperty("channel") val channel: String,
+ @BeanProperty @JsonProperty("datetime") val datetime: DateTime,
+ @BeanProperty @JsonProperty("year") val year: Option[Int] = None,
+ @BeanProperty @JsonProperty("humoId") val humoId: Option[String] = None,
+ @BeanProperty @JsonProperty("humoUrl") val humoUrl: Option[String] = None,
+ @BeanProperty @JsonProperty("yeloId") val yeloId: Option[String] = None,
+ @BeanProperty @JsonProperty("yeloUrl") val yeloUrl: Option[String] = None,
+ @BeanProperty @JsonProperty("imdbId") val imdbId: Option[String] = None
+ ) {
+ @ObjectId
+ @Id
+ def getId = id;
+
+ def humanDate() = {
+ val format = DateTimeFormat.forPattern("dd MMM HH:mm")
+ format.print(datetime.withZone(Application.timezone))
+ }
+
+}
+
+object Broadcast {
+
+ private val logger = Logger("application.db")
+
+ private lazy val db = MongoDB.collection("broadcasts", classOf[Broadcast], classOf[String])
+
+ def create(broadcast: Broadcast) = {
+ val result = db.save(broadcast);
+ val id = result.getSavedId();
+ logger.debug("saved " + broadcast + " with id " + id)
+ db.findOneById(id)
+ }
+
+ def update(broadcast: Broadcast) {
+ val result = db.save(broadcast);
+ val id = result.getSavedId();
+ logger.debug("saved " + broadcast + " with id " + id)
+ }
+
+ def findAll() = { db.find().toArray.toList }
+
+ def findByInterval(interval:Interval) = {
+ db.find().greaterThan("datetime", interval.getStart).lessThan("datetime", interval.getEnd).toArray.toList
+ }
+
+ def findById(id: String) = Option(db.findOneById(id))
+
+ def findByDateTimeAndChannel(datetime: DateTime, channel: String) = db.find().is("datetime", datetime).is("channel", channel).headOption
+
+ def setYelo(b:Broadcast, yeloId:String, yeloUrl:String) {
+ logger.debug("Saving Yelo link for " + b.name + " with id " + b.id)
+ db.updateById(b.id, DBUpdate.set("yeloId", yeloId).set("yeloUrl", yeloUrl))
+ }
+
+ def setImdb(b:Broadcast, imdbId:String) {
+ logger.debug("Saving IMDB link for " + b.name + " with id " + b.id)
+ db.updateById(b.id, DBUpdate.set("imdbId", imdbId))
+ }
+}
View
@@ -1,27 +1,61 @@
package models
+import scala.collection.JavaConversions._
import play.api.Play.current
+import play.modules.mongodb.jackson.MongoDB
import net.vz.mongodb.jackson.{Id, ObjectId}
import org.codehaus.jackson.annotate.JsonProperty
-import play.modules.mongodb.jackson.MongoDB
import reflect.BeanProperty
-
-import scala.collection.JavaConversions._
+import com.mongodb.DBObject
+import play.api.Logger
class Movie(
@ObjectId @Id val id: String,
@BeanProperty @JsonProperty("name") val name: String,
@BeanProperty @JsonProperty("imdbId") val imdbId: String,
- @BeanProperty @JsonProperty("imdbRating") val imdbRating: String) {
+ @BeanProperty @JsonProperty("imdbRating") val imdbRating: String,
+ @BeanProperty @JsonProperty("year") val year: Int,
+ @BeanProperty @JsonProperty("imgUrl") val imgUrl: String) {
@ObjectId
@Id
def getId = id;
+
+ def imdbUrl() = "http://www.imdb.com/title/" + imdbId
}
object Movie {
+ private val logger = Logger("application.db")
+
private lazy val db = MongoDB.collection("movies", classOf[Movie], classOf[String])
- def create(movie: Movie) { db.save(movie) }
+ def create(movie: Movie) = {
+ val result = db.save(movie);
+ val id = result.getSavedId();
+ logger.debug("saved " + movie + " with id " + id)
+ db.findOneById(id)
+ }
+
+ def createIfDoesNotExist(movie: Movie) = {
+ val fromDB = Movie.findByNameAndYear(movie.name, movie.year)
+ fromDB.getOrElse{
+ Movie.create(movie)
+ movie
+ }
+ }
+
def findAll() = { db.find().toArray.toList }
+
+ def findById(id: String) = Option(db.findOneById(id))
+
+ def findByName(name: String) = db.find().is("name", name).headOption
+
+ def findByImdbId(imdbId: String) = db.find().is("imdbId", imdbId).headOption
+
+ def findByNameAndYear(name: String, year:Int) = {
+ val movie = db.find().is("name", name).is("year", year).headOption
+ val result = movie.orElse(findByName(name))
+ logger.debug("Search for %s %s gives %s".format(name, year, result))
+ result
+ }
}
View
@@ -1,37 +0,0 @@
-package models;
-
-import java.util.Date;
-
-import models.helper.TmdbMovie;
-
-public class Movie2{
-
- public String id;
-
- public String url;
- public String title;
- public Integer year;
- public String channel;
- public Date start;
- public Date end;
-
- public ImdbApiMovie imdb;
-
- public TmdbMovie tmdb;
-
-
- public int getAssembledPercentRating() {
- int rating = -1;
- if (imdb != null && imdb.rating != null) {
- rating = imdb.getPercentRating();
- } else if (tmdb != null && tmdb.rating != null) {
- rating = tmdb.getPercentRating();
- }
- return rating;
- }
-
- @Override
- public String toString() {
- return String.format("[%s] at %s", title, url);
- }
-}
@@ -0,0 +1,6 @@
+package models.helper
+
+import models.Broadcast
+import models.Movie
+
+case class BroadcastInfo(broadcast:Broadcast, movie:Option[Movie])
@@ -1,4 +1,4 @@
-package models;
+package models.helper;
import play.Logger;
@@ -1,12 +0,0 @@
-package services;
-
-import java.util.Comparator;
-
-import models.Movie2;
-
-public final class DescendingMovieRatingComparator implements Comparator<Movie2> {
- @Override
- public int compare(Movie2 o1, Movie2 o2) {
- return o2.getAssembledPercentRating() - o1.getAssembledPercentRating();
- }
-}
Oops, something went wrong.

0 comments on commit f2a5883

Please sign in to comment.