正式抽奖完整过程
➜ ~ amm
Loading...
Welcome to the Ammonite Repl 1.2.1
(Scala 2.12.6 Java 1.8.0_172)
If you like Ammonite, please support our development at
www.patreon.com/lihaoyi@ import $ivy.`com.lihaoyi::requests:0.1.4`
import $ivy.$
@ import $ivy.`org.json4s::json4s-jackson:3.5.2`
import $ivy.$
@ val url = "
https://www.v2ex.com/api/replies/show.json?topic_id=493356"
url: String = "
https://www.v2ex.com/api/replies/show.json?topic_id=493356"
@ val numberOfWinners = 6
numberOfWinners: Int = 6
@ val source = requests.get(url)
source: requests.Response = Response(
"
https://www.v2ex.com/api/replies/show.json?topic_id=493356",
200,
"OK",
Map(
"etag" -> Buffer("W/\"02f2dce98bcae83800ccd1568488d6f583c9d72d\""),
"server" -> Buffer("Galaxy/3.9.8.1"),
"x-rate-limit-reset" -> Buffer("1539086400"),
"cache-control" -> Buffer("max-age=3600"),
"google" -> Buffer("XY"),
"date" -> Buffer("Tue, 09 Oct 2018 11:41:36 GMT"),
"x-rate-limit-limit" -> Buffer("120"),
"x-rate-limit-remaining" -> Buffer("118"),
"content-type" -> Buffer("application/json;charset=UTF-8"),
"transfer-encoding" -> Buffer("chunked"),
"connection" -> Buffer("keep-alive"),
"x-frame-options" -> Buffer("SAMEORIGIN"),
"vary" -> Buffer("Accept-Encoding"),
"strict-transport-security" -> Buffer("max-age=31536000"),
"content-encoding" -> Buffer("gzip")
...
@ import org.json4s.jackson.JsonMethods.parse
import org.json4s.jackson.JsonMethods.parse
@ val json = parse(source.text)
json: org.json4s.package.JValue = JArray(
List(
JObject(
List(
(
"member",
JObject(
List(
("username", JString("cye3s")),
("website", JString("")),
("github", JString("")),
("psn", JString("")),
("avatar_normal", JString("//
cdn.v2ex.com/gravatar/041a96d84145b357e82a6e44628125f9?s=24&d=retro")),
("bio", JString("")),
("url", JString("
https://www.v2ex.com/u/cye3s")),
("tagline", JString("")),
("twitter", JString("")),
("created", JInt(1409614320)),
("avatar_large", JString("//
cdn.v2ex.com/gravatar/041a96d84145b357e82a6e44628125f9?s=24&d=retro")),
("avatar_mini", JString("//
cdn.v2ex.com/gravatar/041a96d84145b357e82a6e44628125f9?s=24&d=retro")),
...
@ case class Profile(username: String, github: String)
defined class Profile
@ case class Member(member: Profile)
defined class Member
@ val members = json.extract[List[Member]].map(_.member.username).distinct
cmd9.sc:1: No org.json4s.Formats found. Try to bring an instance of org.json4s.Formats in scope or use the org.json4s.DefaultFormats.
val members = json.extract[List[Member]].map(_.member.username).distinct
^
Compilation Failed
@ implicit val formats = DefaultFormats
cmd9.sc:1: not found: value DefaultFormats
implicit val formats = DefaultFormats
^
Compilation Failed
@ import org.json4s._
import org.json4s._
@ import org.json4s.JsonDSL._
import org.json4s.JsonDSL._
@ implicit val formats = DefaultFormats
formats: DefaultFormats.type = org.json4s.DefaultFormats$@58647985
@ val members = json.extract[List[Member]].map(_.member.username).distinct
members: List[String] = List(
"cye3s",
"CoderHugo",
"zsh1995",
"densuc",
"sadhen",
"hepin1989",
"isnowify",
"chenpipguge",
"yorksonchang",
"WeberXie",
"0vels",
"duoweimi222",
"linghutf",
"nakedou",
"zhijiansha",
"Applenice",
"vecZro",
"billlee",
"someonedeng",
...
@ import scala.util.Random
import scala.util.Random
@ Random.shuffle(members).take(numberOfWinners )
res14: List[String] = List("xcjx", "chocolatesir", "lnomve", "funky", "wenning", "magicsilence")
很幸运,只在 REPL 里面粘贴错两次