Skip to content

Commit 353dc05

Browse files
committed
fix ysp
1 parent d7a6053 commit 353dc05

File tree

10 files changed

+262
-8
lines changed

10 files changed

+262
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ adb install my-tv.apk
119119
* 获取系统时间
120120
* 选中的图标比例能否相差更大
121121
* 自动重连
122+
* 节目增加预告
122123

123124
## 赞赏
124125

2.82 KB
Binary file not shown.
2.29 KB
Binary file not shown.

app/src/main/java/com/lizongying/mytv/Encryptor.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Encryptor {
99

1010
external fun hash(data: ByteArray): ByteArray?
1111

12+
external fun hash2(data: ByteArray): ByteArray?
13+
1214
companion object {
1315
init {
1416
System.loadLibrary("native")

app/src/main/java/com/lizongying/mytv/Request.kt

Lines changed: 137 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import android.util.Base64
77
import android.util.Log
88
import com.lizongying.mytv.Utils.getDateFormat
99
import com.lizongying.mytv.api.ApiClient
10+
import com.lizongying.mytv.api.Auth
11+
import com.lizongying.mytv.api.AuthRequest
1012
import com.lizongying.mytv.api.Info
1113
import com.lizongying.mytv.api.LiveInfo
1214
import com.lizongying.mytv.api.LiveInfoRequest
@@ -52,6 +54,84 @@ class Request {
5254
}
5355

5456
var call: Call<LiveInfo>? = null
57+
var callAuth: Call<Auth>? = null
58+
59+
private fun fetchAuth(tvModel: TVViewModel, cookie: String) {
60+
callAuth?.cancel()
61+
62+
val title = tvModel.title.value
63+
64+
val data = ysp?.getAuthData(tvModel)
65+
val request = data?.let { AuthRequest(it) }
66+
callAuth = request?.let { yspApiService.getAuth("guid=${ysp?.getGuid()}; $cookie", it) }
67+
68+
callAuth?.enqueue(object : Callback<Auth> {
69+
override fun onResponse(call: Call<Auth>, response: Response<Auth>) {
70+
if (response.isSuccessful) {
71+
val liveInfo = response.body()
72+
73+
if (liveInfo?.data?.token != null) {
74+
Log.i(TAG, "token ${liveInfo.data.token}")
75+
ysp?.token = liveInfo.data.token
76+
fetchVideo(tvModel, cookie)
77+
} else {
78+
Log.e(TAG, "$title token error")
79+
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
80+
tvModel.retryTimes++
81+
if (tvModel.getTV().needToken) {
82+
if (tvModel.tokenRetryTimes == tvModel.tokenRetryMaxTimes) {
83+
if (!tvModel.getTV().mustToken) {
84+
fetchAuth(tvModel, cookie)
85+
}
86+
} else {
87+
token = ""
88+
fetchAuth(tvModel)
89+
}
90+
} else {
91+
fetchAuth(tvModel, cookie)
92+
}
93+
}
94+
}
95+
} else {
96+
Log.e(TAG, "$title auth status error")
97+
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
98+
tvModel.retryTimes++
99+
if (tvModel.getTV().needToken) {
100+
if (tvModel.tokenRetryTimes == tvModel.tokenRetryMaxTimes) {
101+
if (!tvModel.getTV().mustToken) {
102+
fetchAuth(tvModel, cookie)
103+
}
104+
} else {
105+
token = ""
106+
fetchAuth(tvModel)
107+
}
108+
} else {
109+
fetchAuth(tvModel, cookie)
110+
}
111+
}
112+
}
113+
}
114+
115+
override fun onFailure(call: Call<Auth>, t: Throwable) {
116+
Log.e(TAG, "$title auth request error $t")
117+
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
118+
tvModel.retryTimes++
119+
if (tvModel.getTV().needToken) {
120+
if (tvModel.tokenRetryTimes == tvModel.tokenRetryMaxTimes) {
121+
if (!tvModel.getTV().mustToken) {
122+
fetchVideo(tvModel, cookie)
123+
}
124+
} else {
125+
token = ""
126+
fetchAuth(tvModel)
127+
}
128+
} else {
129+
fetchAuth(tvModel, cookie)
130+
}
131+
}
132+
}
133+
})
134+
}
55135

