Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/aaa1115910/bv into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ldm0206 committed Dec 16, 2024
2 parents 7c305dc + 5f2bf8a commit 3570465
Show file tree
Hide file tree
Showing 22 changed files with 180 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,10 @@
-dontwarn com.google.protobuf.GeneratedMessageV3$FieldAccessorTable
-dontwarn com.google.protobuf.GeneratedMessageV3
-dontwarn com.google.protobuf.RepeatedFieldBuilderV3

# kotlin-logging
-dontwarn ch.qos.logback.classic.Level
-dontwarn ch.qos.logback.classic.Logger
-dontwarn ch.qos.logback.classic.spi.ILoggingEvent
-dontwarn ch.qos.logback.classic.spi.LogbackServiceProvider
-dontwarn ch.qos.logback.classic.spi.LoggingEvent
2 changes: 1 addition & 1 deletion app/src/main/kotlin/coil/transform/BlurTransformation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class BlurTransformation @JvmOverloads constructor(

val scaledWidth = (input.width / sampling).toInt()
val scaledHeight = (input.height / sampling).toInt()
val output = createBitmap(scaledWidth, scaledHeight, input.config)
val output = createBitmap(scaledWidth, scaledHeight, input.config!!)
output.applyCanvas {
scale(1 / sampling, 1 / sampling)
drawBitmap(input, 0f, 0f, paint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ fun LibVLCDownloaderDialog(
release,
tempFile,
object : ProgressListener {
override suspend fun invoke(downloaded: Long, total: Long) {
text = "正在下载(${downloaded / total.toFloat() * 100}%)"
override suspend fun onProgress(downloaded: Long, total: Long?) {
text = "正在下载(${downloaded / (total?.toFloat() ?: 0f) * 100}%)"
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ fun UpdateDialog(
latestReleaseBuild!!,
tempFile,
object : ProgressListener {
override suspend fun invoke(downloaded: Long, total: Long) {
override suspend fun onProgress(downloaded: Long, total: Long?) {
bytesSentTotal = downloaded
contentLength = total
contentLength = total ?: 0
targetProgress =
runCatching { bytesSentTotal.toFloat() / contentLength }
.getOrDefault(0f)
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/kotlin/dev/aaa1115910/bv/network/HttpServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import io.ktor.http.ContentDisposition
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.Application
import io.ktor.server.application.call
import io.ktor.server.cio.CIO
import io.ktor.server.engine.ApplicationEngine
import io.ktor.server.cio.CIOApplicationEngine
import io.ktor.server.engine.EmbeddedServer
import io.ktor.server.engine.embeddedServer
import io.ktor.server.response.header
import io.ktor.server.response.respondFile
Expand All @@ -20,7 +20,7 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

object HttpServer {
var server: ApplicationEngine? = null
var server: EmbeddedServer<CIOApplicationEngine, CIOApplicationEngine.Configuration>? = null

@OptIn(DelicateCoroutinesApi::class)
fun startServer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fun LogsScreen(

LaunchedEffect(Unit) {
host = getIpAddress()
port = HttpServer.server?.resolvedConnectors()?.first()?.port ?: 0
port = HttpServer.server?.engine?.resolvedConnectors()?.first()?.port ?: 0

updateLogs()
}
Expand Down
2 changes: 1 addition & 1 deletion bili-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies {
implementation(libs.ktor.client.content.negotiation)
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.encoding)
implementation(libs.ktor.jsoup)
//implementation(libs.ktor.jsoup)
implementation(libs.ktor.client.okhttp)
implementation(libs.ktor.client.serialization.kotlinx)
implementation(libs.logging)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
@file:Suppress("unused")

package com.tfowl.ktor.client.plugins

import io.ktor.client.*
import io.ktor.client.plugins.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.util.*
import io.ktor.utils.io.*
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.parser.Parser

/**
* [HttpClient] plugin that parses response bodies into Jsoup [Document]
* class using a provided [Parser]
*
* By default,
*
* [ContentType.Text.Html] is parsed using [Parser.htmlParser].
*
* [ContentType.Text.Xml] & [ContentType.Application.Xml] are parsed using [Parser.xmlParser].
*
* Note: It will only parse registered content types and for receiving
* [Document] or superclasses.
*
* @property parsers Registered parsers for content types
*/
class JsoupPlugin internal constructor(val parsers: Map<ContentType, Parser>) {

/**
* [JsoupPlugin] configuration that is used during installation
*/
class Config {

/**
* [Parsers][Parser] that will be used for each [ContentType]
*
* Defaults:
* - Html: [ContentType.Text.Html]
* - Xml: [ContentType.Text.Xml] and [ContentType.Application.Xml]
*/
var parsers = mutableMapOf(
ContentType.Text.Html to Parser.htmlParser(),
ContentType.Text.Xml to Parser.xmlParser(),
ContentType.Application.Xml to Parser.xmlParser()
)
}

/**
* Companion object for plugin installation
*/
companion object Plugin : HttpClientPlugin<Config, JsoupPlugin> {
override val key: AttributeKey<JsoupPlugin> = AttributeKey("Jsoup")

override fun prepare(block: Config.() -> Unit): JsoupPlugin =
JsoupPlugin(Config().apply(block).parsers)

override fun install(plugin: JsoupPlugin, scope: HttpClient) {
scope.responsePipeline.intercept(HttpResponsePipeline.Transform) { (info, body) ->
if (body !is ByteReadChannel)
return@intercept

if (!info.type.java.isAssignableFrom(Document::class.java))
return@intercept

val responseContentType = context.response.contentType() ?: return@intercept

val parser = plugin.parsers.firstNotNullOfOrNull { (type, parser) ->
parser.takeIf { responseContentType.match(type) }
} ?: return@intercept

val bodyContent = body.readRemaining().readText()
val baseUri = context.request.url.toString()

/* Jsoup Parsers internally contain a stateful TreeBuilder,
We need to create a deep copy to avoid issues with
concurrency */
val document = Jsoup.parse(bodyContent, baseUri, parser.newInstance())
proceedWith(HttpResponseContainer(info, document))
}
}
}
}

/**
* Install [JsoupPlugin]
*/
@Suppress("FunctionName")
fun HttpClientConfig<*>.Jsoup(block: JsoupPlugin.Config.() -> Unit = {}) {
install(JsoupPlugin, block)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ data class CarouselData(
title = it.title,
seasonId = it.seasonId ?: -1,
episodeId = it.episodeId
?: Url(it.link).pathSegments.last().substring(2).toInt()
?: Url(it.link).rawSegments.last().substring(2).toInt()
)
)
}
Expand Down Expand Up @@ -57,7 +57,7 @@ data class CarouselData(
CarouselItem(
cover = item.pic,
title = item.title,
bvid = Url(item.url).pathSegments.last()
bvid = Url(item.url).rawSegments.last()
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ data class UgcItem(
fun fromRcmdItem(rcmdItem: RcmdIndexData.RcmdItem) =
UgcItem(
aid = rcmdItem.args.aid ?: 0,
title = rcmdItem.title,
cover = rcmdItem.cover,
title = rcmdItem.title!!,
cover = rcmdItem.cover!!,
author = rcmdItem.args.upName ?: "",
play = with(rcmdItem.coverLeftText1) {
runCatching {
if (this.endsWith("")) {
if (this!!.endsWith("")) {
(this.substring(0, this.length - 1).toDouble() * 10000).toInt()
} else {
this.toInt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ data class DynamicVideo(
aid = archive.avid,
bvid = archive.bvid,
cid = archive.cid,
title = if (!isDynamicVideo) archive.title else desc!!.text.substring(5),
title = if (!isDynamicVideo) archive.title
else desc?.text?.replace("动态视频|", "") ?: "",
cover = archive.cover,
author = author.author.name,
duration = convertStringTimeToSeconds(archive.coverLeftText1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,11 @@ import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.client.statement.bodyAsChannel
import io.ktor.client.statement.bodyAsText
import io.ktor.client.statement.readBytes
import io.ktor.client.statement.readRawBytes
import io.ktor.http.Parameters
import io.ktor.http.URLProtocol
import io.ktor.serialization.kotlinx.json.json
import io.ktor.util.InternalAPI
import io.ktor.util.toByteArray
import io.ktor.utils.io.InternalAPI
import io.ktor.utils.io.jvm.javaio.toInputStream
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -1131,7 +1130,7 @@ object BiliHttpApi {
parameter("main_ver", mainVer)
highlight?.let { parameter("highlight", it) }
parameter("buvid", buvid)
}.content.toByteArray().toString(Charsets.UTF_8)
}.readRawBytes().toString(Charsets.UTF_8)
val keywordSuggest = json.decodeFromString<KeywordSuggest>(responseText)
val result = json.decodeFromJsonElement<KeywordSuggest.Result>(keywordSuggest.result!!)
keywordSuggest.suggests.addAll(result.tag)
Expand Down Expand Up @@ -1521,7 +1520,7 @@ object BiliHttpApi {
)

suspend fun download(url: String): ByteArray {
return client.get(url).readBytes()
return client.get(url).readRawBytes()
}

suspend fun getWebVideoShot(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,26 @@ data class RcmdIndexData(

@Serializable
data class RcmdItem(
//@SerialName("ad_info")
//val adInfo: AdInfo,
val args: Args,
@SerialName("can_play")
val canPlay: Int? = null,
@SerialName("card_goto")
val cardGoto: String,
@SerialName("card_type")
val cardType: String,
val cover: String,
val cover: String? = null,
@SerialName("cover_left_1_content_description")
val coverLeft1ContentDescription: String,
val coverLeft1ContentDescription: String? = null,
@SerialName("cover_left_2_content_description")
val coverLeft2ContentDescription: String? = null,
@SerialName("cover_left_icon_1")
val coverLeftIcon1: Int,
val coverLeftIcon1: Int? = null,
@SerialName("cover_left_icon_2")
val coverLeftIcon2: Int? = null,
@SerialName("cover_left_text_1")
val coverLeftText1: String,
val coverLeftText1: String? = null,
@SerialName("cover_left_text_2")
val coverLeftText2: String? = null,
@SerialName("cover_right_content_description")
Expand All @@ -91,14 +93,14 @@ data class RcmdIndexData(
val descButton: DescButton? = null,
@SerialName("ff_cover")
val ffCover: String? = null,
val goto: String,
val goto: String? = null,
@SerialName("goto_icon")
val gotoIcon: GotoIcon? = null,
val idx: Int,
@SerialName("official_icon")
val officialIcon: Int? = null,
@SerialName("param")
val `param`: String,
val `param`: String? = null,
@SerialName("player_args")
val playerArgs: PlayerArgs? = null,
@SerialName("rcmd_reason")
Expand All @@ -113,10 +115,10 @@ data class RcmdIndexData(
val threePoint: ThreePoint? = null,
@SerialName("three_point_v2")
val threePointV2: List<ThreePointV2>,
val title: String,
val title: String? = null,
@SerialName("track_id")
val trackId: String? = null,
val uri: String
val uri: String? = null
) {
@Serializable
data class Args(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ package dev.aaa1115910.biliapi.http.entity.live

import io.ktor.utils.io.core.ByteReadPacket
import io.ktor.utils.io.core.buildPacket
import io.ktor.utils.io.core.readInt
import io.ktor.utils.io.core.readShort
import io.ktor.utils.io.core.toByteArray
import io.ktor.utils.io.core.writeInt
import io.ktor.utils.io.core.writeShort
import io.ktor.utils.io.core.writePacket
import kotlinx.io.Source
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.io.ByteArrayInputStream
import java.io.DataInputStream

internal fun ByteReadPacket.readFrameHeader(): FrameHeader = FrameHeader(
internal fun Source.readFrameHeader(): FrameHeader = FrameHeader(
readInt(), readShort(), readShort(), readInt(), readInt()
)

Expand All @@ -37,7 +35,7 @@ data class FrameHeader(
val sequence: Int
) {
val dataLength get() = totalLength - headerLength
fun toBinary(): ByteReadPacket {
fun toBinary(): Source {
return buildPacket {
writeInt(this@FrameHeader.totalLength)
writeShort(headerLength)
Expand All @@ -53,7 +51,7 @@ enum class FrameType(val code: Int) {
}

interface RequestFrame {
fun toBinary(): ByteReadPacket
fun toBinary(): Source
}

@Serializable
Expand All @@ -67,7 +65,7 @@ data class AuthRequest(
val type: Int = 2,
val key: String = ""
) : RequestFrame {
override fun toBinary(): ByteReadPacket {
override fun toBinary(): Source {
val data = Json.encodeToString(this).toByteArray()
val header = FrameHeader(
totalLength = data.size + 16,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object UrlUtil {
if (url.startsWith("bilibili://video/")) {
return url.split("/").last().toLong()
} else {
val pathSegments = Url(url).pathSegments
val pathSegments = Url(url).rawSegments
val videoSegmentIndex = pathSegments.indexOf("video")
val videoId = pathSegments[videoSegmentIndex + 1]
return if (videoId.startsWith("BV")) {
Expand Down
Loading

0 comments on commit 3570465

Please sign in to comment.