Created
December 11, 2018 16:54
-
-
Save stk132/6a46a5dc097624b5645ff9086aad0b11 to your computer and use it in GitHub Desktop.
block.fmのトラックリストを取得するサンプル
This file contains 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
import Dependencies._ | |
lazy val root = (project in file(".")). | |
settings( | |
inThisBuild(List( | |
organization := "com.example", | |
scalaVersion := "2.12.5", | |
version := "0.1.0-SNAPSHOT" | |
)), | |
name := "Hello", | |
libraryDependencies ++= Seq( | |
scalaTest % Test, | |
"com.softwaremill.sttp" %% "core" % "1.4.2", | |
"com.softwaremill.sttp" %% "akka-http-backend" % "1.4.2", | |
"com.typesafe.akka" %% "akka-stream" % "2.5.11", | |
"io.circe" %% "circe-core" % "0.10.0", | |
"io.circe" %% "circe-generic" % "0.10.0", | |
"io.circe" %% "circe-parser" % "0.10.0" | |
) | |
) |
This file contains 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 example | |
import com.softwaremill.sttp._ | |
import com.softwaremill.sttp.akkahttp._ | |
import io.circe._ | |
import io.circe.parser._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
object Main extends App { | |
implicit val backend = AkkaHttpBackend() | |
//チャンネル一覧の取得 | |
val responseFuture = sttp.get(uri"https://newapi.block.fm/user/v1/resource_collections/9").send() | |
for { | |
response <- responseFuture //Futureの結果を受け取る | |
if response.isSuccess //successじゃなかったらここでストップ | |
channelBody <- response.body //EitherのLeftだったらここでストップ | |
channelJsonTmp = parse(channelBody) //responseBodyのjson文字列をcirceでparse | |
channelJson <- channelJsonTmp //parse失敗だったらここでストップ | |
cursor = channelJson.hcursor | |
//circeでjsonから必要な情報を引っ張ってSeq[Json]としてデコード | |
soundSources = cursor.downField("data").downField("resource_collection").downField("sound_sources").as[Seq[Json]] | |
soundSrcList <- soundSources //decode失敗ならここでストップ | |
//番組タイトルが「radio REBOOT」から始まるJsonを探す(Option[Json]を返す) | |
targetRadioTmp = soundSrcList.find(p => p.hcursor.downField("title").as[String].right.get.startsWith("radio REBOOT")) | |
targetRadio <- targetRadioTmp //OptionがNoneならここでストップ | |
//目的の番組に対応するJsonからIdを参照して、Longとしてデコード | |
targetRadioIdTmp = targetRadio.hcursor.downField("id").as[Long] | |
targetRadioId <- targetRadioIdTmp //デコード失敗ならここでストップ | |
//番組のIdを使ってトラックリスト取得のURLを組み立てる | |
trackListUrl = s"https://newapi.block.fm/user/v1/sound_sources/$targetRadioId/timeline?type=track&per=9999" | |
//トラックリスト取得APIにアクセス | |
trackListResponseFuture = sttp.get(uri"$trackListUrl").send() | |
//Futureの結果取得 | |
trackListResponse <- trackListResponseFuture | |
if trackListResponse.isSuccess //アクセス結果がsuccessでなければここでストップ | |
trackListBody <- trackListResponse.body //EitherがLeftであればここでストップ | |
trackListJsonTmp = parse(trackListBody) //トラックリストのresponseBodyをparse | |
trackListJson <- trackListJsonTmp //parse失敗ならここでストップ | |
//トラックリストに相当する情報をSeq[Json]としてデコード | |
trackListResult = trackListJson.hcursor.downField("data").downField("timeline").as[Seq[Json]] | |
trackList <- trackListResult //デコード失敗ならここでストップ | |
} { | |
//トラックリストのjsonを標準出力 | |
println(trackList) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment