From d99f723cbc5ee316ef925184efc702e233fe85ee Mon Sep 17 00:00:00 2001 From: HuanCheng65 <22636177+HuanCheng65@users.noreply.github.com> Date: Sun, 28 Jan 2024 14:16:19 +0800 Subject: [PATCH] =?UTF-8?q?feat(API):=20=E6=94=AF=E6=8C=81=E6=96=B0?= =?UTF-8?q?=E7=89=88=E5=90=A7=E5=86=85=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../huanchengfly/tieba/post/api/Extensions.kt | 25 ++++--------------- .../tieba/post/api/interfaces/ITiebaApi.kt | 21 ++++++++++++++++ .../api/interfaces/impls/MixedTiebaApiImpl.kt | 23 +++++++++++++++++ .../retrofit/interfaces/AppHybridTiebaApi.kt | 5 +++- 4 files changed, 53 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/Extensions.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/Extensions.kt index 19556abf..96ca5800 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/api/Extensions.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/Extensions.kt @@ -3,27 +3,12 @@ package com.huanchengfly.tieba.post.api import com.huanchengfly.tieba.post.api.retrofit.body.MyMultipartBody import okhttp3.FormBody import okio.Buffer -import java.io.UnsupportedEncodingException import java.net.URLDecoder import java.net.URLEncoder -fun String.urlEncode(): String { - return try { - URLEncoder.encode(this, "UTF-8") - } catch (e: UnsupportedEncodingException) { - e.printStackTrace() - this - } -} +fun String.urlEncode(): String = runCatching { URLEncoder.encode(this, "UTF-8") }.getOrDefault(this) -fun String.urlDecode(): String { - return try { - URLDecoder.decode(this, "UTF-8") - } catch (e: UnsupportedEncodingException) { - e.printStackTrace() - this - } -} +fun String.urlDecode(): String = runCatching { URLDecoder.decode(this, "UTF-8") }.getOrDefault(this) fun FormBody.containsEncodedName(name: String): Boolean { repeat(size) { @@ -49,7 +34,7 @@ fun FormBody.raw() = }.toString() fun FormBody.sortedEncodedRaw(separator: Boolean = true): String { - val nameAndValue = ArrayList() + val nameAndValue = mutableListOf() repeat(size) { nameAndValue.add("${encodedName(it)}=${encodedValue(it)}") } @@ -58,7 +43,7 @@ fun FormBody.sortedEncodedRaw(separator: Boolean = true): String { } fun FormBody.sortedRaw(separator: Boolean = true): String { - val nameAndValue = ArrayList() + val nameAndValue = mutableListOf() repeat(size) { nameAndValue.add("${name(it)}=${value(it)}") } @@ -116,7 +101,7 @@ fun MyMultipartBody.newBuilder(): MyMultipartBody.Builder = fun MyMultipartBody.sort(): MyMultipartBody { val builder = newBuilder() - var fileParts = mutableListOf() + val fileParts = mutableListOf() parts.forEach { if (it.fileName() != null) { fileParts.add(it) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/ITiebaApi.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/ITiebaApi.kt index 0c4e63e5..4bff981f 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/ITiebaApi.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/ITiebaApi.kt @@ -947,6 +947,27 @@ interface ITiebaApi { sort: Int, ): Flow + /** + * 吧内搜索 + * + * @param keyword 搜索关键词 + * @param forumName 搜索吧名 + * @param forumId 搜索吧 ID + * @param sortType 排序模式(1 = 时间倒序,2 = 相关性排序) + * @param filterType 过滤(1 = 全部,2 = 仅看主题贴) + * @param page 分页页码(从 1 开始) + * @param pageSize 每页贴数(默认 20) + */ + fun searchPostFlow( + keyword: String, + forumName: String, + forumId: Long, + sortType: Int = 1, + filterType: Int = 1, + page: Int = 1, + pageSize: Int = 20, + ): Flow + /** * 上传图片(web 接口) * diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/impls/MixedTiebaApiImpl.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/impls/MixedTiebaApiImpl.kt index 00a6d2ae..21b3ff7f 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/impls/MixedTiebaApiImpl.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/impls/MixedTiebaApiImpl.kt @@ -118,6 +118,7 @@ import com.huanchengfly.tieba.post.models.MyInfoBean import com.huanchengfly.tieba.post.models.PhotoInfoBean import com.huanchengfly.tieba.post.toJson import com.huanchengfly.tieba.post.utils.AccountUtil +import com.huanchengfly.tieba.post.utils.CuidUtils import com.huanchengfly.tieba.post.utils.ImageUtil import kotlinx.coroutines.Deferred import kotlinx.coroutines.flow.Flow @@ -671,6 +672,28 @@ object MixedTiebaApiImpl : ITiebaApi { sort ) + override fun searchPostFlow( + keyword: String, + forumName: String, + forumId: Long, + sortType: Int, + filterType: Int, + page: Int, + pageSize: Int, + ): Flow = + RetrofitTiebaApi.HYBRID_TIEBA_API.searchThreadFlow( + keyword, + page, + sortType, + filterType, + pageSize, + forumName, + ct = 2, + isUseZonghe = null, + clientVersion = ClientVersion.TIEBA_V12.version, + referer = "https://tieba.baidu.com/mo/q/hybrid-usergrow-search/searchGlobal?entryPage=frs&loadingSignal=1&forumName=${forumName.urlEncode()}&forumId=$forumId&customfullscreen=1&nonavigationbar=1&cuid=${CuidUtils.getNewCuid()}&cuid_galaxy2=${CuidUtils.getNewCuid()}&cuid_gid=×tamp=${System.currentTimeMillis()}&_client_version=${ClientVersion.TIEBA_V12.version}&_client_type=2" + ) + override fun webUploadPic(photoInfoBean: PhotoInfoBean): Call { var base64: String? = null if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/interfaces/AppHybridTiebaApi.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/interfaces/AppHybridTiebaApi.kt index 9c41698a..cbe4a3f8 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/interfaces/AppHybridTiebaApi.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/interfaces/AppHybridTiebaApi.kt @@ -34,8 +34,11 @@ interface AppHybridTiebaApi { @Query("pn") page: Int, @Query("st") sort: Int, @Query("tt") filter: Int = 1, + @Query("rn") pageSize: Int? = null, + @Query("fname") forumName: String? = null, @Query("ct") ct: Int = 1, - @Query("cv") cv: String = "99.9.101", + @Query("is_use_zonghe") isUseZonghe: Int? = 1, + @Query("cv") clientVersion: String = "99.9.101", @Header("Referer") referer: String = "https://tieba.baidu.com/mo/q/hybrid/search?keyword=$keyword&_webview_time=${System.currentTimeMillis()}".urlEncode(), ): Flow