Skip to content

Commit

Permalink
[修复]修复视频无法点赞投币问题
Browse files Browse the repository at this point in the history
[修复]修复Cookie风控问题
  • Loading branch information
1250422131 committed Jan 7, 2024
1 parent 7a5a1e5 commit 5bfe4c9
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.imcys.bilibilias.base.model.user

import kotlinx.serialization.Serializable

@Serializable
data class LikeVideoBean(
val code: Int,
val message: String,
Expand Down
175 changes: 92 additions & 83 deletions app/src/main/java/com/imcys/bilibilias/base/network/NetworkService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.imcys.bilibilias.common.base.model.user.MyUserData
import com.imcys.bilibilias.common.base.model.user.ResponseResult
import com.imcys.bilibilias.common.base.model.user.UserBiliBiliCookieModel
import com.imcys.bilibilias.common.base.utils.http.KtHttpUtils
import com.imcys.bilibilias.common.di.AsCookiesStorage
import com.imcys.bilibilias.home.ui.model.BangumiPlayBean
import com.imcys.bilibilias.home.ui.model.BangumiSeasonBean
import com.imcys.bilibilias.home.ui.model.CollectionDataBean
Expand Down Expand Up @@ -45,11 +46,14 @@ import com.imcys.bilibilias.home.ui.viewmodel.AsLoginBsViewModel
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.request.forms.submitForm
import io.ktor.client.request.get
import io.ktor.client.request.header
import io.ktor.client.request.parameter
import io.ktor.client.request.post
import io.ktor.client.statement.readBytes
import io.ktor.http.HttpHeaders
import io.ktor.http.parameters
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
Expand All @@ -61,8 +65,9 @@ import kotlin.reflect.full.createType

@Singleton
class NetworkService @Inject constructor(
private val ktHttpUtils: KtHttpUtils,
private val httpClient: HttpClient
private val ktHttpUtils: KtHttpUtils,
private val httpClient: HttpClient,
private val asCookiesStorage: AsCookiesStorage
) {
private val ioDispatcher = Dispatchers.IO
suspend fun n1(cid: Long, qn: Int): DashBangumiPlayBean = runCatchingOnWithContextIo {
Expand All @@ -73,7 +78,7 @@ class NetworkService @Inject constructor(

// ---------------------------------------------------------------------------------------------
suspend fun n2(bvid: String, cid: Long, qn: Int): DashVideoPlayBean =
runCatchingOnWithContextIo { videoPlayPath(bvid, cid.toString(), qn) }
runCatchingOnWithContextIo { videoPlayPath(bvid, cid.toString(), qn) }

suspend fun n10(bvid: String, cid: Long): DashVideoPlayBean = runCatchingOnWithContextIo {
videoPlayPath(bvid, cid.toString(), 64)
Expand All @@ -84,24 +89,24 @@ class NetworkService @Inject constructor(
}

suspend fun getDashBangumiPlay(cid: Long, qn: Int): DashBangumiPlayBean =
runCatchingOnWithContextIo {
httpClient.get("${ROAM_API}pgc/player/web/playurl?cid=$cid&qn=$qn&fnval=4048&fourk=1") {
refererBILIHarder()
}.body()
}
runCatchingOnWithContextIo {
httpClient.get("${ROAM_API}pgc/player/web/playurl?cid=$cid&qn=$qn&fnval=4048&fourk=1") {
refererBILIHarder()
}.body()
}

suspend fun getPlayHistory(max: Long, viewAt: Long, type: String): PlayHistoryBean =
runCatchingOnWithContextIo {
httpClient.get("${BilibiliApi.userPlayHistoryPath}?max=$max&view_at=$viewAt&type=archive")
.body()
}
runCatchingOnWithContextIo {
httpClient.get("${BilibiliApi.userPlayHistoryPath}?max=$max&view_at=$viewAt&type=archive")
.body()
}


private suspend inline fun <reified T> videoPlayPath(
bvid: String,
cid: String,
qn: Int,
fnval: Int = 4048,
bvid: String,
cid: String,
qn: Int,
fnval: Int = 4048,
): T = runCatchingOnWithContextIo {
httpClient.get(BilibiliApi.videoPlayPath) {
refererBILIHarder()
Expand All @@ -114,22 +119,22 @@ class NetworkService @Inject constructor(
}

private fun HttpRequestBuilder.parameterBVID(bvid: String): Unit =
url.parameters.append("bvid", bvid)
url.parameters.append("bvid", bvid)

private fun HttpRequestBuilder.parameterCID(cid: String): Unit =
url.parameters.append("cid", cid)
url.parameters.append("cid", cid)

private fun HttpRequestBuilder.parameterUpMID(upMid: String): Unit =
url.parameters.append("up_mid", upMid)
url.parameters.append("up_mid", upMid)

private fun HttpRequestBuilder.refererBILIHarder(): Unit =
header(HttpHeaders.Referrer, BILIBILI_URL)
header(HttpHeaders.Referrer, BILIBILI_URL)

private fun HttpRequestBuilder.parameterMID(mid: String): Unit =
url.parameters.append("mid", mid)
url.parameters.append("mid", mid)

private fun HttpRequestBuilder.parameterEpID(epid: String): Unit =
url.parameters.append("ep_id", epid)
url.parameters.append("ep_id", epid)

// ---------------------------------------------------------------------------------------------
suspend fun n3(bvid: String, cid: Long, qn: Int): VideoPlayBean = runCatchingOnWithContextIo {
Expand Down Expand Up @@ -175,6 +180,10 @@ class NetworkService @Inject constructor(
httpClient.get(BilibiliApi.getMyUserData).body()
}

suspend fun getBILIHome(): String = runCatchingOnWithContextIo {
httpClient.get(BILIBILI_URL).body()
}

suspend fun biliUserLogin(qrcodeKey: String): LoginStateBean = runCatchingOnWithContextIo {
httpClient.get(BilibiliApi.getLoginStatePath + "?qrcode_key=" + qrcodeKey).body()
}
Expand All @@ -198,10 +207,10 @@ class NetworkService @Inject constructor(
}

suspend fun getUserCollection(id: Long, pn: Int): CollectionDataBean =
runCatchingOnWithContextIo {
httpClient.get("${BilibiliApi.userCollectionDataPath}?media_id=${id}&pn=${pn}&ps=20")
.body()
}
runCatchingOnWithContextIo {
httpClient.get("${BilibiliApi.userCollectionDataPath}?media_id=${id}&pn=${pn}&ps=20")
.body()
}

suspend fun getDonateData(): OldDonateBean = runCatchingOnWithContextIo {
httpClient.get("${BiliBiliAsApi.appFunction}?type=Donate").body()
Expand All @@ -212,15 +221,15 @@ class NetworkService @Inject constructor(
}

suspend fun getBangumiFollow(vmid: Long, type: Int, pn: Int, ps: Int): BangumiFollowList =
runCatchingOnWithContextIo {
httpClient.get("${BilibiliApi.bangumiFollowPath}?vmid=$vmid&type=$type&pn=$pn&ps=$ps")
.body()
runCatchingOnWithContextIo {
httpClient.get("${BilibiliApi.bangumiFollowPath}?vmid=$vmid&type=$type&pn=$pn&ps=$ps")
.body()

}
}

suspend fun getUpdateData(): OldUpdateDataBean = runCatchingOnWithContextIo {
httpClient.get("${BiliBiliAsApi.updateDataPath}?type=json&version=${BiliBiliAsApi.version}")
.body()
.body()
}


Expand Down Expand Up @@ -334,86 +343,86 @@ class NetworkService @Inject constructor(
}

// ---------------------------------------------------------------------------------------------
suspend fun n31(bvid: String): LikeVideoBean = runCatchingOnWithContextIo {
ktHttpUtils.addHeader(
COOKIE,
BaseApplication.dataKv.decodeString(COOKIES, "")!!,
)
.addParam("csrf", BaseApplication.dataKv.decodeString("bili_jct", "")!!)
.addParam("like", "1")
.addParam("bvid", bvid)
.asyncPost(BilibiliApi.videLikePath)
suspend fun videoLike(bvid: String): LikeVideoBean = runCatchingOnWithContextIo {
httpClient.submitForm(
url = BilibiliApi.videLikePath,
formParameters = parameters {
append("csrf", asCookiesStorage.getCookieValue("bili_jct") ?: "")
append("like", "1")
append("bvid", bvid)
}
).body()
}

suspend fun n32(bvid: String): LikeVideoBean = runCatchingOnWithContextIo {
ktHttpUtils.addHeader(
COOKIE,
BaseApplication.dataKv.decodeString(COOKIES, "")!!,
COOKIE,
BaseApplication.dataKv.decodeString(COOKIES, "")!!,
)
.addParam("csrf", BaseApplication.dataKv.decodeString("bili_jct", "") ?: "")
.addParam("like", "2")
.addParam("bvid", bvid)
.asyncPost(BilibiliApi.videLikePath)
.addParam("csrf", asCookiesStorage.getCookieValue("bili_jct") ?: "")
.addParam("like", "2")
.addParam("bvid", bvid)
.asyncPost(BilibiliApi.videLikePath)
}

suspend fun n33(bvid: String): VideoCoinAddBean = runCatchingOnWithContextIo {
ktHttpUtils
.addHeader(COOKIE, BaseApplication.dataKv.decodeString(COOKIES, "")!!)
.addParam("bvid", bvid)
.addParam("multiply", "2")
.addParam("csrf", BaseApplication.dataKv.decodeString("bili_jct", "")!!)
.asyncPost(BilibiliApi.videoCoinAddPath)
.addHeader(COOKIE, asCookiesStorage.getCookieValue("bili_jct") ?: "")
.addParam("bvid", bvid)
.addParam("multiply", "2")
.addParam("csrf", BaseApplication.dataKv.decodeString("bili_jct", "")!!)
.asyncPost(BilibiliApi.videoCoinAddPath)
}

suspend fun n35(toString: String, addMediaIds: String): CollectionResultBean =
runCatchingOnWithContextIo {
httpClient.get(BilibiliApi.videoCollectionSetPath) {
parameter("rid", toString)
parameter("add_media_ids", addMediaIds)
parameter("csrf", BaseApplication.dataKv.decodeString("bili_jct", "")!!)
parameter("type", "2")
}.body()
}
runCatchingOnWithContextIo {
httpClient.get(BilibiliApi.videoCollectionSetPath) {
parameter("rid", toString)
parameter("add_media_ids", addMediaIds)
parameter("csrf", asCookiesStorage.getCookieValue("bili_jct") ?: "")
parameter("type", "2")
}.body()
}

suspend fun n36(
asCookie: String?,
asLoginInfo: AsLoginBsViewModel.AsLoginInfo
asCookie: String?,
asLoginInfo: AsLoginBsViewModel.AsLoginInfo
): AsUserLoginModel =
runCatchingOnWithContextIo {
ktHttpUtils.addHeader(COOKIE, asCookie!!).asyncPostJson(
"${BiliBiliAsApi.serviceTestApi}users/login",
asLoginInfo,
)
}
runCatchingOnWithContextIo {
ktHttpUtils.addHeader(COOKIE, asCookie!!).asyncPostJson(
"${BiliBiliAsApi.serviceTestApi}users/login",
asLoginInfo,
)
}

suspend fun n37(asCookie: String?): UserBiliBiliCookieModel = runCatchingOnWithContextIo {
ktHttpUtils.addHeader(COOKIE, asCookie!!)
.asyncGet("${BiliBiliAsApi.serviceTestApi}BiliBiliCookie")
.asyncGet("${BiliBiliAsApi.serviceTestApi}BiliBiliCookie")
}

suspend fun n39(asCookie: String?, data: UserBiliBiliCookieModel.Data): ResponseResult =
runCatchingOnWithContextIo {
ktHttpUtils.addHeader(COOKIE, asCookie!!).asyncDeleteJson(
"${BiliBiliAsApi.serviceTestApi}BiliBiliCookie",
data,
)
}
runCatchingOnWithContextIo {
ktHttpUtils.addHeader(COOKIE, asCookie!!).asyncDeleteJson(
"${BiliBiliAsApi.serviceTestApi}BiliBiliCookie",
data,
)
}

suspend fun n41(): LoginQrcodeBean =
runCatchingOnWithContextIo { ktHttpUtils.asyncGet(BilibiliApi.getLoginQRPath) }
runCatchingOnWithContextIo { ktHttpUtils.asyncGet(BilibiliApi.getLoginQRPath) }

suspend fun n43(biliBiliCookieInfo: AsLoginBsViewModel.BiliBiliCookieInfo): BiLiCookieResponseModel =
runCatchingOnWithContextIo {
runCatchingOnWithContextIo {

ktHttpUtils.addHeader(COOKIE, BaseApplication.asUser.asCookie)
.asyncPostJson(
"${BiliBiliAsApi.serviceTestApi}BiliBiliCookie",
biliBiliCookieInfo,
)
}
ktHttpUtils.addHeader(COOKIE, BaseApplication.asUser.asCookie)
.asyncPostJson(
"${BiliBiliAsApi.serviceTestApi}BiliBiliCookie",
biliBiliCookieInfo,
)
}

private suspend inline fun <reified T> runCatchingOnWithContextIo(
noinline block: suspend CoroutineScope.() -> T
noinline block: suspend CoroutineScope.() -> T
): T {
return runCatching {
withContext(ioDispatcher, block)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ object DialogUtils {
loginQrcodeBean: LoginQrcodeBean,
responseResult: (Int, LoginStateBean) -> Unit,
): BottomSheetDialog {

val binding: DialogLoginQrBottomsheetBinding =
DialogLoginQrBottomsheetBinding.inflate(LayoutInflater.from(context))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ class HomeFragment : BaseFragment() {
internal fun loadLogin() {
launchUI {

//解除风控
networkService.getBILIHome()

//自己会切换IO
val loginQRData = networkService.getLoginQRData()
.apply { data.url = URLEncoder.encode(data.url, "UTF-8") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ data class DashBangumiPlayBean(
}


fun DashBangumiPlayBean.toDashVideoPlayBean(): DashVideoPlayBean {
fun DashBangumiPlayBean.toDashVideoPlayBean(): DashVideoPlayBean {
val json = Json {
prettyPrint = true
isLenient = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,24 +407,22 @@ class AsVideoViewModel @Inject constructor(private val danmakuRepository: Danmak
fun likeVideo(view: View, bvid: String) {
val context = view.context

viewModelScope.launchIO {
val likeVideoBean = networkService.n31(bvid)
viewModelScope.launchUI {
val likeVideoBean = networkService.videoLike(bvid)

if ((context as AsVideoActivity).binding.archiveHasLikeBean?.data == 0) {
launchUI {
when (likeVideoBean.code) {
0 -> {
context.binding.archiveHasLikeBean?.data = 1
context.binding.asVideoLikeBt.isSelected = true
}
when (likeVideoBean.code) {
0 -> {
context.binding.archiveHasLikeBean?.data = 1
context.binding.asVideoLikeBt.isSelected = true
}

65006 -> {
cancelLikeVideo(view, bvid)
}
65006 -> {
cancelLikeVideo(view, bvid)
}

else -> {
asToast(context, likeVideoBean.message)
}
else -> {
asToast(context, likeVideoBean.message)
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ data class AsCookie(
val httpOnly: Boolean = false,
)

fun Cookie.toAsCookie(): AsCookie =
fun Cookie.toAsCookie(): AsCookie =
AsCookie(
name = name,
value = value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ const val SET_COOKIE = "Set-Cookie"

const val COOKIE = "cookie"

const val COOKIES = "cookies"
const val COOKIES = "cookies"

const val REFERER = "Referer"
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fun CoroutineScope.launchIO(
return this.launch(Dispatchers.IO, start, block)
}

fun CoroutineScope.launchUI(
fun CoroutineScope.launchUI(
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> Unit,
): Job {
Expand Down
Loading

0 comments on commit 5bfe4c9

Please sign in to comment.