Skip to content

Instantly share code, notes, and snippets.

@cronokirby
Created February 21, 2017 14:11
Show Gist options
  • Save cronokirby/a96a6f9c5e708729ad331a3d070ac855 to your computer and use it in GitHub Desktop.
Save cronokirby/a96a6f9c5e708729ad331a3d070ac855 to your computer and use it in GitHub Desktop.
data Category = Category { catName :: String
, leaderboard :: Url} deriving (Show)
instance FromJSON Category where
parseJSON (Object o) = Category
<$> (o .: "name")
<*> (last <$> o .: "links" >>= (.: "uri"))
data RunData = RunData { runs :: [Run]} deriving (Show)
instance FromJSON RunData where
parseJSON (Object o) = RunData
<$> ((o .: "data") >>= (.: "runs"))
type Seconds = Int
data Run = Run { playerName :: Maybe String, userLink :: Url
, time :: Seconds, video :: Url} deriving (Show)
instance FromJSON Run where
parseJSON (Object o) = Run
<$> (player >>= (.:? "name"))
<*> (player >>= (.: "uri"))
<*> (run >>= (.: "times") >>= (.: "primary_t"))
<*> ((head <$> (run >>= (.: "videos") >>= (.: "links")))
>>= (.: "uri"))
where
run = o .: "run"
player = head <$> (o .: "run" >>= (.: "players"))
parseRun :: Int -> Json -> IO (Either String Run)
parseRun place json = do
runData <- (eitherDecode <$> json) :: IO (Either String RunData)
return $ runData >>= \runData -> return $ runs runData !! place
fn parse_run(json: &Json) -> Run {
let data = json.as_object().unwrap()
.get("run").unwrap().as_object().unwrap();
let user_name = {
let player = data.get("players").unwrap().as_array().unwrap()
[0].as_object().unwrap();
let user_type = player.get("rel").unwrap().as_string().unwrap();
match user_type {
"user" => {
let url = player.get("uri").unwrap().as_string().unwrap();
fetch(url, &parse_name).unwrap()
},
_ => player.get("name").unwrap().as_string().unwrap().to_string()
}
};
let time = data.get("times").unwrap().as_object().unwrap()
.get("primary_t").unwrap();
let video = data.get("videos").and_then(|links|
links.as_object().unwrap()
.get("links").unwrap().as_array().unwrap()
[0].as_object().unwrap()
.get("uri").unwrap().as_string())
.map(|s| s.to_string());
Run{ user_name: user_name
, time: time.to_string()
, video: video }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment