Skip to content

Commit

Permalink
Merge pull request #253 from xyoye/dev_4.2.0
Browse files Browse the repository at this point in the history
Dev 4.2.0
  • Loading branch information
xyoye authored Sep 2, 2024
2 parents b60bd02 + 365e24b commit 7b357a6
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,8 @@ object AppConfigTable {
@MMKVFiled
//支持的视频后缀
var supportVideoExtension: String = VideoExtension.supportText

@MMKVFiled
// Jsoup的User-Agent
var jsoupUserAgent: String = DefaultConfig.DEFAULT_JSOUP_USER_AGENT
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.xyoye.common_component.base.app.BaseApplication
*/
object DefaultConfig {

// 默认缓存路径
val DEFAULT_CACHE_PATH: String by lazy {
val context = BaseApplication.getAppContext()
val externalFilesDir = context.getExternalFilesDir(null)
Expand All @@ -23,4 +24,7 @@ object DefaultConfig {
}
})
}

// 默认Jsoup的User-Agent
const val DEFAULT_JSOUP_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.xyoye.common_component.weight.dialog

import android.text.InputType
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import com.xyoye.common_component.R
import com.xyoye.common_component.databinding.DialogCommonEditBinding
import com.xyoye.common_component.utils.hideKeyboard
Expand Down Expand Up @@ -39,7 +40,7 @@ class CommonEditDialog(

setPositiveListener {
val result = binding.inputEt.text.toString().trim()
if (result.isEmpty()) {
if (editBean.canInputEmpty.not() && result.isEmpty()) {
ToastCenter.showWarning(editBean.emptyWarningMsg)
return@setPositiveListener
}
Expand All @@ -59,6 +60,9 @@ class CommonEditDialog(
binding.inputEt.setText(editBean.defaultText)
binding.inputEt.hint = editBean.hint
binding.inputEt.postDelayed({ showKeyboard(binding.inputEt) }, 200)

binding.tvInputTips.isVisible = editBean.inputTips?.isNotEmpty() == true
binding.tvInputTips.text = editBean.inputTips
}

override fun dismiss() {
Expand Down
47 changes: 33 additions & 14 deletions common_component/src/main/res/layout/dialog_common_edit.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/input_et"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/background_dialog_edit"
android:gravity="center_vertical"
android:hint=""
android:maxLines="1"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:singleLine="true"
android:textColor="@color/text_black"
android:textColorHint="@color/text_gray"
android:textSize="14sp" />
android:orientation="vertical">

<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/input_et"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@drawable/background_dialog_edit"
android:gravity="center_vertical"
android:hint=""
android:maxLines="1"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:singleLine="true"
android:textColor="@color/text_black"
android:textColorHint="@color/text_gray"
android:textSize="14sp" />

<TextView
android:id="@+id/tv_input_tips"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:textColor="@color/text_gray"
android:textSize="12sp"
tools:text="输入提示" />

</LinearLayout>

</layout>
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ data class EditBean(
val title: String,
val emptyWarningMsg: String,
val hint: String,
val defaultText: String? = null
val defaultText: String? = null,
val inputTips: String? = null,
val canInputEmpty: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ enum class MediaType(

REMOTE_STORAGE(
"remote_storage",
"远程媒体库",
"PC端媒体库",
cover = R.drawable.ic_remote_storage
),

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.xyoye.local_component.ui.activities.bilibili_danmu

import android.content.Intent
import android.view.Menu
import android.view.MenuItem
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.xyoye.common_component.base.BaseActivity
Expand All @@ -12,13 +14,17 @@ import com.xyoye.data_component.bean.SheetActionBean
import com.xyoye.local_component.BR
import com.xyoye.local_component.R
import com.xyoye.local_component.databinding.ActivityBilibiliDanmuBinding
import com.xyoye.local_component.ui.weight.BiliBiliDanmuMenus

@Route(path = RouteTable.Local.BiliBiliDanmu)
class BilibiliDanmuActivity : BaseActivity<BilibiliDanmuViewModel, ActivityBilibiliDanmuBinding>() {
companion object {
private const val REQUEST_CODE_SELECT_URL = 1001
}

// 标题栏菜单管理器
private lateinit var mMenus: BiliBiliDanmuMenus

override fun initViewModel() =
ViewModelInit(
BR.viewModel,
Expand Down Expand Up @@ -60,6 +66,16 @@ class BilibiliDanmuActivity : BaseActivity<BilibiliDanmuViewModel, ActivityBilib
super.onActivityResult(requestCode, resultCode, data)
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
mMenus = BiliBiliDanmuMenus.inflater(this, menu)
return super.onCreateOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
mMenus.onOptionsItemSelected(item)
return super.onOptionsItemSelected(item)
}

private fun showActionDialog() {
BottomActionDialog(
this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.net.Uri
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.xyoye.common_component.base.BaseViewModel
import com.xyoye.common_component.config.AppConfig
import com.xyoye.common_component.network.config.Api
import com.xyoye.common_component.network.repository.OtherRepository
import com.xyoye.common_component.network.repository.ResourceRepository
Expand Down Expand Up @@ -144,7 +145,11 @@ class BilibiliDanmuViewModel : BaseViewModel() {

private suspend fun findVideoCidInJavaScript(url: String): EpisodeCidData? {
val htmlElement = try {
Jsoup.connect(url).timeout(10 * 1000).get().toString()
Jsoup.connect(url)
.userAgent(AppConfig.getJsoupUserAgent().orEmpty())
.timeout(10 * 1000)
.get()
.toString()
} catch (t: Throwable) {
t.printStackTrace()
sendDownloadMessage("错误:${t.message}")
Expand Down Expand Up @@ -177,52 +182,53 @@ class BilibiliDanmuViewModel : BaseViewModel() {

private suspend fun findAnimeCidInJavaScript(url: String): AnimeCidData? {
val htmlElement = try {
Jsoup.connect(url).timeout(10 * 1000).get().toString()
Jsoup.connect(url)
.userAgent(AppConfig.getJsoupUserAgent().orEmpty())
.timeout(10 * 1000)
.get()
.toString()
} catch (t: Throwable) {
t.printStackTrace()
sendDownloadMessage("错误:${t.message}")
return null
}

val header = "<script id=\"__NEXT_DATA__\" type=\"application/json\">"
val footer = "</script>"

val pattern = Pattern.compile("($header).*($footer)")
val matcher = pattern.matcher(htmlElement)
if (matcher.find().not()) {
return null
}
val header = "window.__INITIAL_STATE__="
val footer = "};"

val jsonObject = try {
JSONObject(matcher.group(0)?.removeSurrounding(header, footer).orEmpty())
val headerStart = htmlElement.indexOf(header)
if (headerStart == -1) {
return null
}
var content = htmlElement.substring(headerStart + header.length)

val footerStart = content.indexOf(footer)
if (footerStart == -1) {
return null
}
content = content.substring(0, footerStart + 1)

JSONObject(content)
} catch (e: Exception) {
return null
}

val mediaInfoJson = jsonObject
.optJSONObject("props")
?.optJSONObject("pageProps")
?.optJSONObject("dehydratedState")
?.optJSONArray("queries")
?.optJSONObject(0)
?.optJSONObject("state")
?.optJSONObject("data")
?.optJSONObject("seasonInfo")
?.optJSONObject("mediaInfo")
?: return null

val animeTitle = mediaInfoJson.optString("title").orEmpty()
val episodesJson = mediaInfoJson.optJSONArray("episodes")
val seasonTitle = jsonObject.optJSONObject("mediaInfo")?.optString("season_title").orEmpty()
val episodesJson = jsonObject.optJSONArray("epList")
?: return null

val episodeList = mutableListOf<EpisodeCidData>()
for (index in 0 until episodesJson.length()) {
val episodeJson = episodesJson.optJSONObject(index)
val title = episodeJson.optString("playerEpTitle").orEmpty()
val episodeName = episodeJson.optString("long_title").orEmpty()
val episodeIndex = episodeJson.optString("title").orEmpty()
val title = if (episodeName.isEmpty()) "${episodeIndex}" else "${episodeIndex}$episodeName"

val cid = episodeJson.optString("cid") ?: continue
episodeList.add(EpisodeCidData(title, cid))
}
return AnimeCidData(animeTitle, episodeList)
return AnimeCidData(seasonTitle, episodeList)
}

private suspend fun actionDo(name: String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.xyoye.local_component.ui.weight

import android.view.Menu
import android.view.MenuItem
import com.xyoye.common_component.config.AppConfig
import com.xyoye.common_component.weight.dialog.CommonEditDialog
import com.xyoye.data_component.bean.EditBean
import com.xyoye.local_component.R
import com.xyoye.local_component.ui.activities.bilibili_danmu.BilibiliDanmuActivity

/**
* author: [email protected]
* time : 2024/9/2
* desc :
*/
class BiliBiliDanmuMenus private constructor(
private val activity: BilibiliDanmuActivity
) {

companion object {
fun inflater(activity: BilibiliDanmuActivity, menu: Menu): BiliBiliDanmuMenus {
activity.menuInflater.inflate(R.menu.menu_bilibili_danmu, menu)
return BiliBiliDanmuMenus(activity)
}
}

fun onOptionsItemSelected(item: MenuItem) {
if (item.itemId == R.id.item_modify_user_agent) {
showModifyUserAgentDialog()
return
}
}

private fun showModifyUserAgentDialog() {
CommonEditDialog(
activity,
EditBean(
"修改User-Agent",
"User-Agent不应为空",
"请输入User-Agent",
defaultText = AppConfig.getJsoupUserAgent(),
inputTips = "使用【选取链接下载】时,会携带此User-Agent请求链接的网页内容"
),
) {
AppConfig.putJsoupUserAgent(it)
}.show()
}
}
8 changes: 8 additions & 0 deletions local_component/src/main/res/menu/menu_bilibili_danmu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/item_modify_user_agent"
android:title="@string/action_modify_user_agent"
app:showAsAction="never" />
</menu>
2 changes: 2 additions & 0 deletions local_component/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@
<string name="action_unbind_subtitle">移除字幕绑定</string>
<string name="action_select_subtitle_file">选取本地字幕</string>
<string name="action_subtitle_key_setting">字幕API密钥</string>

<string name="action_modify_user_agent">修改User-Agent</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class RemoteStorageEditDialog(
this.binding = binding
val isEditStorage = originalStorage != null

setTitle(if (isEditStorage) "编辑远程连接帐号" else "添加远程连接帐号")
setTitle(if (isEditStorage) "编辑PC端媒体库帐号" else "添加PC端媒体库帐号")
binding.remoteData = remoteData

setGroupMode(remoteData.remoteAnimeGrouping)
Expand Down Expand Up @@ -78,7 +78,7 @@ class RemoteStorageEditDialog(
setPositiveListener {
if (checkParams(remoteData)) {
if (remoteData.displayName.isEmpty()) {
remoteData.displayName = "远程媒体库"
remoteData.displayName = "PC端媒体库"
}
remoteData.describe = remoteData.url
activity.addStorage(remoteData)
Expand Down

0 comments on commit 7b357a6

Please sign in to comment.