This commit is contained in:
parent
ae0a07ae1b
commit
2fa14a7797
3 changed files with 156 additions and 22 deletions
|
|
@ -41,7 +41,7 @@ func (s *Parser) getPrice(ctx context.Context, task shared.Task) (int32, error)
|
|||
return minimal, nil
|
||||
}
|
||||
|
||||
func (s *Parser) getMinimalPrice(task shared.Task) (int32, error) {
|
||||
func (s *Parser) getMinimalPrice(task shared.Task) int32 {
|
||||
ctx := context.Background()
|
||||
allocCtx, allocCancel := chromedp.NewRemoteAllocator(ctx, s.externalBrowser)
|
||||
defer allocCancel()
|
||||
|
|
@ -52,7 +52,6 @@ func (s *Parser) getMinimalPrice(task shared.Task) (int32, error) {
|
|||
var (
|
||||
singlePrice string
|
||||
rangedPrice string
|
||||
prices []int32
|
||||
)
|
||||
|
||||
if err := chromedp.Run(sessionCtx,
|
||||
|
|
@ -61,26 +60,51 @@ func (s *Parser) getMinimalPrice(task shared.Task) (int32, error) {
|
|||
chromedp.Evaluate(`(document.querySelector('div.price')?.innerText || '').trim()`, &singlePrice),
|
||||
chromedp.Evaluate(`(document.querySelector('div.price_range')?.innerText || '').trim()`, &rangedPrice),
|
||||
); err != nil {
|
||||
return zeroPrice, err
|
||||
return zeroPrice
|
||||
}
|
||||
|
||||
minimal := s.processPrices(singlePrice, rangedPrice)
|
||||
log.Infof(logHeader+"uuid: %s, price: %d", task.MerchUuid, minimal)
|
||||
return minimal
|
||||
}
|
||||
|
||||
func (s *Parser) processPrices(singlePrice, rangedPrice string) int32 {
|
||||
var prices []int32
|
||||
|
||||
//in case of any errors or no price return zeroPrice const
|
||||
//if success add to prices slice
|
||||
if singlePrice != "" {
|
||||
singlePrice = strings.TrimSpace(singlePrice)
|
||||
prices = append(prices, s.getSinglePriceWithTax(singlePrice))
|
||||
counted, err := s.parseSinglePrice(singlePrice)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"err": err.Error(),
|
||||
"singlePrice": singlePrice,
|
||||
}).Error(logHeader + logGetPrice + "failed to parse single price, returning zero price")
|
||||
return zeroPrice
|
||||
}
|
||||
prices = append(prices, counted)
|
||||
} else {
|
||||
return zeroPrice, nil //return zero price immediately
|
||||
log.Warn(logHeader + logGetPrice + "single price not found")
|
||||
return zeroPrice
|
||||
}
|
||||
|
||||
//optional, adds price only if no errors and has non zero value
|
||||
if rangedPrice != "" {
|
||||
rangedPrice = strings.TrimSpace(rangedPrice)
|
||||
prices = append(prices, s.getMinimalPriceFromRangeWithTax(rangedPrice))
|
||||
//no return because of optional ranged price
|
||||
counted, err := s.parseRangedPrice(rangedPrice)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"err": err.Error(),
|
||||
"rangedPrice": rangedPrice,
|
||||
}).Error(logHeader + logGetPrice + "failed to parse ranged price")
|
||||
} else {
|
||||
if counted > 0 {
|
||||
prices = append(prices, counted)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
minimal := slices.Min(prices)
|
||||
log.Infof(logHeader+"uuid: %s, price: %d", task.MerchUuid, minimal)
|
||||
|
||||
return minimal, nil
|
||||
return slices.Min(prices)
|
||||
}
|
||||
|
||||
func (s *Parser) getSinglePriceWithTax(rawPrice string) int32 {
|
||||
|
|
@ -117,3 +141,34 @@ func (s *Parser) getMinimalPriceFromRangeWithTax(priceRange string) int32 {
|
|||
|
||||
return int32(float64(price) * taxMultiplier)
|
||||
}
|
||||
|
||||
func (s *Parser) parseSinglePrice(rawPrice string) (int32, error) {
|
||||
deCommaStr := strings.ReplaceAll(rawPrice, ",", "")
|
||||
split := strings.Split(deCommaStr, "円")
|
||||
finalPrice, err := s.countTax(split[0])
|
||||
if err != nil {
|
||||
return zeroPrice, err
|
||||
}
|
||||
return finalPrice, nil
|
||||
}
|
||||
|
||||
func (s *Parser) parseRangedPrice(rawPrice string) (int32, error) {
|
||||
deCommaStr := strings.ReplaceAll(rawPrice, ",", "")
|
||||
split := strings.Split(deCommaStr, "円")
|
||||
rm1 := strings.ReplaceAll(split[0], "(", "")
|
||||
rm2 := strings.ReplaceAll(rm1, "他", "")
|
||||
|
||||
finalPrice, err := s.countTax(rm2)
|
||||
if err != nil {
|
||||
return zeroPrice, err
|
||||
}
|
||||
return finalPrice, nil
|
||||
}
|
||||
|
||||
func (s *Parser) countTax(priceStr string) (int32, error) {
|
||||
intPrice, err := strconv.Atoi(priceStr)
|
||||
if err != nil {
|
||||
return zeroPrice, err
|
||||
}
|
||||
return int32(float64(intPrice) * taxMultiplier), nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue