-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from esafirm/placeholder_transformer
Placeholder transformer
- Loading branch information
Showing
5 changed files
with
115 additions
and
16 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
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
7 changes: 7 additions & 0 deletions
7
skrape-core/src/main/kotlin/nolambda/skrape/transformer/PageTransformer.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,7 @@ | ||
package nolambda.skrape.transformer | ||
|
||
import nolambda.skrape.nodes.Page | ||
|
||
interface PageTransformer<T> { | ||
fun transform(page: Page): Page | ||
} |
54 changes: 54 additions & 0 deletions
54
skrape-core/src/main/kotlin/nolambda/skrape/transformer/PlaceholderTransformer.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,54 @@ | ||
package nolambda.skrape.transformer | ||
|
||
import nolambda.skrape.nodes.* | ||
|
||
class PlaceholderTransformer( | ||
private val args: Map<String, String> | ||
) : PageTransformer<Page> { | ||
|
||
companion object { | ||
private val PLACEHOLDER_PATTERN = Regex(".*\\{\\{(.*)}}.*") | ||
} | ||
|
||
override fun transform(page: Page): Page { | ||
page.evaluate() | ||
|
||
val pageInfo = page.pageInfo | ||
return page.copy(pageInfo = pageInfo.copy(path = pageInfo.path.replacePlaceholder())).apply { | ||
setNewChildren(transformChildren(page.children)) | ||
} | ||
} | ||
|
||
private fun transformChildren(children: List<SkrapeElemenet>): List<SkrapeElemenet> { | ||
return children.map { | ||
when (it) { | ||
is Query -> it.copy(cssSelector = it.cssSelector.replacePlaceholder()).also { query -> | ||
query.setNewChildren(transformChildren(it.children)) | ||
} | ||
else -> it | ||
} | ||
} | ||
} | ||
|
||
private fun ParentElement.setNewChildren(newChildren: List<SkrapeElemenet>) { | ||
children.clear() | ||
children.addAll(newChildren) | ||
} | ||
|
||
private fun String.replacePlaceholder(): String { | ||
val results = PLACEHOLDER_PATTERN.findAll(this) | ||
if (results.count() == 0) return this | ||
|
||
val finalResult = results.fold(this) { acc, result -> | ||
val capturedKey = result.groupValues[1] | ||
args[capturedKey]?.let { acc.replace("{{${capturedKey}}}", it) } ?: acc | ||
} | ||
|
||
// Check if there's un-fulfilled placeholder | ||
if (PLACEHOLDER_PATTERN.matches(finalResult)) { | ||
throw IllegalArgumentException("Unfulfilled placeholder on: $this") | ||
} | ||
|
||
return finalResult | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
skrape-core/src/test/kotlin/nolamda/skrape/PlaceholderTransformerSpec.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,47 @@ | ||
package nolamda.skrape | ||
|
||
import io.kotlintest.matchers.shouldBe | ||
import io.kotlintest.matchers.shouldThrow | ||
import io.kotlintest.specs.StringSpec | ||
import nolambda.skrape.nodes.* | ||
import nolambda.skrape.transformer.PlaceholderTransformer | ||
import kotlin.to | ||
|
||
class PlaceholderTransformerSpec : StringSpec({ | ||
|
||
val transformer = PlaceholderTransformer(mapOf( | ||
"ngasal" to "tweet", | ||
"COBA" to "a" | ||
)) | ||
|
||
val page = Page("https://ngasal.com/{{ngasal}}") { | ||
query("td {{ngasal}}") { | ||
"place" to text() | ||
query("td {{COBA}}") { | ||
"another" to text() | ||
"place" to attr("href") | ||
} | ||
} | ||
} | ||
|
||
val resultPage = transformer.transform(page) | ||
|
||
"it should replace path placeholder" { | ||
val expectedPath = "https://ngasal.com/tweet" | ||
resultPage.pageInfo.path shouldBe expectedPath | ||
} | ||
|
||
"it should replace css selector placeholder" { | ||
val expectedSelector = "td tweet" | ||
|
||
val query = resultPage.children.first() as Query | ||
query.cssSelector shouldBe expectedSelector | ||
} | ||
|
||
"it should throw if there's unfulfilled" { | ||
val failingTransformer = PlaceholderTransformer(emptyMap()) | ||
shouldThrow<IllegalArgumentException> { | ||
failingTransformer.transform(page) | ||
} | ||
} | ||
}) |