Created
July 8, 2015 10:42
-
-
Save mnd999/ac00b97709425cab21af to your computer and use it in GitHub Desktop.
Code to download bitcoin prices and persist with akka-http. Pretty horrible though, surely this can be done better?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package quandl | |
import scala.concurrent.Await | |
import scala.concurrent.Future | |
import scala.concurrent.duration.DurationInt | |
import org.joda.time.DateTime | |
import akka.actor.ActorSystem | |
import akka.http.scaladsl.Http | |
import akka.http.scaladsl.client.RequestBuilding | |
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport | |
import akka.http.scaladsl.unmarshalling.Unmarshal | |
import akka.stream.ActorFlowMaterializer | |
import spray.json.JsArray | |
import spray.json.JsNumber | |
import spray.json.JsString | |
import spray.json.JsValue | |
import spray.json.JsonFormat | |
import trading.Trade | |
import trading.TradeDao | |
object QuandlBitcoinPrice extends App with SprayJsonSupport { | |
implicit val system = ActorSystem() | |
implicit val materializer = ActorFlowMaterializer() | |
import system.dispatcher // implicit execution context | |
val prices = getPrices() | |
val dao = TradeDao("quandl") | |
dao.mongoDb.drop() | |
Await.result(prices.map { ps => ps.map { p => new Trade(p.weightedPrice, p.volumeBtc, "BTC/USD", DateTime.parse(p.date + "T12:00:00").toDate()) }.foreach { trade => dao.insert(trade) } }, 5 seconds) | |
System.exit(0) | |
def getPrices(): Future[Seq[Price]] = { | |
object PriceJsonProtocol extends spray.json.DefaultJsonProtocol { | |
implicit object PriceJsonFormat extends JsonFormat[Price] { | |
def write(p: Price) = | |
JsArray(JsString(p.date), JsNumber(p.open), JsNumber(p.high), JsNumber(p.low), JsNumber(p.close), JsNumber(p.volumeBtc), JsNumber(p.volumeCcy), JsNumber(p.weightedPrice)) | |
def read(value: JsValue) = value match { | |
case JsArray(Vector(JsString(date), JsNumber(open), JsNumber(high), JsNumber(low), JsNumber(close), JsNumber(volumeBtc), JsNumber(volumeCcy), JsNumber(weightedPrice))) => | |
new Price(date, open, high, low, close, volumeBtc, volumeCcy, weightedPrice) | |
case _ => null | |
} | |
} | |
} | |
import PriceJsonProtocol._ | |
val jsonResponse: Future[JsValue] = Http(system).singleRequest(RequestBuilding.Get("https://www.quandl.com/api/v1/datasets/BITCOIN/BITSTAMPUSD")) | |
.flatMap(resp => Unmarshal(resp.entity).to[JsValue]) | |
jsonResponse.map { response => | |
response.asJsObject.getFields("data") match { | |
case Vector(JsArray(vec)) => vec.map { price => price.convertTo[Price] } | |
} | |
} | |
} | |
case class Price(date: String, open: BigDecimal, high: BigDecimal, low: BigDecimal, close: BigDecimal, volumeBtc: BigDecimal, volumeCcy: BigDecimal, weightedPrice: BigDecimal) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment