From 49abaf8a2d3820419f06aa70c8faff5bc4871b4f Mon Sep 17 00:00:00 2001 From: HuanCheng65 <22636177+HuanCheng65@users.noreply.github.com> Date: Sun, 8 Oct 2023 22:04:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=83=AD=E7=82=B9=E8=AF=9D=E9=A2=98?= =?UTF-8?q?=E8=AF=A6=E6=83=85=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tieba/post/api/HttpConstant.kt | 3 + .../tieba/post/api/models/TopicDetailBean.kt | 161 ++++++++++++++++++ .../post/api/retrofit/RetrofitTiebaApi.kt | 6 +- .../interceptors/StParamInterceptor.kt | 11 ++ .../retrofit/interfaces/AppHybridTiebaApi.kt | 28 +++ 5 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/huanchengfly/tieba/post/api/models/TopicDetailBean.kt diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/HttpConstant.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/HttpConstant.kt index 3cb02d9d..6b57350f 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/api/HttpConstant.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/HttpConstant.kt @@ -20,6 +20,9 @@ object Error { } object Header { + const val NO_ST_PARAMS = "no_st_params" + const val NO_ST_PARAMS_TRUE = "true" + const val FORCE_PARAM = "force_param" const val FORCE_PARAM_QUERY = "query" diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/models/TopicDetailBean.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/models/TopicDetailBean.kt new file mode 100644 index 00000000..a641d7b6 --- /dev/null +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/models/TopicDetailBean.kt @@ -0,0 +1,161 @@ +package com.huanchengfly.tieba.post.api.models + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class TopicDetailBean( + @SerialName("no") + val errorCode: Int, + @SerialName("error") + val errorMsg: String, + val data: TopicDetailDataBean, +) + +@Serializable +data class TopicDetailDataBean( + @SerialName("topic_info") + val topicInfo: TopicInfoBean, + val user: UserBean, + val tbs: String, + val relateForum: List, + @SerialName("special_topic") + val specialTopic: List, + @SerialName("relate_thread") + val relateThread: RelateThreadBean, + @SerialName("has_more") + val hasMore: Boolean, +) + +@Serializable +data class RelateThreadBean( + @SerialName("thread_list") + val threadList: List, +) + +@Serializable +data class ThreadBean( + @SerialName("feed_id") + val feedId: Long, + val source: Int, + @SerialName("thread_info") + val threadInfo: ThreadInfoBean, + @SerialName("user_agree") + val userAgree: Int, +) + +@Serializable +data class TopicInfoBean( + @SerialName("topic_id") + val topicId: String, + @SerialName("topic_name") + val topicName: String, + val candle: String, + @SerialName("topic_desc") + val topicDesc: String, + @SerialName("discuss_num") + val discussNum: String, + @SerialName("topic_image") + val topicImage: String, + @SerialName("share_title") + val shareTitle: String, + @SerialName("share_pic") + val sharePic: String, + @SerialName("is_video_topic") + val isVideoTopic: Int, +) + +@Serializable +data class UserBean( + @SerialName("is_login") + val isLogin: Boolean, + val id: Long, + val uid: Long, + val name: String, + @SerialName("name_show") + val nameShow: String, + @SerialName("portrait") + val portraitUrl: String, +) + +@Serializable +data class RelateForumBean( + @SerialName("forum_id") + val forumId: Long, + @SerialName("forum_name") + val forumName: String, + val avatar: String, + val desc: String, + @SerialName("member_num") + val memberNum: Long, + @SerialName("thread_num") + val threadNum: Long, + @SerialName("post_num") + val postNum: Long, +) + +@Serializable +data class SpecialTopicBean( + val title: String, + @SerialName("thread_list") + val threadList: List, +) + +@Serializable +data class ThreadInfoBean( + val id: Long, + @SerialName("feed_id") + val feedId: Long, + val title: String, + @SerialName("tid") + val threadId: Long, + @SerialName("forum_id") + val forumId: Long, + @SerialName("forum_name") + val forumName: String, + @SerialName("create_time") + val createTime: Long, + @SerialName("last_time") + val lastTime: String, + @SerialName("last_time_int") + val lastTimeInt: Long, + @SerialName("abstract") + val abstractText: String, + val media: List, + @SerialName("media_num") + val mediaNum: MediaNumBean, + @SerialName("agree_num") + val agreeNum: Long, + @SerialName("reply_num") + val replyNum: Long, + @SerialName("share_num") + val shareNum: Long, + @SerialName("user_id") + val userId: Long, + @SerialName("first_post_id") + val firstPostId: Long, + @SerialName("user_agree") + val userAgree: Int, +) + +@Serializable +data class MediaNumBean( + val pic: Int, +) + +@Serializable +data class MediaBean( + val type: String, + val width: String, + val height: String, + @SerialName("small_pic") + val smallPic: String, + @SerialName("big_pic") + val bigPic: String, + @SerialName("water_pic") + val waterPic: String, + @SerialName("is_long_pic") + val isLongPic: Int, + @SerialName("bsize") + val bSize: String, +) \ No newline at end of file diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/RetrofitTiebaApi.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/RetrofitTiebaApi.kt index 94966397..91915501 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/RetrofitTiebaApi.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/RetrofitTiebaApi.kt @@ -145,7 +145,11 @@ object RetrofitTiebaApi { ) } ), - AddWebCookieInterceptor + AddWebCookieInterceptor, + CommonParamInterceptor( + Param.BDUSS to { AccountUtil.getBduss() }, + Param.STOKEN to { AccountUtil.getSToken() }, + ) ) } diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/interceptors/StParamInterceptor.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/interceptors/StParamInterceptor.kt index d4a4f2ab..e0ba020a 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/interceptors/StParamInterceptor.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/interceptors/StParamInterceptor.kt @@ -17,6 +17,17 @@ class StParamInterceptor(private val method: Boolean = false) : Interceptor { var httpUrl = request.url var body = request.body + var addStParam = true + val noStParams = headers[Header.NO_ST_PARAMS] + if (noStParams != null) { + headers = headers.newBuilder().removeAll(Header.NO_ST_PARAMS).build() + addStParam = noStParams != Header.NO_ST_PARAMS_TRUE + } + + if (!addStParam) { + return chain.proceed(request.newBuilder().headers(headers).build()) + } + //是否强制加到 Query(暂不存在强制加到 FormBody 的情况) var forceQuery = false val forceParam = headers[Header.FORCE_PARAM] 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 34a08005..9c41698a 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 @@ -1,21 +1,33 @@ package com.huanchengfly.tieba.post.api.retrofit.interfaces +import com.huanchengfly.tieba.post.api.Param import com.huanchengfly.tieba.post.api.models.SearchForumBean import com.huanchengfly.tieba.post.api.models.SearchThreadBean import com.huanchengfly.tieba.post.api.models.SearchUserBean +import com.huanchengfly.tieba.post.api.models.TopicDetailBean import com.huanchengfly.tieba.post.api.urlEncode import kotlinx.coroutines.flow.Flow import retrofit2.http.GET import retrofit2.http.Header +import retrofit2.http.Headers import retrofit2.http.Query +import com.huanchengfly.tieba.post.api.Header as TiebaHeaders interface AppHybridTiebaApi { + @Headers( + "${TiebaHeaders.NO_ST_PARAMS}: ${TiebaHeaders.NO_ST_PARAMS_TRUE}", + "${TiebaHeaders.NO_COMMON_PARAMS}: ${Param.BDUSS},${Param.STOKEN}", + ) @GET("/mo/q/search/forum") fun searchForumFlow( @Query("word") keyword: String, @Header("Referer") referer: String = "https://tieba.baidu.com/mo/q/hybrid/search?keyword=$keyword&_webview_time=${System.currentTimeMillis()}".urlEncode(), ): Flow + @Headers( + "${TiebaHeaders.NO_ST_PARAMS}: ${TiebaHeaders.NO_ST_PARAMS_TRUE}", + "${TiebaHeaders.NO_COMMON_PARAMS}: ${Param.BDUSS},${Param.STOKEN}", + ) @GET("/mo/q/search/thread") fun searchThreadFlow( @Query("word") keyword: String, @@ -27,9 +39,25 @@ interface AppHybridTiebaApi { @Header("Referer") referer: String = "https://tieba.baidu.com/mo/q/hybrid/search?keyword=$keyword&_webview_time=${System.currentTimeMillis()}".urlEncode(), ): Flow + @Headers( + "${TiebaHeaders.NO_ST_PARAMS}: ${TiebaHeaders.NO_ST_PARAMS_TRUE}", + "${TiebaHeaders.NO_COMMON_PARAMS}: ${Param.BDUSS},${Param.STOKEN}", + ) @GET("/mo/q/search/user") fun searchUserFlow( @Query("word") keyword: String, @Header("Referer") referer: String = "https://tieba.baidu.com/mo/q/hybrid/search?keyword=$keyword&_webview_time=${System.currentTimeMillis()}".urlEncode(), ): Flow + + @GET("/mo/q/newtopic/topicDetail") + fun topicDetailFlow( + @Query("topic_id") topicId: String, + @Query("topic_name") topicName: String, + @Query("is_new") isNew: Int = 0, + @Query("is_share") isShare: Int = 1, + @Query("pn") page: Int, + @Query("rn") pageSize: Int = 10, + @Query("offset") offset: Int = 0, + @Query("derivative_to_pic_id") derivativeToPicId: String = "", + ): Flow } \ No newline at end of file