push new streams to the homepage, log streaming api errors

This commit is contained in:
Thibault Duplessis 2014-05-07 00:27:48 +02:00
parent 000086d9b3
commit dba35ecedf
8 changed files with 41 additions and 7 deletions

View file

@ -35,5 +35,7 @@ private[app] final class Renderer extends Actor {
case lila.puzzle.RenderDaily(puzzle, fen, lastMove) =>
sender ! V.puzzle.daily(puzzle, fen, lastMove)
case lila.tv.StreamsOnAir(streams) => sender ! V.tv.streamsOnAir(streams)
}
}

View file

@ -31,9 +31,7 @@
}
</div>
}
@streams.map { s =>
<a class="stream" href="@s.url"><span data-icon="1">&nbsp;LIVE!</span> @s.name</a>
}
<div id="streams_on_air">@tv.streamsOnAir(streams)</div>
<div id="timeline" data-href="@routes.Lobby.timeline">
@timeline.entries(userTimeline)
<div class="links clearfix">

View file

@ -0,0 +1,4 @@
@(ss: List[lila.tv.StreamOnAir])
@ss.map { s =>
<a class="stream" href="@s.url"><span data-icon="1">&nbsp;LIVE!</span> @s.name</a>
}

View file

@ -25,6 +25,7 @@ sealed abstract class RemindDeploy(val key: String)
case object RemindDeployPre extends RemindDeploy("deployPre")
case object RemindDeployPost extends RemindDeploy("deployPost")
case class Deploy(event: RemindDeploy, html: String)
case class StreamsOnAir(html: String)
package map {
case class Get(id: String)

View file

@ -24,7 +24,7 @@ private[lobby] final class Socket(
router: akka.actor.ActorSelection,
uidTtl: Duration) extends SocketActor[Member](uidTtl) with Historical[Member] {
context.system.lilaBus.subscribe(self, 'changeFeaturedGame)
context.system.lilaBus.subscribe(self, 'changeFeaturedGame, 'streams)
def receiveSpecific = {
@ -67,6 +67,8 @@ private[lobby] final class Socket(
}
case HookIds(ids) => notifyVersion("hook_list", ids)
case lila.hub.actorApi.StreamsOnAir(html) => notifyAll(makeMessage("streams", html))
}
private def playerUrl(fullId: String) =

View file

@ -24,6 +24,7 @@ final class Env(
private lazy val streaming = new Streaming(
system = system,
isOnline = isOnline,
renderer = hub.actor.renderer,
ustreamApiKey = UstreamApiKey)
def streamsOnAir = streaming.onAir

View file

@ -7,6 +7,7 @@ import play.api.libs.ws.WS
private final class Streaming(
system: ActorSystem,
ustreamApiKey: String,
renderer: ActorSelection,
isOnline: String => Boolean) {
import Streaming._
@ -31,17 +32,39 @@ private final class Streaming(
val twitch = WS.url("https://api.twitch.tv/kraken/search/streams")
.withQueryString("q" -> "lichess.org")
.withHeaders("Accept" -> "application/vnd.twitchtv.v2+json")
.get().map { _.json.asOpt[Twitch.Result] ?? (_.streamsOnAir take max) }
.get().map {
_.json.asOpt[Twitch.Result] match {
case Some(data) => data.streamsOnAir take max
case None =>
logger.warn(s"twitch {res.status} {~res.body.lines.toList.headOption}")
Nil
}
}
val chesswhiz = isOnline("chesswhiz") ??
WS.url(s"http://api.ustream.tv/json/stream/recent/search/title:like:chesswhiz")
.withQueryString("key" -> ustreamApiKey)
.get().map { _.json.asOpt[Ustream.Result] ?? (_.streamsOnAir take max) }
.get().map {
_.json.asOpt[Ustream.Result] match {
case Some(data) => data.streamsOnAir take max
case None =>
logger.warn(s"chesswhiz {res.status} {~res.body.lines.toList.headOption}")
Nil
}
}
twitch |+| chesswhiz map StreamsOnAir.apply pipeTo self
case StreamsOnAir(streams) => onAir = streams
case event@StreamsOnAir(streams) if onAir != streams =>
onAir = streams
import makeTimeout.short
renderer ? event foreach {
case html: play.api.templates.Html =>
context.system.lilaBus.publish(lila.hub.actorApi.StreamsOnAir(html.body), 'stream)
}
}
}))
private def logger = play.api.Logger("tv.streaming")
actor ! Search
}

View file

@ -2310,6 +2310,9 @@ var storage = {
}
});
},
streams: function(html) {
$('#streams_on_air').html(html);
},
hook_add: function(hook) {
addHook(hook);
drawHooks();