This commit is contained in:
parent
784a1a74ff
commit
ae0a07ae1b
2 changed files with 47 additions and 3 deletions
|
|
@ -41,6 +41,48 @@ func (s *Parser) getPrice(ctx context.Context, task shared.Task) (int32, error)
|
|||
return minimal, nil
|
||||
}
|
||||
|
||||
func (s *Parser) getMinimalPrice(task shared.Task) (int32, error) {
|
||||
ctx := context.Background()
|
||||
allocCtx, allocCancel := chromedp.NewRemoteAllocator(ctx, s.externalBrowser)
|
||||
defer allocCancel()
|
||||
|
||||
sessionCtx, sessionCancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf))
|
||||
defer sessionCancel()
|
||||
|
||||
var (
|
||||
singlePrice string
|
||||
rangedPrice string
|
||||
prices []int32
|
||||
)
|
||||
|
||||
if err := chromedp.Run(sessionCtx,
|
||||
chromedp.Navigate(task.Link),
|
||||
chromedp.WaitVisible("body", chromedp.ByQuery),
|
||||
chromedp.Evaluate(`(document.querySelector('div.price')?.innerText || '').trim()`, &singlePrice),
|
||||
chromedp.Evaluate(`(document.querySelector('div.price_range')?.innerText || '').trim()`, &rangedPrice),
|
||||
); err != nil {
|
||||
return zeroPrice, err
|
||||
}
|
||||
|
||||
if singlePrice != "" {
|
||||
singlePrice = strings.TrimSpace(singlePrice)
|
||||
prices = append(prices, s.getSinglePriceWithTax(singlePrice))
|
||||
} else {
|
||||
return zeroPrice, nil //return zero price immediately
|
||||
}
|
||||
|
||||
if rangedPrice != "" {
|
||||
rangedPrice = strings.TrimSpace(rangedPrice)
|
||||
prices = append(prices, s.getMinimalPriceFromRangeWithTax(rangedPrice))
|
||||
//no return because of optional ranged price
|
||||
}
|
||||
|
||||
minimal := slices.Min(prices)
|
||||
log.Infof(logHeader+"uuid: %s, price: %d", task.MerchUuid, minimal)
|
||||
|
||||
return minimal, nil
|
||||
}
|
||||
|
||||
func (s *Parser) getSinglePriceWithTax(rawPrice string) int32 {
|
||||
re := regexp.MustCompile(`(\d+)\s*円`)
|
||||
matches := re.FindStringSubmatch(rawPrice)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue