Last active
March 17, 2022 13:10
-
-
Save leandromoh/e99fd3d2fdf75aff41edd6ace40533dc to your computer and use it in GitHub Desktop.
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
open System | |
open PuppeteerSharp | |
open System.Threading.Tasks | |
open System.Text.Json | |
type Position = { Ticker: string; Qtd: int } | |
let getTaskResult (task: Task<'T>) = task.GetAwaiter().GetResult() | |
let positions = [ | |
{ Ticker ="XPTO"; Qtd = 1 } | |
] | |
let getBrowser() = task { | |
let! _ = BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultChromiumRevision) | |
return! Puppeteer.LaunchAsync(LaunchOptions(Headless=false)) | |
} | |
let browser = getBrowser() |> getTaskResult | |
let find url script = task { | |
let! page = browser.NewPageAsync() | |
page.DefaultTimeout <- 0 | |
let! _ = page.GoToAsync url | |
return! page.EvaluateFunctionAsync<string[]> script | |
} | |
let crawler pos = task { | |
let url = $"https://fiis.com.br/{pos.Ticker}11/" | |
let script = "() => [...document.querySelectorAll('#last-revenues--table tbody tr:first-child td')].map(x => x.innerHTML)" | |
let! values = find url script | |
let mes = values.[1].Split("/").[1] |> int | |
let dy = values.[3].Replace("%", String.Empty).Replace(",",".") |> decimal | |
let div = values.[4].Replace("R$", String.Empty).Replace(",",".") |> decimal | |
return {| | |
Ticker = pos.Ticker | |
Qtd = pos.Qtd | |
Mes = mes | |
Div = div | |
Dy = dy | |
Total = div * decimal pos.Qtd | |
|} | |
} | |
let mainAsync _ = task { | |
let! situations = positions |> Seq.map crawler |> Task.WhenAll | |
situations |> Seq.iter (JsonSerializer.Serialize >> printfn "%s") | |
situations |> Seq.sumBy (fun x -> x.Total) |> printfn "%A" | |
situations | |
|> Seq.fold (fun (a, b) x -> (x.Dy * decimal x.Qtd) + a, b + decimal x.Qtd) (0M, 0M) | |
|> (fun (a, b) -> a / b) | |
|> printfn "%A" | |
do! browser.DisposeAsync() | |
let _ = Console.ReadKey() | |
return 0 | |
} | |
[<EntryPoint>] | |
let main argv = | |
argv |> mainAsync |> getTaskResult | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment