143 lines
3.6 KiB
Go
143 lines
3.6 KiB
Go
package crawler
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/maximotejeda/us_dop_scrapper/helpers"
|
|
"github.com/maximotejeda/us_dop_scrapper/internal/application/core/domain"
|
|
"github.com/maximotejeda/us_dop_scrapper/internal/ports"
|
|
"github.com/playwright-community/playwright-go"
|
|
)
|
|
|
|
type scotia struct {
|
|
client ports.DollarPort
|
|
}
|
|
|
|
func NewScotia(client ports.DollarPort) ports.APIPorts {
|
|
return &scotia{
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
func (sct scotia) Scrape(ctx context.Context, page playwright.Page, log *slog.Logger) (insts []*domain.History, err error) {
|
|
tout := 120000.00
|
|
uri := os.Getenv("SCOTIA")
|
|
log = log.With("scrapper", "scotia")
|
|
if _, err := page.Goto(uri, playwright.PageGotoOptions{
|
|
Timeout: &tout,
|
|
WaitUntil: playwright.WaitUntilStateLoad,
|
|
}); err != nil {
|
|
log.Error("could not get info", "error", err)
|
|
return nil, err
|
|
}
|
|
|
|
currencyTable := page.Locator(".bns--table")
|
|
currencyTable.WaitFor()
|
|
|
|
firstRow := page.Locator(".bns--table > tbody:nth-child(1) > tr:nth-child(2)")
|
|
secondRow := page.Locator(".bns--table > tbody:nth-child(1) > tr:nth-child(3)")
|
|
|
|
// the same institution has 2 prices for dollar sell
|
|
// first row is onsite selling dollars
|
|
// second row is online selling dollars
|
|
buyOnsite := firstRow.Locator("td:nth-child(3)")
|
|
sellOnsite := firstRow.Locator("td:nth-child(4)")
|
|
|
|
// the first row has 4 elements
|
|
// but the second row only has 3
|
|
buyOnline := secondRow.Locator("td:nth-child(2)")
|
|
sellOnline := secondRow.Locator("td:nth-child(3)")
|
|
|
|
compraOnsiteSTR, err := buyOnsite.InnerText()
|
|
if err != nil {
|
|
log.Error("could not get compra str", "err", err)
|
|
return nil, err
|
|
}
|
|
|
|
ventaOnsiteSTR, err := sellOnsite.InnerText()
|
|
if err != nil {
|
|
log.Error("could not get venta string", "err", err)
|
|
return nil, err
|
|
}
|
|
instOnsite := &domain.History{
|
|
Name: "scotiabank",
|
|
Parser: "scotia",
|
|
Parsed: time.Now().Unix(),
|
|
}
|
|
|
|
instOnsite.Venta = helpers.Normalize(ventaOnsiteSTR)
|
|
instOnsite.Compra = helpers.Normalize(compraOnsiteSTR)
|
|
if instOnsite.Compra == 0 || instOnsite.Venta == 0 {
|
|
return nil, fmt.Errorf("scotia: institution not parsed: %v", instOnsite)
|
|
}
|
|
|
|
compraOnlineSTR, err := buyOnline.InnerText()
|
|
if err != nil {
|
|
log.Error("could not get compra onlie str", "err", err)
|
|
return nil, err
|
|
}
|
|
|
|
ventaOnlineSTR, err := sellOnline.InnerText()
|
|
if err != nil {
|
|
log.Error("could not get venta online string", "err", err)
|
|
return nil, err
|
|
}
|
|
|
|
instOnline := &domain.History{
|
|
Name: "scotiabank cambio online",
|
|
Parser: "scotia",
|
|
Parsed: time.Now().Unix(),
|
|
}
|
|
instOnline.Venta = helpers.Normalize(ventaOnlineSTR)
|
|
instOnline.Compra = helpers.Normalize(compraOnlineSTR)
|
|
|
|
if instOnline.Compra == 0 || instOnline.Venta == 0 {
|
|
return nil, fmt.Errorf("scotia: institution not parsed: %v", instOnline)
|
|
}
|
|
|
|
insts = append(insts, instOnline, instOnsite)
|
|
return insts, nil
|
|
}
|
|
|
|
func (sct scotia) ExecParser(
|
|
ctx context.Context,
|
|
browser *playwright.Browser,
|
|
log *slog.Logger) (err error) {
|
|
t := true
|
|
ua := helpers.NewMobileUA()
|
|
b := *browser
|
|
page, err := b.NewPage(playwright.BrowserNewPageOptions{
|
|
UserAgent: &ua,
|
|
// IsMobile: &t,
|
|
HasTouch: &t,
|
|
Viewport: &playwright.Size{
|
|
Width: 412,
|
|
Height: 915,
|
|
},
|
|
Screen: &playwright.Size{
|
|
Width: 412,
|
|
Height: 915,
|
|
},
|
|
})
|
|
if err != nil {
|
|
log.Error("creating page", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, 6*time.Minute)
|
|
defer page.Close()
|
|
defer cancel()
|
|
insts, err := sct.Scrape(ctx, page, log)
|
|
// here we execute db operations
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, inst := range insts {
|
|
sct.client.NewHistory(inst)
|
|
}
|
|
return err
|
|
}
|