56136
fun fetchVideo(tvModel: TVViewModel, cookie: String) {
57137
call?.cancel()
@@ -64,7 +144,7 @@ class Request {
64144
tvModel.seq = 0
65145
val data = ysp?.switch(tvModel)
66146
val request = data?.let { LiveInfoRequest(it) }
67-
call = request?.let { yspApiService.getLiveInfo("guid=${ysp?.getGuid()}; $cookie", it) }
147+
call = request?.let { yspApiService.getLiveInfo("guid=${ysp?.getGuid()}; $cookie", ysp!!.token, it) }
68148

69149
call?.enqueue(object : Callback<LiveInfo> {
70150
override fun onResponse(call: Call<LiveInfo>, response: Response<LiveInfo>) {
@@ -175,6 +255,53 @@ class Request {
175255
})
176256
}
177257

258+
fun fetchAuth(tvModel: TVViewModel) {
259+
if (token == "") {
260+
yspTokenService.getInfo()
261+
.enqueue(object : Callback<Info> {
262+
override fun onResponse(call: Call<Info>, response: Response<Info>) {
263+
if (response.isSuccessful) {
264+
token = response.body()?.data?.token!!
265+
Log.i(TAG, "info success $token")
266+
val cookie =
267+
"versionName=99.99.99; versionCode=999999; vplatform=109; platformVersion=Chrome; deviceModel=120; appid=1400421205; yspappid=519748109;yspopenid=vu0-8lgGV2LW9QjDeuBFsX8yMnzs37Q3_HZF6XyVDpGR_I; vusession=$token"
268+
fetchAuth(tvModel, cookie)
269+
} else {
270+
Log.e(TAG, "info status error")
271+
if (tvModel.tokenRetryTimes < tvModel.tokenRetryMaxTimes) {
272+
tvModel.tokenRetryTimes++
273+
fetchAuth(tvModel)
274+
} else {
275+
if (!tvModel.getTV().mustToken) {
276+
val cookie =
277+
"versionName=99.99.99; versionCode=999999; vplatform=109; platformVersion=Chrome; deviceModel=120; appid=1400421205; yspappid=519748109"
278+
fetchAuth(tvModel, cookie)
279+
}
280+
}
281+
}
282+
}
283+
284+
override fun onFailure(call: Call<Info>, t: Throwable) {
285+
Log.e(TAG, "info request error $t")
286+
if (tvModel.tokenRetryTimes < tvModel.tokenRetryMaxTimes) {
287+
tvModel.tokenRetryTimes++
288+
fetchVideo(tvModel)
289+
} else {
290+
if (!tvModel.getTV().mustToken) {
291+
val cookie =
292+
"versionName=99.99.99; versionCode=999999; vplatform=109; platformVersion=Chrome; deviceModel=120; appid=1400421205; yspappid=519748109"
293+
fetchAuth(tvModel, cookie)
294+
}
295+
}
296+
}
297+
})
298+
} else {
299+
val cookie =
300+
"versionName=99.99.99; versionCode=999999; vplatform=109; platformVersion=Chrome; deviceModel=120; appid=1400421205; yspappid=519748109;yspopenid=vu0-8lgGV2LW9QjDeuBFsX8yMnzs37Q3_HZF6XyVDpGR_I; vusession=$token"
301+
fetchAuth(tvModel, cookie)
302+
}
303+
}
304+
178305
fun fetchVideo(tvModel: TVViewModel) {
179306
if (token == "") {
180307
yspTokenService.getInfo()
@@ -184,7 +311,7 @@ class Request {
184311
token = response.body()?.data?.token!!
185312
Log.i(TAG, "info success $token")
186313
val cookie =
187-
"versionName=99.99.99; versionCode=999999; vplatform=109; platformVersion=Chrome; deviceModel=120; yspappid=519748109;yspopenid=vu0-8lgGV2LW9QjDeuBFsX8yMnzs37Q3_HZF6XyVDpGR_I; vusession=$token"
314+
"versionName=99.99.99; versionCode=999999; vplatform=109; platformVersion=Chrome; deviceModel=120; appid=1400421205; yspappid=519748109;yspopenid=vu0-8lgGV2LW9QjDeuBFsX8yMnzs37Q3_HZF6XyVDpGR_I; vusession=$token"
188315
fetchVideo(tvModel, cookie)
189316
} else {
190317
Log.e(TAG, "info status error")
@@ -194,7 +321,7 @@ class Request {
194321
} else {
195322
if (!tvModel.getTV().mustToken) {
196323
val cookie =
197-
"versionName=99.99.99; versionCode=999999; vplatform=109; platformVersion=Chrome; deviceModel=120; yspappid=519748109"
324+
"versionName=99.99.99; versionCode=999999; vplatform=109; platformVersion=Chrome; deviceModel=120; appid=1400421205; yspappid=519748109"
198325
fetchVideo(tvModel, cookie)
199326
}
200327
}
@@ -209,26 +336,28 @@ class Request {
209336
} else {
210337
if (!tvModel.getTV().mustToken) {
211338
val cookie =
212-
"versionName=99.99.99; versionCode=999999; vplatform=109; platformVersion=Chrome; deviceModel=120; yspappid=519748109"
339+
"versionName=99.99.99; versionCode=999999; vplatform=109; platformVersion=Chrome; deviceModel=120; appid=1400421205; yspappid=519748109"
213340
fetchVideo(tvModel, cookie)
214341
}
215342
}
216343
}
217344
})
218345
} else {
219346
val cookie =
220-
"versionName=99.99.99; versionCode=999999; vplatform=109; platformVersion=Chrome; deviceModel=120; yspappid=519748109;yspopenid=vu0-8lgGV2LW9QjDeuBFsX8yMnzs37Q3_HZF6XyVDpGR_I; vusession=$token"
347+
"versionName=99.99.99; versionCode=999999; vplatform=109; platformVersion=Chrome; deviceModel=120; appid=1400421205; yspappid=519748109;yspopenid=vu0-8lgGV2LW9QjDeuBFsX8yMnzs37Q3_HZF6XyVDpGR_I; vusession=$token"
221348
fetchVideo(tvModel, cookie)
222349
}
223350
}
224351

225352
fun fetchData(tvModel: TVViewModel) {
226353
if (tvModel.getTV().needToken) {
227-
fetchVideo(tvModel)
354+
// fetchVideo(tvModel)
355+
fetchAuth(tvModel)
228356
} else {
229357
val cookie =
230-
"versionName=99.99.99; versionCode=999999; vplatform=109; platformVersion=Chrome; deviceModel=120; yspappid=519748109"
231-
fetchVideo(tvModel, cookie)
358+
"versionName=99.99.99; versionCode=999999; vplatform=109; platformVersion=Chrome; deviceModel=120; appid=1400421205"
359+
// fetchVideo(tvModel, cookie)
360+
fetchAuth(tvModel, cookie)
232361
}
233362
}
234363

app/src/main/java/com/lizongying/mytv/UpdateManager.kt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import android.content.BroadcastReceiver
66
import android.content.Context
77
import android.content.Intent
88
import android.content.IntentFilter
9+
import android.database.Cursor
910
import android.net.Uri
1011
import android.os.Build
1112
import android.os.Environment
13+
import android.os.Handler
14+
import android.os.Looper
1215
import android.util.Log
1316
import android.widget.Toast
1417
import com.lizongying.mytv.api.Release
@@ -66,6 +69,7 @@ class UpdateManager(
6669
val downloadManager =
6770
context!!.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
6871
val request = Request(Uri.parse(release.data.downloadUrl))
72+
Log.i(TAG, "url ${Uri.parse(release.data.downloadUrl)}")
6973
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, apkFileName)
7074
request.setTitle("New Version Download")
7175
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
@@ -87,6 +91,48 @@ class UpdateManager(
8791
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
8892
)
8993
}
94+
95+
getDownloadProgress(context!!, downloadReference) { progress ->
96+
println("Download progress: $progress%")
97+
}
98+
}
99+
100+
private fun getDownloadProgress(context: Context, downloadId: Long, progressListener: (Int) -> Unit) {
101+
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
102+
val handler = Handler(Looper.getMainLooper())
103+
val intervalMillis: Long = 1000
104+
105+
handler.post(object : Runnable {
106+
override fun run() {
107+
Log.i(TAG, "search")
108+
val query = DownloadManager.Query().setFilterById(downloadId)
109+
val cursor: Cursor = downloadManager.query(query)
110+
cursor.use {
111+
if (it.moveToFirst()) {
112+
val bytesDownloadedIndex =
113+
it.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)
114+
val bytesTotalIndex =
115+
it.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)
116+
117+
// 检查列名是否存在
118+
if (bytesDownloadedIndex != -1 && bytesTotalIndex != -1) {
119+
val bytesDownloaded = it.getInt(bytesDownloadedIndex)
120+
val bytesTotal = it.getInt(bytesTotalIndex)
121+
122+
if (bytesTotal != -1) {
123+
val progress = (bytesDownloaded * 100L / bytesTotal).toInt()
124+
progressListener(progress)
125+
if (progress == 100) {
126+
return
127+
}
128+
}
129+
}
130+
}
131+
}
132+
133+
// handler.postDelayed(this, intervalMillis)
134+
}
135+
})
90136
}
91137

