Skip to content

Commit

Permalink
fix retry bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Feb 3, 2024
1 parent 73239a6 commit 088808c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 24 deletions.
4 changes: 0 additions & 4 deletions app/src/main/java/com/lizongying/mytv/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,13 @@ 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)
.add(R.id.main_browse_fragment, channelFragment)
.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())

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/lizongying/mytv/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 48 additions & 13 deletions app/src/main/java/com/lizongying/mytv/Request.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 综合",
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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<Info>, 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)
}
}
}
})
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/lizongying/mytv/TVList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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"),
Expand Down
30 changes: 28 additions & 2 deletions app/src/main/java/com/lizongying/mytv/models/TVViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>()
Expand Down Expand Up @@ -77,6 +79,8 @@ class TVViewModel(private var tv: TV) : ViewModel() {

var needToken = false

var mustToken = false

private val channelsNeedToken = arrayOf(
"CCTV4K 超高清",
"CCTV2 财经",
Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 088808c

Please sign in to comment.