Skip to content

Instantly share code, notes, and snippets.

@rfvgyhn
Created August 23, 2020 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rfvgyhn/9371c737518abd2b3487c7438a470efc to your computer and use it in GitHub Desktop.
Save rfvgyhn/9371c737518abd2b3487c7438a470efc to your computer and use it in GitHub Desktop.
#r "nuget: FSharp.Data"
open FSharp.Data
open System
open System.Collections.Generic
open System.Linq
module Async =
let ParallelThrottle throttle tasks = Async.Parallel(tasks, throttle)
module String =
let Join (separator:string) (strings:IEnumerable<'a>) = String.Join(separator, strings)
let buildUrl path = sprintf "https://vault.racerxonline.com%s" path
let fetchRidersByName (location: string) letter =
async {
let url = buildUrl <| sprintf "/riders/%c" letter
try
let! doc = HtmlDocument.AsyncLoad(url)
return
doc.CssSelect(".listing li")
|> Seq.filter (fun (htmlNode) -> htmlNode.InnerText().Contains(location, StringComparison.OrdinalIgnoreCase))
|> Seq.map (fun (htmlNode) ->
let anchor = htmlNode.CssSelect("a").Single()
let name = anchor.InnerText().Split(" ")
anchor.TryGetAttribute("href")
|> Option.map (fun href -> ((name.First(), name.Last()), buildUrl <| href.Value().Replace("/points", "/races"))))
|> Seq.choose id
with
| :? System.Net.WebException as e -> return Seq.empty
}
let fetchRiders location =
"abcdefghijklmnopqrstuvwxyz".ToCharArray()
|> Array.map (fetchRidersByName location)
|> Async.ParallelThrottle 3
|> Async.RunSynchronously
|> Seq.concat
fetchRiders "Japan"
|> Seq.sortBy (fun r -> fst r |> snd, fst r |> fst)
|> Seq.map (fun r -> sprintf "* [%s %s](%s)" (fst r |> fst) (fst r |> snd) (snd r))
|> String.Join Environment.NewLine
|> printfn "%s"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment