maximo tejeda baa2d658af
All checks were successful
dev test / test (push) Successful in 18s
dev test / vulnCheck (push) Successful in 13s
dev test / Ci-Lint (push) Successful in 38s
change repo name
2024-12-02 22:56:57 -04:00

66 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 bcd struct{}
func NewBCD() ports.APIPorts {
return &bcd{}
}
func (b bcd) Scrape(ctx context.Context, page playwright.Page, log *slog.Logger) (insts []*domain.History, err error) {
log = log.With("scrapper", "bcd")
tout := 90000.00
if _, err = page.Goto(config.GetBCDURL(), playwright.PageGotoOptions{
Timeout: &tout,
WaitUntil: playwright.WaitUntilStateLoad,
}); err != nil {
log.Error("could not get info", "error", err)
return nil, err
}
compraLocator := page.Locator("span#actualPurchaseValue")
_ = compraLocator.WaitFor(playwright.LocatorWaitForOptions{
Timeout: &tout,
State: playwright.WaitForSelectorStateVisible,
})
ventaLocator := page.Locator("span#actualSellingValue")
compra, err := compraLocator.TextContent()
if err != nil {
log.Error("locating compra", "err", err)
return nil, err
}
venta, err := ventaLocator.TextContent()
if err != nil {
log.Error("locating venta", "err", err)
return nil, err
}
inst := &domain.History{
Parser: "bcd",
Name: "banco central dominicano",
Parsed: time.Now().Unix(),
}
inst.Compra = helpers.Normalize(compra)
inst.Venta = helpers.Normalize(venta)
if inst.Compra == 0 || inst.Venta == 0 {
return nil, fmt.Errorf("bcd: institution not parsed compra or venta cant be 0")
}
return []*domain.History{inst}, nil
}