Skip to content

Commit

Permalink
✨ 自定义ua
Browse files Browse the repository at this point in the history
  • Loading branch information
yaoxieyoulei committed Jun 23, 2024
1 parent c0afb10 commit 1710380
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 10 deletions.
10 changes: 5 additions & 5 deletions app/src/main/java/top/yogiczy/mytv/data/utils/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object Constants {
* IPTV源地址
*/
const val IPTV_SOURCE_URL =
"https://mirror.ghproxy.com/https://raw.githubusercontent.com/zhumeng11/IPTV/main/IPTV.m3u"
"https://mirror.ghproxy.com/https://fanmingming.com/txt?url=https://raw.githubusercontent.com/fanmingming/live/main/tv/m3u/ipv6.m3u"

/**
* IPTV源缓存时间(毫秒)
Expand Down Expand Up @@ -57,14 +57,14 @@ object Constants {
const val HTTP_RETRY_INTERVAL = 3000L

/**
* 日志历史最大保留条数
* HTTP userAgent
*/
const val LOG_HISTORY_MAX_SIZE = 50
const val HTTP_USER_AGENT = "ExoPlayer"

/**
* 播放器 http userAgent
* 日志历史最大保留条数
*/
const val VIDEO_PLAYER_HTTP_USER_AGENT = "ExoPlayer"
const val LOG_HISTORY_MAX_SIZE = 50

/**
* 播放器加载超时
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package top.yogiczy.mytv.ui.screens.leanback.settings
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.BugReport
import androidx.compose.material.icons.filled.DisplaySettings
import androidx.compose.material.icons.filled.Http
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.LiveTv
import androidx.compose.material.icons.filled.Menu
Expand All @@ -23,6 +24,7 @@ enum class LeanbackSettingsCategories(
UI(Icons.Default.DisplaySettings, "界面"),
FAVORITE(Icons.Default.Star, "收藏"),
UPDATE(Icons.Default.Update, "更新"),
HTTP(Icons.Default.Http, "网络"),
DEBUG(Icons.Default.BugReport, "调试"),
MORE(Icons.Default.MoreHoriz, "更多设置"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,12 @@ class LeanbackSettingsViewModel : ViewModel() {
_updateForceRemind = value
SP.updateForceRemind = value
}

private var _httpUserAgent by mutableStateOf(SP.httpUserAgent)
var httpUserAgent: String
get() = _httpUserAgent
set(value) {
_httpUserAgent = value
SP.httpUserAgent = value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fun LeanbackSettingsCategoryContent(
LeanbackSettingsCategories.UI -> LeanbackSettingsCategoryUI()
LeanbackSettingsCategories.FAVORITE -> LeanbackSettingsCategoryFavorite()
LeanbackSettingsCategories.UPDATE -> LeanbackSettingsCategoryUpdate()
LeanbackSettingsCategories.HTTP -> LeanbackSettingsCategoryHttp()
LeanbackSettingsCategories.DEBUG -> LeanbackSettingsCategoryDebug()
LeanbackSettingsCategories.MORE -> LeanbackSettingsCategoryMore()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package top.yogiczy.mytv.ui.screens.leanback.settings.components

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Switch
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.tv.foundation.lazy.list.TvLazyColumn
import top.yogiczy.mytv.ui.screens.leanback.settings.LeanbackSettingsViewModel
import top.yogiczy.mytv.ui.theme.LeanbackTheme
import top.yogiczy.mytv.ui.utils.SP

@Composable
fun LeanbackSettingsCategoryHttp(
modifier: Modifier = Modifier,
settingsViewModel: LeanbackSettingsViewModel = viewModel(),
) {
TvLazyColumn(
modifier = modifier,
verticalArrangement = Arrangement.spacedBy(10.dp),
contentPadding = PaddingValues(vertical = 10.dp),
) {
item {
LeanbackSettingsCategoryListItem(
headlineContent = "自定义UA",
supportingContent = settingsViewModel.httpUserAgent,
)
}
}
}

@Preview
@Composable
private fun LeanbackSettingsCategoryHttpPreview() {
SP.init(LocalContext.current)
LeanbackTheme {
LeanbackSettingsCategoryHttp(
modifier = Modifier.padding(20.dp),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.media3.exoplayer.hls.HlsMediaSource
import androidx.media3.exoplayer.source.ProgressiveMediaSource
import androidx.media3.exoplayer.util.EventLogger
import top.yogiczy.mytv.data.utils.Constants
import top.yogiczy.mytv.ui.utils.SP
import androidx.media3.common.PlaybackException as Media3PlaybackException

@OptIn(UnstableApi::class)
Expand All @@ -41,7 +42,7 @@ class LeanbackMedia3VideoPlayer(
private fun prepare(uri: Uri, contentType: Int? = null) {
val dataSourceFactory =
DefaultDataSource.Factory(context, DefaultHttpDataSource.Factory().apply {
setUserAgent(Constants.VIDEO_PLAYER_HTTP_USER_AGENT)
setUserAgent(SP.httpUserAgent)
setConnectTimeoutMs(Constants.VIDEO_PLAYER_LOAD_TIMEOUT.toInt())
setReadTimeoutMs(Constants.VIDEO_PLAYER_LOAD_TIMEOUT.toInt())
setKeepPostFor302Redirects(true)
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/top/yogiczy/mytv/ui/utils/HttpServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ object HttpServer : Loggable() {
appRepo = Constants.APP_REPO,
iptvSourceUrl = SP.iptvSourceUrl,
epgXmlUrl = SP.epgXmlUrl,
httpUserAgent = SP.httpUserAgent,
logHistory = Logger.history,
)
)
Expand All @@ -109,6 +110,7 @@ object HttpServer : Loggable() {
val body = request.getBody<JSONObjectBody>().get()
val iptvSourceUrl = body.get("iptvSourceUrl").toString()
val epgXmlUrl = body.get("epgXmlUrl").toString()
val httpUserAgent = body.get("httpUserAgent").toString()

if (SP.iptvSourceUrl != iptvSourceUrl) {
SP.iptvSourceUrl = iptvSourceUrl
Expand All @@ -120,6 +122,8 @@ object HttpServer : Loggable() {
EpgRepository().clearCache()
}

SP.httpUserAgent = httpUserAgent

wrapResponse(response).send("success")
}

Expand Down Expand Up @@ -185,6 +189,7 @@ private data class AllSettings(
val appRepo: String,
val iptvSourceUrl: String,
val epgXmlUrl: String,
val httpUserAgent: String,

val logHistory: List<Logger.HistoryItem>,
)
12 changes: 12 additions & 0 deletions app/src/main/java/top/yogiczy/mytv/ui/utils/SP.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ object SP {
/** ==================== 更新 ==================== */
/** 更新强提醒(弹窗形式) */
UPDATE_FORCE_REMIND,

/** ==================== 网络 ==================== */
/** 自定义ua */
HTTP_USER_AGENT,
}

