2025-12-26 16:19:09 +03:00
|
|
|
package mandarake
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/chromedp/chromedp"
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
"regexp"
|
|
|
|
|
"slices"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"task-processor/internal/shared"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (s *Parser) getPrice(ctx context.Context, task shared.Task) (int32, error) {
|
|
|
|
|
var (
|
|
|
|
|
singlePrice string
|
|
|
|
|
rangedPrice string
|
|
|
|
|
prices []int32
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err := chromedp.Run(ctx,
|
|
|
|
|
chromedp.Navigate(task.Link),
|
|
|
|
|
chromedp.WaitReady("body"),
|
2025-12-26 16:56:40 +03:00
|
|
|
chromedp.Text(`div.price`, &singlePrice, chromedp.ByQuery, chromedp.AtLeast(0)),
|
|
|
|
|
chromedp.Text(`div.price_range`, &rangedPrice, chromedp.ByQuery, chromedp.AtLeast(0)),
|
2025-12-26 16:19:09 +03:00
|
|
|
); err != nil {
|
|
|
|
|
log.WithError(err).Error(logHeader + logGetPrice + "failed to get single price tag")
|
|
|
|
|
return zeroPrice, err
|
|
|
|
|
}
|
|
|
|
|
singlePrice = strings.TrimSpace(singlePrice)
|
|
|
|
|
prices = append(prices, s.getSinglePriceWithTax(singlePrice))
|
|
|
|
|
|
|
|
|
|
rangedPrice = strings.TrimSpace(rangedPrice)
|
|
|
|
|
if rangedPrice != "" {
|
|
|
|
|
prices = append(prices, s.getMinimalPriceFromRangeWithTax(rangedPrice))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
minimal := slices.Min(prices)
|
|
|
|
|
log.Infof(logHeader+"uuid: %s, price: %d", task.MerchUuid, minimal)
|
|
|
|
|
|
2026-01-04 15:19:21 +03:00
|
|
|
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)
|
|
|
|
|
|
2025-12-26 16:19:09 +03:00
|
|
|
return minimal, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Parser) getSinglePriceWithTax(rawPrice string) int32 {
|
|
|
|
|
re := regexp.MustCompile(`(\d+)\s*円`)
|
|
|
|
|
matches := re.FindStringSubmatch(rawPrice)
|
|
|
|
|
if len(matches) < 2 {
|
|
|
|
|
log.Error("Mandarake | Single price not found, returning zero price")
|
|
|
|
|
return zeroPrice
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
priceStr := matches[1]
|
|
|
|
|
price, err := strconv.Atoi(priceStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error("Mandarake | Failed to convert single price, returning zero price")
|
|
|
|
|
return zeroPrice
|
|
|
|
|
}
|
|
|
|
|
return int32(price)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Parser) getMinimalPriceFromRangeWithTax(priceRange string) int32 {
|
|
|
|
|
re := regexp.MustCompile(`他([\d,]+)円`)
|
|
|
|
|
matches := re.FindStringSubmatch(priceRange)
|
|
|
|
|
if len(matches) < 2 {
|
|
|
|
|
log.Error("Price not found in range, returning zero price")
|
|
|
|
|
return zeroPrice
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
priceStr := strings.ReplaceAll(matches[1], ",", "")
|
|
|
|
|
price, err := strconv.Atoi(priceStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error("Failed to convert minimal price in range, returning zero price")
|
|
|
|
|
return zeroPrice
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return int32(float64(price) * taxMultiplier)
|
|
|
|
|
}
|