Fixed the TCI number parser
This commit is contained in:
2 files changed
+92
-14
No files matched your search
@@ -14,7 +14,15 @@ data class TciLoginPage(
|
|||||||
)
|
)
|
||||||
|
|
||||||
object TciHtmlParser {
|
object TciHtmlParser {
|
||||||
private const val RESERVED_TRAFFIC_LABEL = "میزان ترافیک رزرو شما"
|
private val TRAFFIC_LABELS = listOf(
|
||||||
|
"میزان ترافیک رزرو شما",
|
||||||
|
"ترافیک رزرو شما",
|
||||||
|
"حجم باقیمانده",
|
||||||
|
"حجم باقی مانده",
|
||||||
|
"حجم باقیمانده",
|
||||||
|
"ترافیک باقی مانده",
|
||||||
|
"ترافیک باقیمانده"
|
||||||
|
)
|
||||||
|
|
||||||
fun isAuthenticated(html: String): Boolean {
|
fun isAuthenticated(html: String): Boolean {
|
||||||
val lower = html.lowercase(Locale.US)
|
val lower = html.lowercase(Locale.US)
|
||||||
@@ -40,18 +48,8 @@ object TciHtmlParser {
|
|||||||
|
|
||||||
fun parseReservedTraffic(html: String): UsageSnapshotDraft {
|
fun parseReservedTraffic(html: String): UsageSnapshotDraft {
|
||||||
val document = Jsoup.parse(html)
|
val document = Jsoup.parse(html)
|
||||||
val candidateTexts = document.select("h5, h4, h3, div, span")
|
val match = trafficTextCandidates(document)
|
||||||
.filter { it.text().contains(RESERVED_TRAFFIC_LABEL) }
|
.firstNotNullOfOrNull { text -> TRAFFIC_REGEX.find(text) }
|
||||||
.flatMap { element ->
|
|
||||||
listOfNotNull(element.text(), element.parent()?.text())
|
|
||||||
}
|
|
||||||
.ifEmpty { listOf(document.text()) }
|
|
||||||
|
|
||||||
val match = candidateTexts
|
|
||||||
.asSequence()
|
|
||||||
.map { normalizeDigits(it) }
|
|
||||||
.mapNotNull { TRAFFIC_REGEX.find(it) }
|
|
||||||
.firstOrNull()
|
|
||||||
?: throw RefreshException(
|
?: throw RefreshException(
|
||||||
RefreshErrorCode.PARSER_CHANGED,
|
RefreshErrorCode.PARSER_CHANGED,
|
||||||
"TCI reserved traffic value was not found."
|
"TCI reserved traffic value was not found."
|
||||||
@@ -74,6 +72,44 @@ object TciHtmlParser {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun trafficTextCandidates(document: org.jsoup.nodes.Document): Sequence<String> =
|
||||||
|
sequence {
|
||||||
|
val labeledElements = document.select("h5, h4, h3, div, span, li, td, p")
|
||||||
|
.filter { element -> containsTrafficLabel(element.text()) }
|
||||||
|
|
||||||
|
labeledElements.forEach { element ->
|
||||||
|
textAfterTrafficLabel(element.text())?.let { yield(it) }
|
||||||
|
|
||||||
|
var sibling = element.nextElementSibling()
|
||||||
|
repeat(4) {
|
||||||
|
val currentSibling = sibling ?: return@repeat
|
||||||
|
yield(normalizeForSearch(currentSibling.text()))
|
||||||
|
sibling = currentSibling.nextElementSibling()
|
||||||
|
}
|
||||||
|
|
||||||
|
element.parent()?.text()?.let { parentText ->
|
||||||
|
textAfterTrafficLabel(parentText)?.let { yield(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
textAfterTrafficLabel(document.text())?.let { yield(it) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun containsTrafficLabel(value: String): Boolean {
|
||||||
|
val normalized = normalizeForSearch(value)
|
||||||
|
return TRAFFIC_LABELS.any { normalized.contains(normalizeForSearch(it)) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun textAfterTrafficLabel(value: String): String? {
|
||||||
|
val normalized = normalizeForSearch(value)
|
||||||
|
val label = TRAFFIC_LABELS
|
||||||
|
.map(::normalizeForSearch)
|
||||||
|
.filter { normalized.contains(it) }
|
||||||
|
.maxByOrNull { normalized.indexOf(it) }
|
||||||
|
?: return null
|
||||||
|
return normalized.substring(normalized.indexOf(label) + label.length)
|
||||||
|
}
|
||||||
|
|
||||||
fun normalizeDigits(value: String): String =
|
fun normalizeDigits(value: String): String =
|
||||||
buildString(value.length) {
|
buildString(value.length) {
|
||||||
value.forEach { char ->
|
value.forEach { char ->
|
||||||
@@ -96,5 +132,11 @@ object TciHtmlParser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val TRAFFIC_REGEX = Regex("""(\d+(?:\.\d+)?)\s*(گیگابایت|مگابایت|GB|MB)""", RegexOption.IGNORE_CASE)
|
private fun normalizeForSearch(value: String): String =
|
||||||
|
normalizeDigits(value)
|
||||||
|
.replace('\u200c', ' ')
|
||||||
|
.replace(Regex("\\s+"), " ")
|
||||||
|
.trim()
|
||||||
|
|
||||||
|
private val TRAFFIC_REGEX = Regex("""(\d+(?:\.\d+)?)\s*(گیگابایت|گیگ|مگابایت|مگ|GB|MB)""", RegexOption.IGNORE_CASE)
|
||||||
}
|
}
|
||||||
@@ -73,6 +73,42 @@ class ParserTest {
|
|||||||
assertEquals("12.50 GB", snapshot.remainingDisplay)
|
assertEquals("12.50 GB", snapshot.remainingDisplay)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun tciUsesTrafficValueAfterReservedTrafficLabel() {
|
||||||
|
val snapshot = TciHtmlParser.parseReservedTraffic(
|
||||||
|
"""
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<div>
|
||||||
|
<h5>میزان ترافیک مصرفی شما ۸۸ گیگابایت</h5>
|
||||||
|
<h5>میزان ترافیک رزرو شما ۱۲٫۵ گیگابایت</h5>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
""".trimIndent()
|
||||||
|
)
|
||||||
|
|
||||||
|
assertEquals("12.50 GB", snapshot.remainingDisplay)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun tciReadsReservedTrafficFromFollowingSibling() {
|
||||||
|
val snapshot = TciHtmlParser.parseReservedTraffic(
|
||||||
|
"""
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<div>
|
||||||
|
<h5>میزان ترافیک رزرو شما</h5>
|
||||||
|
<span>۷۵۰ مگابایت</span>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
""".trimIndent()
|
||||||
|
)
|
||||||
|
|
||||||
|
assertEquals("750 MB", snapshot.remainingDisplay)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun trafficFormattingUsesMegabytesBelowOneGigabyte() {
|
fun trafficFormattingUsesMegabytesBelowOneGigabyte() {
|
||||||
assertEquals("512 MB", (512L * 1024L * 1024L).asTrafficDisplay())
|
assertEquals("512 MB", (512L * 1024L * 1024L).asTrafficDisplay())
|
||||||
|
|||||||
Reference in new issue
Block a user