This commit is contained in:
parent
784a1a74ff
commit
ae0a07ae1b
2 changed files with 47 additions and 3 deletions
|
|
@ -37,10 +37,12 @@ func (s *Parser) worker(ctx context.Context, receiver chan shared.Task, sender c
|
||||||
for task := range receiver {
|
for task := range receiver {
|
||||||
log.WithField("task_uuid", task.MerchUuid).Debug(logHeader + logWorker + "processing task")
|
log.WithField("task_uuid", task.MerchUuid).Debug(logHeader + logWorker + "processing task")
|
||||||
|
|
||||||
pageCtx, pageCancel := chromedp.NewContext(ctx, chromedp.WithLogf(func(string, ...any) {}))
|
//pageCtx, pageCancel := chromedp.NewContext(ctx, chromedp.WithLogf(func(string, ...any) {}))
|
||||||
|
//
|
||||||
|
//price, err := s.getPrice(pageCtx, task)
|
||||||
|
//pageCancel()
|
||||||
|
|
||||||
price, err := s.getPrice(pageCtx, task)
|
price, err := s.getMinimalPrice(task)
|
||||||
pageCancel()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithField("task_uuid", task.MerchUuid).Warn(logHeader + logWorker + logTaskWarning + "failed to process, zero price")
|
log.WithField("task_uuid", task.MerchUuid).Warn(logHeader + logWorker + logTaskWarning + "failed to process, zero price")
|
||||||
sender <- shared.TaskResult{
|
sender <- shared.TaskResult{
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,48 @@ func (s *Parser) getPrice(ctx context.Context, task shared.Task) (int32, error)
|
||||||
return minimal, nil
|
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 {
|
func (s *Parser) getSinglePriceWithTax(rawPrice string) int32 {
|
||||||
re := regexp.MustCompile(`(\d+)\s*円`)
|
re := regexp.MustCompile(`(\d+)\s*円`)
|
||||||
matches := re.FindStringSubmatch(rawPrice)
|
matches := re.FindStringSubmatch(rawPrice)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue