Skip to content

Commit

Permalink
fix: PriceParser 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
cookienc committed Aug 31, 2024
1 parent 7782834 commit d473548
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import backend.itracker.crawl.exception.CrawlException
import backend.itracker.crawl.service.util.helper.DriverConnector
import backend.itracker.crawl.service.util.helper.PriceParser
import backend.itracker.crawl.service.util.helper.WebElementHelper
import backend.itracker.crawl.service.vo.DefaultPrice
import backend.itracker.crawl.service.vo.DefaultProduct
import io.github.oshai.kotlinlogging.KotlinLogging
import org.openqa.selenium.By
Expand Down Expand Up @@ -55,14 +56,18 @@ class Crawler(
}


val price = priceParser.getDefaultPrice(element)
if (DefaultPrice.none() == price) {
continue
}
products[productId] = DefaultProduct(
productId = productId.toLong(),
subCategory = helper.findClassName(
helper.findGrandParentElement(element),
"product-list-header__title"
),
name = element.text.split(System.lineSeparator())[0],
price = priceParser.getDefaultPrice(element),
price = price,
productLink = helper.findByTagAndAttribute(element, "a", "href"),
thumbnailLink = helper.findByTagAndAttribute(element, "img", "src")
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package backend.itracker.crawl.service.util.helper

import backend.itracker.crawl.service.vo.DefaultPrice
import backend.itracker.schedule.infra.notification.solapi.logger
import org.openqa.selenium.By
import org.openqa.selenium.WebElement
import org.springframework.stereotype.Component
Expand All @@ -21,15 +22,24 @@ class PriceParser(
var discountPercentage = 0
var basePrice = BigDecimal.ZERO
if (helper.hasClass(element, DISCOUNT_PERCENTAGE)) {
val discountPercentageElement = element.findElement(By.className(DISCOUNT_PERCENTAGE)).text
val basePriceElement = element.findElement(By.className(BASE_PRICE)).text
if (discountPercentageElement.isBlank() || basePriceElement.isBlank()) {
return DefaultPrice.none()
}
discountPercentage =
element.findElement(By.className(DISCOUNT_PERCENTAGE)).text.split(System.lineSeparator())[0]
.replace("%", "").toInt()
basePrice = element.findElement(By.className(BASE_PRICE)).text
discountPercentageElement.split(System.lineSeparator())[0]
.replace("%", "").toIntOrNull() ?: loggingElement("discountPercent", discountPercentageElement)
basePrice = basePriceElement
.replace(",", "")
.removeSuffix("").toBigDecimal()
}

val currentPrice = element.findElement(By.className(CURRENT_PRICE)).text
val currentPriceElement = element.findElement(By.className(CURRENT_PRICE)).text
if (currentPriceElement.isBlank()) {
return DefaultPrice.none()
}
val currentPrice = currentPriceElement
.replace(",", "")
.removeSuffix("").toBigDecimal()

Expand All @@ -45,4 +55,9 @@ class PriceParser(
isOutOfStock = isOutOfStock
)
}

private fun loggingElement(name: String, element: String?): Int {
logger.error { "할인율 파싱 중 오류가 발생했습니다. $name: $element" }
return 0
}
}
11 changes: 11 additions & 0 deletions src/main/kotlin/backend/itracker/crawl/service/vo/DefaultPrice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@ package backend.itracker.crawl.service.vo

import java.math.BigDecimal

private val none = DefaultPrice(
discountPercentage = 0,
basePrice = BigDecimal.ZERO,
discountPrice = BigDecimal.ZERO,
isOutOfStock = "품절"
)

data class DefaultPrice(
val discountPercentage: Int,
val basePrice: BigDecimal,
val discountPrice: BigDecimal,
val isOutOfStock: Boolean
) {
companion object {
fun none() = none
}

constructor(
discountPercentage: Int,
basePrice: BigDecimal,
Expand Down

0 comments on commit d473548

Please sign in to comment.