diff options
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/parsers/SearchParser.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/parsers/SearchParser.kt | 59 |
1 files changed, 34 insertions, 25 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/parsers/SearchParser.kt b/app/src/main/kotlin/com/pitchedapps/frost/parsers/SearchParser.kt index 908bb153..bc09d4db 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/parsers/SearchParser.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/parsers/SearchParser.kt @@ -1,8 +1,10 @@ package com.pitchedapps.frost.parsers -import ca.allanwang.kau.utils.withMaxLength +import ca.allanwang.kau.searchview.SearchItem +import com.pitchedapps.frost.dbflow.CookieModel import com.pitchedapps.frost.facebook.FbItem import com.pitchedapps.frost.facebook.formattedFbUrl +import com.pitchedapps.frost.parsers.FrostSearch.Companion.create import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.frostJsoup import org.jsoup.Jsoup @@ -12,11 +14,11 @@ import org.jsoup.nodes.Element /** * Created by Allan Wang on 2017-10-09. */ -object SearchParser : FrostParser<List<FrostSearch>> by SearchParserImpl() { - fun query(input: String): List<FrostSearch>? { +object SearchParser : FrostParser<FrostSearches> by SearchParserImpl() { + fun query(cookie: String?, input: String): ParseResponse<FrostSearches>? { val url = "${FbItem._SEARCH.url}?q=${if (input.isNotBlank()) input else "a"}" L.i(null, "Search Query $url") - return parse(frostJsoup(url)) + return parse(cookie, frostJsoup(url)) } } @@ -25,25 +27,40 @@ enum class SearchKeys(val key: String) { EVENTS("keywords_events") } +data class FrostSearches(val results: List<FrostSearch>) { + + override fun toString() = StringBuilder().apply { + append("FrostSearches {\n") + append(results.toJsonString("results", 1)) + append("}") + }.toString() +} + /** * As far as I'm aware, all links are independent, so the queries don't matter * A lot of it is tracking information, which I'll strip away * Other text items are formatted for safety + * + * Note that it's best to create search results from [create] */ -class FrostSearch(href: String, title: String, description: String?) { - val href = with(href.indexOf("?")) { if (this == -1) href else href.substring(0, this) } - val title = title.format() - val description = description?.format() +data class FrostSearch(val href: String, val title: String, val description: String?) { - private fun String.format() = replace("\n", " ").withMaxLength(50) - - override fun toString(): String - = "FrostSearch(href=$href, title=$title, description=$description)" + fun toSearchItem() = SearchItem(href, title, description) + companion object { + fun create(href: String, title: String, description: String?) = FrostSearch( + with(href.indexOf("?")) { if (this == -1) href else href.substring(0, this) }, + title.format(), + description?.format() + ) + } } -private class SearchParserImpl : FrostParserBase<List<FrostSearch>>() { - override fun parse(doc: Document): List<FrostSearch>? { +private class SearchParserImpl : FrostParserBase<FrostSearches>(false) { + + override val url = "${FbItem._SEARCH.url}?q=a" + + override fun parseImpl(doc: Document): FrostSearches? { val container: Element = doc.getElementById("BrowseResultsContainer") ?: doc.getElementById("root") ?: return null @@ -51,19 +68,11 @@ private class SearchParserImpl : FrostParserBase<List<FrostSearch>>() { * * Removed [data-store*=result_id] */ - return container.select("a.touchable[href]").filter(Element::hasText).map { - FrostSearch(it.attr("href").formattedFbUrl, + return FrostSearches(container.select("a.touchable[href]").filter(Element::hasText).map { + FrostSearch.create(it.attr("href").formattedFbUrl, it.select("._uoi").first()?.text() ?: "", it.select("._1tcc").first()?.text()) - }.filter { it.title.isNotBlank() } - } - - - override fun textToDoc(text: String): Document? = Jsoup.parse(text) - - override fun debugImpl(data: List<FrostSearch>, result: MutableList<String>) { - result.add("Has size ${data.size}") - result.addAll(data.map(FrostSearch::toString)) + }.filter { it.title.isNotBlank() }) } }
\ No newline at end of file |