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 {
|
||||
private const val RESERVED_TRAFFIC_LABEL = "میزان ترافیک رزرو شما"
|
||||
private val TRAFFIC_LABELS = listOf(
|
||||
"میزان ترافیک رزرو شما",
|
||||
"ترافیک رزرو شما",
|
||||
"حجم باقیمانده",
|
||||
"حجم باقی مانده",
|
||||
"حجم باقیمانده",
|
||||
"ترافیک باقی مانده",
|
||||
"ترافیک باقیمانده"
|
||||
)
|
||||
|
||||
fun isAuthenticated(html: String): Boolean {
|
||||
val lower = html.lowercase(Locale.US)
|
||||
@@ -40,18 +48,8 @@ object TciHtmlParser {
|
||||
|
||||
fun parseReservedTraffic(html: String): UsageSnapshotDraft {
|
||||
val document = Jsoup.parse(html)
|
||||
val candidateTexts = document.select("h5, h4, h3, div, span")
|
||||
.filter { it.text().contains(RESERVED_TRAFFIC_LABEL) }
|
||||
.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()
|
||||
val match = trafficTextCandidates(document)
|
||||
.firstNotNullOfOrNull { text -> TRAFFIC_REGEX.find(text) }
|
||||
?: throw RefreshException(
|
||||
RefreshErrorCode.PARSER_CHANGED,
|
||||
"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 =
|
||||
buildString(value.length) {
|
||||
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)
|
||||
}
|
||||
Reference in new issue
Block a user