92138
private class DownloadReceiver(

app/src/main/java/com/lizongying/mytv/api/ApiClient.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import okhttp3.OkHttpClient
55
import retrofit2.Retrofit
66
import retrofit2.converter.gson.GsonConverterFactory
77
import retrofit2.converter.protobuf.ProtoConverterFactory
8+
import java.net.InetSocketAddress
9+
import java.net.Proxy
810
import javax.net.ssl.SSLContext
911
import javax.net.ssl.TrustManager
1012
import javax.net.ssl.X509TrustManager
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.lizongying.mytv.api
2+
3+
import android.util.Log
4+
import okhttp3.MediaType
5+
import okhttp3.RequestBody
6+
import okio.BufferedSink
7+
import java.io.IOException
8+
9+
data class Auth(
10+
val code: Int,
11+
val msg: String,
12+
val data: AuthData,
13+
)
14+
15+
data class AuthData(
16+
val token: String,
17+
)
18+
19+
data class AuthRequest(
20+
var data: String,
21+
) : RequestBody() {
22+
override fun contentType(): MediaType? {
23+
return MediaType.parse("application/x-www-form-urlencoded;charset=UTF-8")
24+
}
25+
26+
override fun writeTo(sink: BufferedSink) {
27+
try {
28+
sink.writeUtf8(data)
29+
} catch (e: IOException) {
30+
Log.e(TAG, "$e")
31+
}
32+
}
33+
34+
companion object {
35+
private const val TAG = "AuthRequest"
36+
}
37+
}

app/src/main/java/com/lizongying/mytv/api/YSP.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ class YSP(var context: Context) {
5050

5151
private var signature = ""
5252

53+
private var appid = "ysp_pc"
54+
var token = ""
55+
5356
private var encryptor: Encryptor? = null
5457
private lateinit var sharedPref: SharedPreferences
5558

@@ -83,6 +86,19 @@ class YSP(var context: Context) {
8386
return """{"cnlid":"$cnlid","livepid":"$livepid","stream":"$stream","guid":"$guid","cKey":"$cKey","adjust":$adjust,"sphttps":"$sphttps","platform":"$platform","cmd":"$cmd","encryptVer":"$encryptVer","dtype":"$dtype","devid":"$devid","otype":"$otype","appVer":"$appVer","app_version":"$appVersion","rand_str":"$randStr","channel":"$channel","defn":"$defn","signature":"$signature"}"""
8487
}
8588

89+
fun getAuthData(tvModel: TVViewModel): String {
90+
livepid = tvModel.pid.value!!
91+
92+
randStr = getRand()
93+
94+
if (tvModel.retryTimes > 0) {
95+
guid = newGuid()
96+
}
97+
98+
signature = getAuthSignature()
99+
return """pid=$livepid&guid=$guid&appid=$appid&rand_str=$randStr&signature=$signature"""
100+
}
101+
86102
private fun getTimeStr(): String {
87103
return getDateTimestamp().toString()
88104
}
@@ -131,6 +147,13 @@ class YSP(var context: Context) {
131147
return hashedData.let { it -> it.joinToString("") { "%02x".format(it) } }
132148
}
133149

150+
private fun getAuthSignature(): String {
151+
val e =
152+
"appid=${appid}&guid=${guid}&pid=${livepid}&rand_str=${randStr}".toByteArray()
153+
val hashedData = encryptor?.hash2(e) ?: return ""
154+
return hashedData.let { it -> it.joinToString("") { "%02x".format(it) } }
155+
}
156+
134157
companion object {
135158
private const val TAG = "YSP"
136159
}

0 commit comments

Comments
 (0)