-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix lidl parsing of products (and use coroutines)
- Loading branch information
1 parent
f3e19bb
commit 64b9123
Showing
9 changed files
with
10,094 additions
and
6,247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/kotlin/com/stefanbratanov/sofiasupermarketsapi/extractors/LidlProductExtractor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.stefanbratanov.sofiasupermarketsapi.extractors | ||
|
||
import com.stefanbratanov.sofiasupermarketsapi.common.Log | ||
import com.stefanbratanov.sofiasupermarketsapi.common.Log.Companion.log | ||
import com.stefanbratanov.sofiasupermarketsapi.common.getHtmlDocument | ||
import com.stefanbratanov.sofiasupermarketsapi.common.normalizePrice | ||
import com.stefanbratanov.sofiasupermarketsapi.model.Product | ||
import java.net.URL | ||
import java.time.LocalDate | ||
import java.time.format.DateTimeFormatter | ||
import org.apache.commons.lang3.StringUtils | ||
import org.springframework.stereotype.Component | ||
|
||
@Log | ||
@Component | ||
class LidlProductExtractor { | ||
|
||
fun extract(url: URL): Product { | ||
log.debug("Processing Lidl product URL: {}", url) | ||
|
||
val document = getHtmlDocument(url) | ||
|
||
val name = document.selectFirst("h1.keyfacts__title")?.text() | ||
val quantity = document.selectFirst("div.price-footer")?.text() | ||
val price = document.selectFirst("div.m-price__price")?.text() | ||
val oldPrice = document.selectFirst("span.m-price__rrp")?.text() | ||
|
||
val dateRange = | ||
document.selectFirst("span[data-v-35dadb86]")?.text()?.trim()?.let { dateSpan -> | ||
"\\d+.\\d+.".toRegex().findAll(dateSpan).map { date -> | ||
val match = date.groupValues[0] | ||
try { | ||
LocalDate.parse( | ||
match.plus(LocalDate.now().year), | ||
DateTimeFormatter.ofPattern("dd.MM.yyyy"), | ||
) | ||
} catch (ex: Exception) { | ||
log.error("Error while parsing $date", ex) | ||
null | ||
} | ||
} | ||
} | ||
|
||
return Product( | ||
name = StringUtils.normalizeSpace(name), | ||
quantity = StringUtils.normalizeSpace(quantity), | ||
price = normalizePrice(price), | ||
oldPrice = normalizePrice(oldPrice), | ||
category = null, | ||
picUrl = null, | ||
validFrom = dateRange?.elementAtOrNull(0), | ||
validUntil = dateRange?.elementAtOrNull(1) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...est/kotlin/com/stefanbratanov/sofiasupermarketsapi/extractors/LidlProductExtractorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.stefanbratanov.sofiasupermarketsapi.extractors | ||
|
||
import assertk.assertThat | ||
import assertk.assertions.isEqualTo | ||
import assertk.assertions.isEqualToIgnoringGivenProperties | ||
import com.stefanbratanov.sofiasupermarketsapi.getUri | ||
import com.stefanbratanov.sofiasupermarketsapi.model.Product | ||
import java.time.LocalDate | ||
import java.time.Month | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class LidlProductExtractorTest { | ||
|
||
private val underTest = LidlProductExtractor() | ||
|
||
@Test | ||
fun `test extracting product`() { | ||
val testHtmlUrl = getUri("/extractors/lidl/input-single.html").toURL() | ||
|
||
val product = underTest.extract(testHtmlUrl) | ||
|
||
val expectedProduct = | ||
Product( | ||
name = "Багета Рустик", | ||
quantity = "250 g/бр.", | ||
price = 0.99, | ||
oldPrice = 1.99, | ||
category = null, | ||
picUrl = null | ||
) | ||
|
||
assertThat(product) | ||
.isEqualToIgnoringGivenProperties(expectedProduct, Product::validFrom, Product::validUntil) | ||
|
||
assertThat(product.validFrom?.dayOfMonth).isEqualTo(30) | ||
assertThat(product.validFrom?.month).isEqualTo(Month.OCTOBER) | ||
assertThat(product.validFrom?.year).isEqualTo(LocalDate.now().year) | ||
assertThat(product.validUntil?.dayOfMonth).isEqualTo(5) | ||
assertThat(product.validUntil?.month).isEqualTo(Month.NOVEMBER) | ||
assertThat(product.validUntil?.year).isEqualTo(LocalDate.now().year) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.