70 lines
1.7 KiB
Go
70 lines
1.7 KiB
Go
package crawler
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"git.maximotejeda.com/maximo/us-dop-scrapper/config"
|
|
"git.maximotejeda.com/maximo/us-dop-scrapper/helpers"
|
|
"git.maximotejeda.com/maximo/us-dop-scrapper/internal/application/core/domain"
|
|
"git.maximotejeda.com/maximo/us-dop-scrapper/internal/ports"
|
|
"github.com/playwright-community/playwright-go"
|
|
)
|
|
|
|
type brd struct{}
|
|
|
|
func NewBRD() ports.APIPorts {
|
|
return &brd{}
|
|
}
|
|
func (bd brd) Scrape(ctx context.Context, page playwright.Page, log *slog.Logger) (insts []*domain.History, err error) {
|
|
tout := 120000.00
|
|
log = log.With("scrapper", "brd")
|
|
if _, err := page.Goto(config.GetBRDURL(), playwright.PageGotoOptions{
|
|
Timeout: &tout,
|
|
WaitUntil: playwright.WaitUntilStateLoad,
|
|
}); err != nil {
|
|
log.Error("could not get info", "error", err)
|
|
return nil, err
|
|
}
|
|
|
|
err = page.WaitForLoadState()
|
|
if err != nil {
|
|
log.Error("waiting for page state", "err", err)
|
|
return nil, err
|
|
}
|
|
|
|
_ = page.Locator("section#divisas").WaitFor()
|
|
compraLocator := page.Locator("span#compraUS")
|
|
_ = compraLocator.WaitFor()
|
|
ventaLocator := page.Locator("span#ventaUS")
|
|
compraSTR, err := compraLocator.TextContent()
|
|
if err != nil {
|
|
log.Error("parsing compra", "err", err)
|
|
return nil, err
|
|
}
|
|
ventaSTR, err := ventaLocator.TextContent()
|
|
if err != nil {
|
|
log.Error("parsing compra", "err", err)
|
|
return nil, err
|
|
}
|
|
inst := &domain.History{
|
|
Name: "banreservas",
|
|
Parser: "brd",
|
|
Parsed: time.Now().Unix(),
|
|
}
|
|
|
|
compra := helpers.Normalize(compraSTR)
|
|
venta := helpers.Normalize(ventaSTR)
|
|
|
|
inst.Compra = compra
|
|
inst.Venta = venta
|
|
|
|
if inst.Compra == 0 || inst.Venta == 0 {
|
|
return nil, fmt.Errorf("brd: institution not parsed")
|
|
}
|
|
insts = append(insts, inst)
|
|
return insts, nil
|
|
}
|