/** ==================== 应用 ==================== */
Expand Down Expand Up @@ -243,6 +247,14 @@ object SP {
get() = sp.getBoolean(KEY.UPDATE_FORCE_REMIND.name, false)
set(value) = sp.edit().putBoolean(KEY.UPDATE_FORCE_REMIND.name, value).apply()

/** ==================== 网络 ==================== */
/** 自定义ua */
var httpUserAgent: String
get() = (sp.getString(KEY.HTTP_USER_AGENT.name, "") ?: "").ifBlank {
Constants.HTTP_USER_AGENT
}
set(value) = sp.edit().putString(KEY.HTTP_USER_AGENT.name, value).apply()

enum class UiTimeShowMode(val value: Int) {
/** 隐藏 */
HIDDEN(0),
Expand Down
26 changes: 22 additions & 4 deletions app/src/main/res/raw/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<van-space class="w-full" direction="vertical" size="small">
<span>支持m3u链接与tvbox链接</span>
<van-field class="!p-0" placeholder="置为空时,将恢复默认"
v-model="settings.iptvSourceUrl"></van-field>
v-model="settings.iptvSourceUrl" type="textarea" rows="2"></van-field>
</van-space>
</template>

Expand All @@ -68,7 +68,7 @@
<van-space class="w-full" direction="vertical" size="small">
<span>支持xml、xml.gz格式</span>
<van-field class="!p-0" placeholder="置为空时,将恢复默认"
v-model="settings.epgXmlUrl"></van-field>
v-model="settings.epgXmlUrl" type="textarea" rows="2"></van-field>
</van-space>
</template>

Expand All @@ -81,6 +81,24 @@
</van-cell>
</van-cell-group>

<van-cell-group inset title="网络">
<van-cell title="自定义UA">
<template #label>
<van-space class="w-full" direction="vertical" size="small">
<van-field class="!p-0" placeholder="置为空时,将恢复默认"
v-model="settings.httpUserAgent" type="textarea" rows="2"></van-field>
</van-space>
</template>

<template #extra>
<van-button
@click="settings.epgXmlCachedAt = 0; settings.epgCachedHash = 0; confirmSettings()"
size="small" type="primary">推送
</van-button>
</template>
</van-cell>
</van-cell-group>

<van-cell-group inset title="调试">
<van-cell title="上传apk">
<template #extra>
Expand Down Expand Up @@ -123,8 +141,8 @@
<script>
const { createApp, ref, onMounted, watch, nextTick } = Vue

// const baseUrl = "http://127.0.0.1:10481"
const baseUrl = ""
const baseUrl = "http://127.0.0.1:10481"
//const baseUrl = ""

async function requestApi(url, config) {
const resp = await fetch(`${baseUrl}${url}`, config)
Expand Down

0 comments on commit 1710380

Please sign in to comment.