From e04487f5b1a4c6a5999db8d9746a07919ae9a333 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Wed, 25 Oct 2017 12:41:05 -0400 Subject: Update search logic (#444) --- .../kotlin/com/pitchedapps/frost/activities/MainActivity.kt | 7 ++++--- .../main/kotlin/com/pitchedapps/frost/facebook/FbItem.kt | 8 +++++++- .../kotlin/com/pitchedapps/frost/facebook/FbUrlFormatter.kt | 3 --- .../kotlin/com/pitchedapps/frost/parsers/MessageParser.kt | 5 ++--- .../kotlin/com/pitchedapps/frost/parsers/SearchParser.kt | 13 +++++++------ app/src/main/kotlin/com/pitchedapps/frost/settings/Debug.kt | 4 ++-- .../com/pitchedapps/frost/parsers/MessageParserTest.kt | 4 ++-- 7 files changed, 24 insertions(+), 20 deletions(-) (limited to 'app') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt index b6232272..a346e809 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt @@ -56,8 +56,8 @@ import com.pitchedapps.frost.fragments.WebFragment import com.pitchedapps.frost.parsers.SearchParser import com.pitchedapps.frost.utils.* import com.pitchedapps.frost.utils.iab.FrostBilling -import com.pitchedapps.frost.utils.iab.IabMain import com.pitchedapps.frost.utils.iab.IS_FROST_PRO +import com.pitchedapps.frost.utils.iab.IabMain import com.pitchedapps.frost.views.BadgedIcon import com.pitchedapps.frost.views.FrostVideoViewer import com.pitchedapps.frost.views.FrostViewPager @@ -355,13 +355,14 @@ class MainActivity : BaseActivity(), doAsync { val data = SearchParser.query(query) ?: return@doAsync val items = data.map { SearchItem(it.href, it.title, it.description) }.toMutableList() - items.add(SearchItem("${FbItem.SEARCH.url}?q=$query", string(R.string.show_all_results), iicon = null)) + if (items.isNotEmpty()) + items.add(SearchItem("${FbItem._SEARCH.url}?q=$query", string(R.string.show_all_results), iicon = null)) searchViewCache.put(query, items) uiThread { searchView?.results = items } } } textDebounceInterval = 300 - searchCallback = { query, _ -> launchWebOverlay("${FbItem.SEARCH.url}/?q=$query"); true } + searchCallback = { query, _ -> launchWebOverlay("${FbItem._SEARCH.url}/?q=$query"); true } closeListener = { _ -> searchViewCache.clear() } foregroundColor = Prefs.textColor backgroundColor = Prefs.bgColor.withMinAlpha(200) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbItem.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbItem.kt index a4736091..2f478d44 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbItem.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbItem.kt @@ -29,11 +29,17 @@ enum class FbItem(@StringRes val titleId: Int, val icon: IIcon, relativeUrl: Str PHOTOS(R.string.photos, GoogleMaterial.Icon.gmd_photo, "me/photos"), PROFILE(R.string.profile, CommunityMaterial.Icon.cmd_account, "me"), SAVED(R.string.saved, GoogleMaterial.Icon.gmd_bookmark, "saved"), - SEARCH(R.string.search_menu_title, GoogleMaterial.Icon.gmd_search, "search"), + _SEARCH(R.string.search_menu_title, GoogleMaterial.Icon.gmd_search, "search/top"), SETTINGS(R.string.settings, GoogleMaterial.Icon.gmd_settings, "settings"), ; val url = "$FB_URL_BASE$relativeUrl" } +inline val fbSearch + get() = fbSearch() + +fun fbSearch(query: String = "a") = "${FB_SEARCH}$query" + +private const val FB_SEARCH = "${FB_URL_BASE}search/top/?q=" fun defaultTabs(): List = listOf(FbItem.FEED, FbItem.MESSAGES, FbItem.NOTIFICATIONS, FbItem.MENU) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbUrlFormatter.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbUrlFormatter.kt index 3298ff15..4db75fff 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbUrlFormatter.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbUrlFormatter.kt @@ -12,9 +12,6 @@ import java.nio.charset.StandardCharsets inline val String.formattedFbUrl: String get() = FbUrlFormatter(this).toString() -inline val String.formattedFbUrlCss: String - get() = FbUrlFormatter(this).toString() - class FbUrlFormatter(url: String) { private val queries = mutableMapOf() private val cleaned: String diff --git a/app/src/main/kotlin/com/pitchedapps/frost/parsers/MessageParser.kt b/app/src/main/kotlin/com/pitchedapps/frost/parsers/MessageParser.kt index 7e6ef4bb..9430407d 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/parsers/MessageParser.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/parsers/MessageParser.kt @@ -1,7 +1,6 @@ package com.pitchedapps.frost.parsers import com.pitchedapps.frost.facebook.formattedFbUrl -import com.pitchedapps.frost.facebook.formattedFbUrlCss import com.pitchedapps.frost.utils.L import org.apache.commons.text.StringEscapeUtils import org.jsoup.Jsoup @@ -65,10 +64,10 @@ private class MessageParserImpl : FrostParserBase, Fros L.v("url", a.attr("href")) return FrostThread( id = id.toInt(), - img = pUrl.formattedFbUrlCss, + img = pUrl.formattedFbUrl, title = a.text(), time = epoch, - url = a.attr("href").formattedFbUrlCss, + url = a.attr("href").formattedFbUrl, unread = !element.hasClass("acw"), content = content ) 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 37c09299..908bb153 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/parsers/SearchParser.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/parsers/SearchParser.kt @@ -2,7 +2,7 @@ package com.pitchedapps.frost.parsers import ca.allanwang.kau.utils.withMaxLength import com.pitchedapps.frost.facebook.FbItem -import com.pitchedapps.frost.facebook.formattedFbUrlCss +import com.pitchedapps.frost.facebook.formattedFbUrl import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.frostJsoup import org.jsoup.Jsoup @@ -14,7 +14,7 @@ import org.jsoup.nodes.Element */ object SearchParser : FrostParser> by SearchParserImpl() { fun query(input: String): List? { - val url = "${FbItem.SEARCH.url}?q=$input" + val url = "${FbItem._SEARCH.url}?q=${if (input.isNotBlank()) input else "a"}" L.i(null, "Search Query $url") return parse(frostJsoup(url)) } @@ -51,10 +51,10 @@ private class SearchParserImpl : FrostParserBase>() { * * Removed [data-store*=result_id] */ - return container.select("a.touchable.primary[href]").filter(Element::hasText).mapNotNull { - FrostSearch(it.attr("href").formattedFbUrlCss, - it.select(".title").first()?.text() ?: "", - it.select(".subtitle").first()?.ownText()) + return container.select("a.touchable[href]").filter(Element::hasText).map { + FrostSearch(it.attr("href").formattedFbUrl, + it.select("._uoi").first()?.text() ?: "", + it.select("._1tcc").first()?.text()) }.filter { it.title.isNotBlank() } } @@ -62,6 +62,7 @@ private class SearchParserImpl : FrostParserBase>() { override fun textToDoc(text: String): Document? = Jsoup.parse(text) override fun debugImpl(data: List, result: MutableList) { + result.add("Has size ${data.size}") result.addAll(data.map(FrostSearch::toString)) } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/settings/Debug.kt b/app/src/main/kotlin/com/pitchedapps/frost/settings/Debug.kt index a4f4388f..7001275d 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/settings/Debug.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/settings/Debug.kt @@ -48,8 +48,8 @@ fun SettingsActivity.getDebugPrefs(): KPrefAdapterBuilder.() -> Unit = { private enum class Debugger(val data: FbItem, val injector: InjectorContract?, vararg query: String) { MENU(FbItem.MENU, JsAssets.MENU_DEBUG, "#viewport"), //todo modify menu js for debugging - NOTIFICATIONS(FbItem.NOTIFICATIONS, null, "#notifications_list"), - SEARCH(FbItem.SEARCH, JsActions.FETCH_BODY); + NOTIFICATIONS(FbItem.NOTIFICATIONS, null, "#notifications_list"); +// SEARCH(FbItem.SEARCH, JsActions.FETCH_BODY); val query = if (query.isNotEmpty()) arrayOf(*query, "#root", "main", "body") else emptyArray() diff --git a/app/src/test/kotlin/com/pitchedapps/frost/parsers/MessageParserTest.kt b/app/src/test/kotlin/com/pitchedapps/frost/parsers/MessageParserTest.kt index 91f765cc..61c69c40 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/parsers/MessageParserTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/parsers/MessageParserTest.kt @@ -1,6 +1,6 @@ package com.pitchedapps.frost.parsers -import com.pitchedapps.frost.facebook.formattedFbUrlCss +import com.pitchedapps.frost.facebook.formattedFbUrl import org.junit.Test import kotlin.test.assertEquals @@ -21,7 +21,7 @@ class MessageParserTest { @Test fun parseImage() { var input = "https\\3a //scontent.fyhu1-1.fna.fbcdn.net/v/t1.0-1/cp0/e15/q65/p100x100/12994387_243040309382307_4586627375882013710_n.jpg?efg\\3d eyJpIjoidCJ9\\26 oh\\3d b9ae0d7a1298989fe24873e2ee4054b6\\26 oe\\3d 5A3A7FE1" - input = input.formattedFbUrlCss + input = input.formattedFbUrl println(input) } } \ No newline at end of file -- cgit v1.2.3