Skip to content

Commit

Permalink
fix: 크롤링 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
cookienc committed Dec 14, 2024
1 parent 28f960e commit dab160a
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 23 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ dependencies {
implementation 'com.slack.api:slack-api-client:1.40.2'

runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.mysql:mysql-connector-j'
implementation 'com.mysql:mysql-connector-j'

// RestAssured
testImplementation 'io.rest-assured:rest-assured'
Expand Down
23 changes: 23 additions & 0 deletions src/main/kotlin/backend/itracker/crawl/macbook/domain/Macbook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ class Macbook(

id: Long = 0L
) : BaseEntity(id) {
companion object {
fun empty(): Macbook {
return Macbook(
coupangId = 0,
company = "",
name = "",
chip = "",
cpu = "",
gpu = "",
storage = "",
memory = "",
language = "",
color = "",
size = 0,
releaseYear = 0,
category = MacbookCategory.MACBOOK_AIR,
productLink = "",
thumbnail = ""
).apply {
id = -1
}
}
}

fun addAllPrices(targetPrices: MacbookPrices) {
targetPrices.macbookPrices.forEach(this::addPrice)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fun MacbookRepository.findByIdAllFetch(macbookId: Long): Macbook = findAllPrices

interface MacbookRepository: JpaRepository<Macbook, Long>, MacbookRepositoryCustom {

fun findByCoupangId(coupangId: Long): Optional<Macbook>
fun findByCoupangId(coupangId: Long): Macbook?

@Query(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ class MacbookService(
) {

fun saveAll(macbooks: List<Macbook>) {
for (macbook in macbooks) {
val maybeMacbook = macbookRepository.findByCoupangId(macbook.coupangId)
if (maybeMacbook.isEmpty) {
macbookRepository.save(macbook)
continue
macbooks.filter { it != Macbook.empty() }
.forEach { macbook ->
macbookRepository.findByCoupangId(macbook.coupangId)
?.apply { addAllPrices(macbook.prices) }
?: macbookRepository.save(macbook)
}
maybeMacbook.get().addAllPrices(macbook.prices)
}
}

fun updateAllPartnersLink(partnersLinkInformation: List<PartnersLinkInfo>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MacbookAirM213Mapper : MacbookMappingComponent {
val gpu = names[3].split(" ").last()
val storage = names[4]
val memory = names[5]
val language = names[7]
val language = names.getOrElse(7) { "" }

return MacbookCrawlResponse(
coupangId = product.productId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@ private const val MAC_BOOK_AIR_M3_15_CTO = "MacBook Air 15 M3 CTO"

@Component
class MacbookAirM3Mapper : MacbookMappingComponent {
companion object {
private val ALL_MAC_BOOK_AIR_M3 = listOf(
MAC_BOOK_AIR_M3_13,
MAC_BOOK_AIR_M3_13_CTO,
MAC_BOOK_AIR_M3_15,
MAC_BOOK_AIR_M3_15_CTO
)
}

override fun supports(subCategory: String): Boolean {
return subCategory == MAC_BOOK_AIR_M3_13 ||
subCategory == MAC_BOOK_AIR_M3_13_CTO ||
subCategory == MAC_BOOK_AIR_M3_15 ||
subCategory == MAC_BOOK_AIR_M3_15_CTO
return subCategory in ALL_MAC_BOOK_AIR_M3
}

override fun toDomain(product: DefaultProduct): Macbook {
if (!product.name.contains("M3")) { // 쿠팡에서 M2지만 카테고리가 M3로 표기되는 경우가 있음
return Macbook.empty()
}

val names = product.name.split(",")
.map { it.trim() }
.toList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class MacbookMappers(
macbooks.add(macbookMapper.toDomain(product))
}
}
macbooks.filterNot{ it == Macbook.empty()}
} catch (e: Exception) {
throw CrawlException(
"""
Expand Down
17 changes: 8 additions & 9 deletions src/main/kotlin/backend/itracker/logging/aspect/LoggerAspect.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import org.springframework.context.annotation.Profile
import org.springframework.stereotype.Component
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.context.request.ServletRequestAttributes
import java.util.*


private val LOGGER = KotlinLogging.logger {}
Expand Down Expand Up @@ -40,12 +39,12 @@ class LoggerAspect {

@Before("apiInfo()")
fun beforeApiRequest(joinPoint: JoinPoint) {
val request =
(Objects.requireNonNull(RequestContextHolder.getRequestAttributes()) as ServletRequestAttributes).request
val request = RequestContextHolder.getRequestAttributes()?.let { (it as ServletRequestAttributes).request }

LOGGER.info {
"""
[ API Start ]
- Method: ${request.method}
- Method: ${request?.method ?: "Local"}
- URI: ${getURI(request)}
- IP: ${getClientIP(request)}
- Signature: ${getSignature(joinPoint)}
Expand All @@ -54,7 +53,7 @@ class LoggerAspect {
}

@AfterReturning(value = "apiInfo()", returning = "response")
fun afterApiRequest(response: Any) {
fun afterApiRequest(response: Any?) {
LOGGER.info {
"""
[ API End ]
Expand All @@ -63,12 +62,12 @@ class LoggerAspect {
}
}

private fun getURI(request: HttpServletRequest): String {
return "${request.requestURI}${request.queryString.let { if (it != null) "?$it" else "" }}"
private fun getURI(request: HttpServletRequest?): String {
return "${request?.requestURI}${request?.queryString.let { if (it != null) "?$it" else "" }}"
}

private fun getClientIP(request: HttpServletRequest): String? {
return IP_HEADERS.firstNotNullOfOrNull { header -> request.getHeader(header) } ?: request.remoteAddr
private fun getClientIP(request: HttpServletRequest?): String? {
return IP_HEADERS.firstNotNullOfOrNull { header -> request?.getHeader(header) } ?: request?.remoteAddr
}

private fun getSignature(joinPoint: JoinPoint): String {
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
spring:
application:
name: itracker
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:14306/itracker?serverTimezone=Asia/Seoul
username: root
password: itracker123!

jpa:
hibernate:
ddl-auto: update
Expand Down

0 comments on commit dab160a

Please sign in to comment.