appstore: drop the jsoup play store scraping fallback
- Commit
416dbca07ed2a1c79f8a2a5f9d6b59bf7e60f801- Author
- Vayun Mathur <[email protected]>
- When
- 2026-08-05 22:14 UTC (4 months ago)
- Parents
724e9ef
Diff
appstore/build.gradle.kts
diff --git a/appstore/build.gradle.kts b/appstore/build.gradle.ktsindex 66e6369880..d826ce45a6 100644--- a/appstore/build.gradle.kts+++ b/appstore/build.gradle.kts@@ -23,7 +23,6 @@ dependencies { implementation(project(":library:work")) implementation(libs.androidx.datastore.preferences) implementation(project(":library:image"))- implementation(libs.jsoup) // APK source-stamp verification. The stamp is a second signing identity that // survives Play App Signing re-signing, so it can be pinned per package where the // APK signing key (held by Google) cannot be.
appstore/src/main/java/com/vayunmathur/appstore/data/PlayStoreDataSource.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/PlayStoreDataSource.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/PlayStoreDataSource.ktdeleted file mode 100644index a865f3919b..0000000000--- a/appstore/src/main/java/com/vayunmathur/appstore/data/PlayStoreDataSource.kt+++ /dev/null@@ -1,115 +0,0 @@-package com.vayunmathur.appstore.data--import kotlinx.coroutines.Dispatchers-import kotlinx.coroutines.withContext-import org.jsoup.Jsoup-import android.net.Uri-import java.net.HttpURLConnection-import java.net.URL--object PlayStoreDataSource {-- const val PLAY_BASE = "https://play.google.com"-- suspend fun search(query: String): List<UnifiedApp> = withContext(Dispatchers.IO) {- if (query.isBlank()) return@withContext emptyList()- try {- val q = Uri.encode(query)- val url = "$PLAY_BASE/store/search?q=$q&c=apps"- val conn = (URL(url).openConnection() as HttpURLConnection).apply {- connectTimeout = 15000- readTimeout = 20000- setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; Android 13) AppleWebKit/537.36")- setRequestProperty("Accept-Language", "en-US,en;q=0.9")- }- val html = conn.inputStream.bufferedReader().readText()- AppProvider.filterTargetSdk(parseSearchHtml(html))- } catch (_: Exception) { emptyList() }- }-- suspend fun topCharts(category: String = ""): List<UnifiedApp> = withContext(Dispatchers.IO) {- try {- val catPath = if (category.isNotBlank()) "/category/${Uri.encode(category)}" else ""- val url = "$PLAY_BASE/store/apps$catPath?hl=en_US&gl=US"- val conn = (URL(url).openConnection() as HttpURLConnection).apply {- connectTimeout = 15000- readTimeout = 20000- setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; Android 13) AppleWebKit/537.36")- }- val html = conn.inputStream.bufferedReader().readText()- AppProvider.filterTargetSdk(parseSearchHtml(html).take(30))- } catch (_: Exception) { emptyList() }- }-- suspend fun appDetails(packageName: String): UnifiedApp? = withContext(Dispatchers.IO) {- try {- val url = "$PLAY_BASE/store/apps/details?id=$packageName&hl=en_US&gl=US"- val doc = Jsoup.connect(url)- .userAgent("Mozilla/5.0 (Linux; Android 13) AppleWebKit/537.36")- .timeout(20000)- .get()-- val name = doc.selectFirst("h1 span")?.text()- ?: doc.selectFirst("[itemprop=name] span")?.text()- ?: packageName.substringAfterLast('.')-- val developer = doc.selectFirst("a[href*=/store/apps/developer] span")?.text()- ?: doc.selectFirst("a[href*=/store/apps/dev] span")?.text()-- val icon = doc.selectFirst("img[alt*=Icon]")?.attr("src")- ?: doc.selectFirst("img.T75of")?.attr("src")-- val summary = doc.selectFirst("div[itemprop=description]")?.text()?.take(500) ?: ""- val ratingText = doc.selectFirst("div.TT9eCd")?.text()- val rating = ratingText?.toFloatOrNull()-- val pageText = doc.text()- val targetSdk = Regex("Target.*API.*?(\\d{2})|targetSdk.*?(\\d{2})", RegexOption.IGNORE_CASE)- .find(pageText)?.groupValues?.filter { it.toIntOrNull() != null }?.lastOrNull()?.toIntOrNull()-- val app = UnifiedApp(- packageName = packageName,- source = AppSource.PLAYSTORE,- name = name,- summary = summary.take(200),- description = doc.selectFirst("div[itemprop=description]")?.text() ?: summary,- iconUrl = icon,- author = developer,- categories = emptyList(),- rating = rating,- targetSdk = targetSdk,- website = "$PLAY_BASE/store/apps/details?id=$packageName"- )- if (targetSdk != null && targetSdk < AppProvider.MIN_TARGET_SDK) null else app- } catch (_: Exception) { null }- }-- private fun parseSearchHtml(html: String): List<UnifiedApp> {- val results = mutableListOf<UnifiedApp>()- try {- val doc = Jsoup.parse(html)- val candidates = doc.select("a[href*=/store/apps/details?id=]")- val seen = mutableSetOf<String>()- for (a in candidates) {- val href = a.attr("href")- val pkg = Regex("[?&]id=([^&]+)").find(href)?.groupValues?.get(1) ?: continue- if (!seen.add(pkg)) continue- if (results.size >= 40) break- val title = a.attr("title").ifBlank { a.text().ifBlank { pkg } }- val img = a.selectFirst("img")?.attr("src")- ?: a.parent()?.selectFirst("img")?.attr("src")- results += UnifiedApp(- packageName = pkg,- source = AppSource.PLAYSTORE,- name = title.take(80),- iconUrl = img,- summary = "",- website = "$PLAY_BASE/store/apps/details?id=$pkg"- )- }- } catch (_: Exception) { }- return results- }-- fun playStoreUrl(pkg: String): String = "$PLAY_BASE/store/apps/details?id=$pkg"-}
appstore/src/main/java/com/vayunmathur/appstore/data/PlayStoreLinks.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/PlayStoreLinks.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/PlayStoreLinks.ktnew file mode 100644index 0000000000..1c81139a75--- /dev/null+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/PlayStoreLinks.kt@@ -0,0 +1,8 @@+package com.vayunmathur.appstore.data++object PlayStoreLinks {++ const val PLAY_BASE = "https://play.google.com"++ fun playStoreUrl(pkg: String): String = "$PLAY_BASE/store/apps/details?id=$pkg"+}
appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.ktindex 92ea1741ef..7b6984ecc9 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt@@ -13,7 +13,7 @@ import com.vayunmathur.appstore.data.AppSource import com.vayunmathur.appstore.data.CatalogRepository import com.vayunmathur.appstore.data.InstalledAppsRepository import com.vayunmathur.appstore.data.InstalledInfo-import com.vayunmathur.appstore.data.PlayStoreDataSource+import com.vayunmathur.appstore.data.PlayStoreLinks import com.vayunmathur.appstore.data.SyncStep import com.vayunmathur.appstore.data.UnifiedApp import com.vayunmathur.appstore.data.installer.InstallCoordinator@@ -291,7 +291,7 @@ class AppStoreViewModel( if (_playSections.value.isEmpty()) { // No account, or Play changed its stream shape. A top chart is one request and // still gives the screen something beyond this repo's own dozen apps.- val chart = play.topChart().ifEmpty { PlayStoreDataSource.topCharts() }+ val chart = play.topChart() if (chart.isNotEmpty()) { _playSections.value = listOf( AppSection(@@ -413,7 +413,7 @@ class AppStoreViewModel( val local = catalog.searchLocal(query) _searchResults.value = rank(local, query) - val remote = play.search(query).ifEmpty { PlayStoreDataSource.search(query) }+ val remote = play.search(query) _searchResults.value = rank(merge(local, remote), query) _isSearching.value = false _hasSearched.value = true@@ -469,12 +469,11 @@ class AppStoreViewModel( _selectedApp.value = cached return@launch }- // Play listings from a cluster or a scrape are shells: no description, no+ // Play listings from a cluster are shells: no description, no // screenshots, no version code. Fill them in before the page settles. if (app.source == AppSource.PLAYSTORE && app.screenshots.isEmpty()) { _isLoadingDetails.value = true val details = play.details(app.packageName)- ?: PlayStoreDataSource.appDetails(app.packageName) if (details != null) _selectedApp.value = details _isLoadingDetails.value = false }@@ -495,7 +494,7 @@ class AppStoreViewModel( source = AppSource.PLAYSTORE, name = packageName.substringAfterLast('.'), )- val details = play.details(packageName) ?: PlayStoreDataSource.appDetails(packageName)+ val details = play.details(packageName) if (details != null) _selectedApp.value = details _isLoadingDetails.value = false }@@ -555,7 +554,7 @@ class AppStoreViewModel( override fun openInPlayStore(packageName: String) { if (startActivity(Intent(Intent.ACTION_VIEW, "market://details?id=$packageName".toUri()))) return- startActivity(Intent(Intent.ACTION_VIEW, PlayStoreDataSource.playStoreUrl(packageName).toUri()))+ startActivity(Intent(Intent.ACTION_VIEW, PlayStoreLinks.playStoreUrl(packageName).toUri())) } override fun openInBrowser(url: String) {@@ -564,7 +563,7 @@ class AppStoreViewModel( } override fun shareApp(app: UnifiedApp) {- val link = app.website ?: PlayStoreDataSource.playStoreUrl(app.packageName)+ val link = app.website ?: PlayStoreLinks.playStoreUrl(app.packageName) val share = Intent(Intent.ACTION_SEND).apply { type = "text/plain" putExtra(Intent.EXTRA_TEXT, context.getString(R.string.share_app_text, app.name, link))