From 088808c3fde558518664b2e3c67ca813e20a860b Mon Sep 17 00:00:00 2001 From: Li ZongYing Date: Sat, 3 Feb 2024 20:07:08 +0800 Subject: [PATCH] fix retry bug --- .../java/com/lizongying/mytv/MainActivity.kt | 4 -- .../java/com/lizongying/mytv/MainFragment.kt | 2 +- .../main/java/com/lizongying/mytv/Request.kt | 61 +++++++++++++++---- .../main/java/com/lizongying/mytv/TVList.kt | 8 +-- .../com/lizongying/mytv/models/TVViewModel.kt | 30 ++++++++- 5 files changed, 81 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/lizongying/mytv/MainActivity.kt b/app/src/main/java/com/lizongying/mytv/MainActivity.kt index 4875b6aa..4c9837f5 100644 --- a/app/src/main/java/com/lizongying/mytv/MainActivity.kt +++ b/app/src/main/java/com/lizongying/mytv/MainActivity.kt @@ -54,7 +54,6 @@ class MainActivity : FragmentActivity() { window.decorView.systemUiVisibility = SYSTEM_UI_FLAG_HIDE_NAVIGATION if (savedInstanceState == null) { - Log.i(TAG, "beginTransaction begin") supportFragmentManager.beginTransaction() .add(R.id.main_browse_fragment, playerFragment) .add(R.id.main_browse_fragment, infoFragment) @@ -62,9 +61,6 @@ class MainActivity : FragmentActivity() { .add(R.id.main_browse_fragment, mainFragment) .hide(mainFragment) .commit() - Log.i(TAG, "beginTransaction end") - } else { - Log.i(TAG, "savedInstanceState $savedInstanceState") } gestureDetector = GestureDetector(this, GestureListener()) diff --git a/app/src/main/java/com/lizongying/mytv/MainFragment.kt b/app/src/main/java/com/lizongying/mytv/MainFragment.kt index 2b6793d0..b537dcbe 100644 --- a/app/src/main/java/com/lizongying/mytv/MainFragment.kt +++ b/app/src/main/java/com/lizongying/mytv/MainFragment.kt @@ -304,7 +304,7 @@ class MainFragment : BrowseSupportFragment() { inner class UpdateProgramRunnable : Runnable { override fun run() { - tvListViewModel.tvListViewModel.value?.filter { it.programId.value != null } + tvListViewModel.tvListViewModel.value?.filter { it.programId.value != null && it.programId.value != "" } ?.forEach { tvViewModel -> updateProgram( tvViewModel diff --git a/app/src/main/java/com/lizongying/mytv/Request.kt b/app/src/main/java/com/lizongying/mytv/Request.kt index dc34c1f4..89f0def4 100644 --- a/app/src/main/java/com/lizongying/mytv/Request.kt +++ b/app/src/main/java/com/lizongying/mytv/Request.kt @@ -39,6 +39,8 @@ class Request { private lateinit var btraceRunnable: BtraceRunnable private var tokenRunnable: TokenRunnable = TokenRunnable() + private val regex = Regex("""des_key = "([^"]+).+var des_iv = "([^"]+)""") + private var mapping = mapOf( "CCTV4K" to "CCTV4K 超高清", "CCTV1" to "CCTV1 综合", @@ -121,7 +123,6 @@ class Request { Base64.DEFAULT ) val decodedString = String(decodedBytes) - val regex = Regex("""des_key = "([^"]+).+var des_iv = "([^"]+)""") val matchResult = regex.find(decodedString) if (matchResult != null) { val (key, iv) = matchResult.destructured @@ -142,8 +143,14 @@ class Request { if (tvModel.retryTimes < tvModel.retryMaxTimes) { tvModel.retryTimes++ if (tvModel.needToken) { - token = "" - fetchVideo(tvModel) + if (tvModel.tokenRetryTimes == tvModel.tokenRetryMaxTimes) { + if (!tvModel.mustToken) { + fetchVideo(tvModel, cookie) + } + } else { + token = "" + fetchVideo(tvModel) + } } else { fetchVideo(tvModel, cookie) } @@ -158,8 +165,14 @@ class Request { if (tvModel.retryTimes < tvModel.retryMaxTimes) { tvModel.retryTimes++ if (tvModel.needToken) { - token = "" - fetchVideo(tvModel) + if (tvModel.tokenRetryTimes == tvModel.tokenRetryMaxTimes) { + if (!tvModel.mustToken) { + fetchVideo(tvModel, cookie) + } + } else { + token = "" + fetchVideo(tvModel) + } } else { fetchVideo(tvModel, cookie) } @@ -171,8 +184,14 @@ class Request { if (tvModel.retryTimes < tvModel.retryMaxTimes) { tvModel.retryTimes++ if (tvModel.needToken) { - token = "" - fetchVideo(tvModel) + if (tvModel.tokenRetryTimes == tvModel.tokenRetryMaxTimes) { + if (!tvModel.mustToken) { + fetchVideo(tvModel, cookie) + } + } else { + token = "" + fetchVideo(tvModel) + } } else { fetchVideo(tvModel, cookie) } @@ -185,8 +204,14 @@ class Request { if (tvModel.retryTimes < tvModel.retryMaxTimes) { tvModel.retryTimes++ if (tvModel.needToken) { - token = "" - fetchVideo(tvModel) + if (tvModel.tokenRetryTimes == tvModel.tokenRetryMaxTimes) { + if (!tvModel.mustToken) { + fetchVideo(tvModel, cookie) + } + } else { + token = "" + fetchVideo(tvModel) + } } else { fetchVideo(tvModel, cookie) } @@ -208,18 +233,28 @@ class Request { fetchVideo(tvModel, cookie) } else { Log.e(TAG, "info status error") - if (tvModel.retryTimes < tvModel.retryMaxTimes) { - tvModel.retryTimes++ + if (tvModel.tokenRetryTimes < tvModel.tokenRetryMaxTimes) { + tvModel.tokenRetryTimes++ fetchVideo(tvModel) + } else { + if (!tvModel.mustToken) { + val cookie = "vplatform=109" + fetchVideo(tvModel, cookie) + } } } } override fun onFailure(call: Call, t: Throwable) { Log.e(TAG, "info request error $t") - if (tvModel.retryTimes < tvModel.retryMaxTimes) { - tvModel.retryTimes++ + if (tvModel.tokenRetryTimes < tvModel.tokenRetryMaxTimes) { + tvModel.tokenRetryTimes++ fetchVideo(tvModel) + } else { + if (!tvModel.mustToken) { + val cookie = "vplatform=109" + fetchVideo(tvModel, cookie) + } } } }) diff --git a/app/src/main/java/com/lizongying/mytv/TVList.kt b/app/src/main/java/com/lizongying/mytv/TVList.kt index ca03d677..66bb78f6 100644 --- a/app/src/main/java/com/lizongying/mytv/TVList.kt +++ b/app/src/main/java/com/lizongying/mytv/TVList.kt @@ -71,7 +71,7 @@ object TVList { ) private var mappingEPG = mapOf( "CCTV4K 超高清" to "600002264", - "CCTV8K 超高清" to "600156816", +// "CCTV8K 超高清" to "600156816", "CCTV1 综合" to "600001859", "CCTV2 财经" to "600001800", "CCTV3 综艺" to "600001801", @@ -95,7 +95,7 @@ object TVList { "CGTN 俄语频道" to "600084758", "CGTN 阿拉伯语频道" to "600084782", "CGTN 西班牙语频道" to "600084744", - "CGTN 记录频道" to "600084781", +// "CGTN 记录频道" to "600084781", "风云剧场" to "600099658", "第一剧场" to "600099655", "怀旧剧场" to "600099620", @@ -130,8 +130,8 @@ object TVList { "四川卫视" to "600002516", "东南卫视" to "600002484", "海南卫视" to "600002506", - "天津卫视" to "600152137", - "新疆卫视" to "600152138", +// "天津卫视" to "600152137", +// "新疆卫视" to "600152138", ) private var mappingVideo = mapOf( "CCTV4K 超高清" to arrayOf("600002264", "2000266303"), diff --git a/app/src/main/java/com/lizongying/mytv/models/TVViewModel.kt b/app/src/main/java/com/lizongying/mytv/models/TVViewModel.kt index 16d7a830..a2a4451b 100644 --- a/app/src/main/java/com/lizongying/mytv/models/TVViewModel.kt +++ b/app/src/main/java/com/lizongying/mytv/models/TVViewModel.kt @@ -19,8 +19,10 @@ class TVViewModel(private var tv: TV) : ViewModel() { private var rowPosition: Int = 0 private var itemPosition: Int = 0 - var retryTimes: Int = 0 - var retryMaxTimes: Int = 8 + var retryTimes = 0 + var tokenRetryTimes = 0 + var retryMaxTimes = 8 + var tokenRetryMaxTimes = 2 var programUpdateTime: Long = 0 private val _errInfo = MutableLiveData() @@ -77,6 +79,8 @@ class TVViewModel(private var tv: TV) : ViewModel() { var needToken = false + var mustToken = false + private val channelsNeedToken = arrayOf( "CCTV4K 超高清", "CCTV2 财经", @@ -134,6 +138,25 @@ class TVViewModel(private var tv: TV) : ViewModel() { "新疆卫视", ) + private val channelsMustToken = arrayOf( + "CCTV3 综艺", + "CCTV6 电影", + "CCTV8 电视剧", + "风云剧场", + "第一剧场", + "怀旧剧场", + "世界地理", + "风云音乐", + "兵器科技", + "风云足球", + "高尔夫网球", + "女性时尚", + "央视文化精品", + "央视台球", + "电视指南", + "卫生健康", + ) + fun addVideoUrl(url: String) { if (_videoUrl.value?.isNotEmpty() == true) { if (_videoUrl.value!!.last().contains("cctv.cn")) { @@ -188,6 +211,9 @@ class TVViewModel(private var tv: TV) : ViewModel() { if (tv.title in channelsNeedToken) { needToken = true } + if (tv.title in channelsMustToken) { + mustToken = true + } } fun getRowPosition(): Int {