This commit is contained in:
parent
d63d345a9b
commit
e48160dfa3
8 changed files with 243 additions and 390 deletions
90
internal/parsers/mandarake/service.go
Normal file
90
internal/parsers/mandarake/service.go
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
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
|
||||
)
|
||||
|
||||
//get single price
|
||||
if err := chromedp.Run(ctx,
|
||||
chromedp.Navigate(task.Link),
|
||||
chromedp.WaitReady("body"),
|
||||
chromedp.WaitVisible(`div.price`, chromedp.ByQuery),
|
||||
chromedp.Text(`div.price`, &singlePrice, chromedp.ByQuery),
|
||||
); 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))
|
||||
|
||||
//get price range
|
||||
if err := chromedp.Run(ctx,
|
||||
chromedp.Navigate(task.Link),
|
||||
chromedp.WaitReady("body"),
|
||||
chromedp.WaitVisible(`price_range`, chromedp.ByQuery),
|
||||
chromedp.Text(`price_range`, &rangedPrice, chromedp.ByQuery),
|
||||
); err != nil {
|
||||
log.WithError(err).Warn(logHeader + logGetPrice + "failed to get ranged price tag")
|
||||
}
|
||||
|
||||
rangedPrice = strings.TrimSpace(rangedPrice)
|
||||
|
||||
if rangedPrice != "" {
|
||||
prices = append(prices, s.getMinimalPriceFromRangeWithTax(rangedPrice))
|
||||
}
|
||||
|
||||
//get minimal 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)
|
||||
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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue