Skip to content

Commit

Permalink
Add all area search (yujincheng08#275)
Browse files Browse the repository at this point in the history
* Add all area search

* Use Uri to parse url instead of Regex
  • Loading branch information
JasonKhew96 authored Sep 25, 2021
1 parent d417be3 commit 553d2c6
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 40 deletions.
104 changes: 69 additions & 35 deletions app/src/main/java/me/iacn/biliroaming/hook/BangumiSeasonHook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ import me.iacn.biliroaming.Constant.TYPE_EPISODE_ID
import me.iacn.biliroaming.Constant.TYPE_SEASON_ID
import me.iacn.biliroaming.Protos
import me.iacn.biliroaming.network.BiliRoamingApi
import me.iacn.biliroaming.network.BiliRoamingApi.getAreaSearchBangumi
import me.iacn.biliroaming.network.BiliRoamingApi.getContent
import me.iacn.biliroaming.network.BiliRoamingApi.getSeason
import me.iacn.biliroaming.network.BiliRoamingApi.getThailandSearchBangumi
import me.iacn.biliroaming.utils.*
import org.json.JSONObject
import java.io.InputStream
import java.lang.reflect.Method
import java.net.URL
import java.net.URLDecoder
import kotlin.Array
import kotlin.Boolean
import kotlin.Int
import java.lang.reflect.Array as RArray

/**
Expand All @@ -32,7 +35,16 @@ class BangumiSeasonHook(classLoader: ClassLoader) : BaseHook(classLoader) {
instance.kotlinJsonClass?.getStaticObjectField("Companion")?.callMethod("getNonstrict")
}
const val FAIL_CODE = -404
private const val TH_TYPE = 114514

data class Area(val type: String, val text: String)

private val AREA_TYPES =
mapOf(
114 to Area("th", ""),
514 to Area("cn", ""),
1919 to Area("hk", ""),
810 to Area("tw", "")
)
}

private val isSerializable by lazy {
Expand Down Expand Up @@ -173,10 +185,12 @@ class BangumiSeasonHook(classLoader: ClassLoader) : BaseHook(classLoader) {
) {
fixBangumi(body)
}
if (url != null && url.startsWith("https://appintl.biliapi.net/intl/gateway/app/search/type") &&
!url.contains("type=$TH_TYPE")
if (url != null && url.startsWith("https://appintl.biliapi.net/intl/gateway/app/search/type")
) {
fixPlaySearchType(body, url)
val area = Uri.parse(url).getQueryParameter("type")?.toInt()
if (!AREA_TYPES.containsKey(area)) {
fixPlaySearchType(body, url)
}
}
if (instance.generalResponseClass?.isInstance(body) == true ||
instance.rxGeneralResponseClass?.isInstance(body) == true
Expand All @@ -185,15 +199,24 @@ class BangumiSeasonHook(classLoader: ClassLoader) : BaseHook(classLoader) {
if (instance.generalResponseClass?.isInstance(body) == true) "data" else instance.responseDataField().value
val data = body.getObjectField(dataField)
if (data?.javaClass == searchAllResultClass) {
addThailandTag(data)
addAreaTags(data)
}
url ?: return@hookBeforeAllConstructors
if (data?.javaClass == bangumiSearchPageClass &&
(url.startsWith("https://app.bilibili.com/x/v2/search/type") ||
url.startsWith("https://appintl.biliapi.net/intl/gateway/app/search/type"))
&& url.contains("type=$TH_TYPE")
) {
body.setObjectField(dataField, retrieveThailandSearch(data, url))
val area = Uri.parse(url).getQueryParameter("type")?.toInt()
if (AREA_TYPES.containsKey(area)) {
body.setObjectField(dataField,
AREA_TYPES[area]?.let {
retrieveAreaSearch(
data,
url,
it.type
)
})
}
} else if (url.startsWith("https://app.bilibili.com/x/v2/view?") ||
url.startsWith("https://app.bilibili.com/x/intl/view?") ||
url.startsWith("https://appintl.biliapi.net/intl/gateway/app/view?") &&
Expand Down Expand Up @@ -238,7 +261,7 @@ class BangumiSeasonHook(classLoader: ClassLoader) : BaseHook(classLoader) {
hookAfterMethod("getCommentJumpUrl", hooker = urlHook)
}

if (sPrefs.getBoolean("hidden", false) && sPrefs.getBoolean("search_th", false)) {
if (sPrefs.getBoolean("hidden", false) && sPrefs.getBoolean("search_area", false)) {
"com.bilibili.bangumi.ui.page.search.BangumiSearchResultFragment".findClassOrNull(
mClassLoader
)?.run {
Expand All @@ -247,12 +270,18 @@ class BangumiSeasonHook(classLoader: ClassLoader) : BaseHook(classLoader) {
Boolean::class.javaPrimitiveType
) { param ->
param.thisObject.callMethodAs<Bundle>("getArguments").run {
if (getString("from") == "th") {
declaredFields.filter {
it.type == Int::class.javaPrimitiveType
}.forEach {
it.isAccessible = true
if (it.get(param.thisObject) == 7) it.set(param.thisObject, TH_TYPE)
val from = getString("from")
for (area in AREA_TYPES) {
if (from == area.value.type) {
declaredFields.filter {
it.type == Int::class.javaPrimitiveType
}.forEach {
it.isAccessible = true
if (it.get(param.thisObject) == 7) it.set(
param.thisObject,
area.key
)
}
}
}
}
Expand All @@ -264,14 +293,17 @@ class BangumiSeasonHook(classLoader: ClassLoader) : BaseHook(classLoader) {
mClassLoader
)
val pageArrays = pageTypesClass.getStaticObjectFieldAs<Array<Any>>("\$VALUES")
val newPageArray = pageArrays.copyOf(pageArrays.size + 1)
newPageArray[pageArrays.size] = pageTypesClass.new(
"PAGE_BANGUMI",
4,
"bilibili://search-result/new-bangumi?from=th",
TH_TYPE,
"bangumi"
)
val newPageArray = pageArrays.copyOf(pageArrays.size + 4)
var counter = 0
for (area in AREA_TYPES) {
newPageArray[pageArrays.size + counter++] = pageTypesClass.new(
"PAGE_BANGUMI",
4,
"bilibili://search-result/new-bangumi?from=" + area.value.type,
area.key,
"bangumi"
)
}
pageTypesClass.setStaticObjectField("\$VALUES", newPageArray)
}
}
Expand All @@ -286,11 +318,11 @@ class BangumiSeasonHook(classLoader: ClassLoader) : BaseHook(classLoader) {
) ?: data
}

private fun retrieveThailandSearch(data: Any?, url: String): Any? {
private fun retrieveAreaSearch(data: Any?, url: String, area: String): Any? {
data ?: return data
if (sPrefs.getBoolean("hidden", false) && sPrefs.getBoolean("search_th", false)) {
if (sPrefs.getBoolean("hidden", false) && sPrefs.getBoolean("search_area", false)) {
val content =
getThailandSearchBangumi(URL(URLDecoder.decode(url, Charsets.UTF_8.name())).query)
getAreaSearchBangumi(URL(URLDecoder.decode(url, Charsets.UTF_8.name())).query, area)
?: return data
val jsonContent = content.toJSONObject()
val newData = jsonContent.optJSONObject("data") ?: return data
Expand All @@ -304,16 +336,18 @@ class BangumiSeasonHook(classLoader: ClassLoader) : BaseHook(classLoader) {
}
}

private fun addThailandTag(body: Any?) {
private fun addAreaTags(body: Any?) {
body ?: return
if (sPrefs.getBoolean("hidden", false) && sPrefs.getBoolean("search_th", false)) {
searchAllResultNavInfoClass?.new()?.run {
setObjectField("name", "泰区")
setIntField("pages", 0)
setIntField("total", 0)
setIntField("type", TH_TYPE)
}?.also {
body.getObjectFieldAs<MutableList<Any>>("nav").add(1, it)
if (sPrefs.getBoolean("hidden", false) && sPrefs.getBoolean("search_area", false)) {
for (area in AREA_TYPES) {
searchAllResultNavInfoClass?.new()?.run {
setObjectField("name", area.value.text)
setIntField("pages", 0)
setIntField("total", 0)
setIntField("type", area.key)
}?.also {
body.getObjectFieldAs<MutableList<Any>>("nav").add(1, it)
}
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/me/iacn/biliroaming/network/BiliRoamingApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import java.util.concurrent.TimeUnit
object BiliRoamingApi {
private const val BILI_SEASON_URL = "api.bilibili.com/pgc/view/web/season"
private const val BILI_HIDDEN_SEASON_URL = "bangumi.bilibili.com/view/web_api/season"
private const val BILI_SEARCH_URL = "/x/v2/search/type"
private const val BILIPLUS_VIEW_URL = "www.biliplus.com/api/view"
private const val BILI_REVIEW_URL = "api.bilibili.com/pgc/review/user"
private const val BILI_USER_STATUS_URL = "api.bilibili.com/pgc/view/web/season/user/status"
Expand Down Expand Up @@ -141,6 +142,31 @@ object BiliRoamingApi {
return getContent(uri)
}

@JvmStatic
fun getAreaSearchBangumi(queryString: String, area: String): String? {
if (area == "th") {
return getThailandSearchBangumi(queryString)
}
val hostUrl = sPrefs.getString(area + "_server", null) ?: return null
val uri = Uri.Builder()
.scheme("https")
.encodedAuthority(hostUrl + BILI_SEARCH_URL)
.encodedQuery(
signQuery(
queryString, mapOf(
"type" to "7",
"appkey" to "1d8b6e7d45233436",
"build" to "6400000",
"mobi_app" to "android",
"platform" to "android",
"area" to area,
)
)
)
.toString()
return getContent(uri)
}

@JvmStatic
fun getThailandSearchBangumi(queryString: String): String? {
val thUrl = sPrefs.getString("th_server", null) ?: return null
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@
<string name="movie_title">影视</string>
<string name="activity_title">热点</string>
<string name="other_tabs_title">其它</string>
<string name="search_th_title">开启泰区番剧搜索</string>
<string name="search_th_summary">在搜索页面添加泰区番剧搜索结果,需要解析服务器支持</string>
<string name="search_area_title">开启其它地区番剧搜索</string>
<string name="search_area_summary">在搜索页面添加其它地区番剧搜索结果,需要解析服务器支持</string>
<string name="customize_bottom_bar_title">自定义底栏</string>
<string name="customize_bottom_bar_summary">至少保留一个元素(重启两次生效)</string>
<string name="customize_accessKey_title">自定义访问密钥</string>
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/xml/prefs_setting.xml
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@
android:title="@string/add_movie_title" />
<SwitchPreference
android:dependency="hidden"
android:key="search_th"
android:summary="@string/search_th_summary"
android:title="@string/search_th_title" />
android:key="search_area"
android:summary="@string/search_area_summary"
android:title="@string/search_area_title" />
<Preference
android:dependency="hidden"
android:key="customize_accessKey"
Expand Down

0 comments on commit 553d2c6

Please sign in to comment.