kotlin 修改

This commit is contained in:
“lushunming” 2025-07-13 11:03:57 +08:00
parent 1bf138ada9
commit 977e98c2b4
5 changed files with 37 additions and 24 deletions

View File

@ -10,9 +10,11 @@ import com.github.catvod.utils.ProxyVideo.proxy
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.joinAll import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import okhttp3.Response import okhttp3.Response
import org.apache.commons.lang3.StringUtils import org.apache.commons.lang3.StringUtils
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
@ -25,21 +27,27 @@ object DownloadMT {
val THREAD_NUM: Int = Runtime.getRuntime().availableProcessors() * 2 val THREAD_NUM: Int = Runtime.getRuntime().availableProcessors() * 2
private val infos = mutableMapOf<String, Array<Any>>(); private val infos = mutableMapOf<String, Array<Any>>();
fun proxyMultiThread(url: String, headers: Map<String, String>): Array<out Any?>? {
fun proxyMultiThread(url: String, headers: Map<String, String>): Array<out Any?>? =
runBlocking {
proxy(url, headers)
}
/* val service = Executors.newFixedThreadPool(THREAD_NUM) suspend fun proxy(url: String, headers: Map<String, String>): Array<out Any?>? {
SpiderDebug.log("--proxyMultiThread: THREAD_NUM " + THREAD_NUM)*/
/* val service = Executors.newFixedThreadPool(THREAD_NUM)*/
SpiderDebug.log("--proxyMultiThread: THREAD_NUM " + THREAD_NUM)
try { try {
//缓存避免每次都请求total等信息 //缓存避免每次都请求total等信息
val info = infos[url] var info = infos[url]
if (info == null) { if (info == null) {
infos.clear() infos.clear()
infos[url] = getInfo(url, headers) info = getInfo(url, headers)
infos[url] = info
} }
val code = info?.get(0) as Int val code = info?.get(0) as Int
@ -57,9 +65,9 @@ object DownloadMT {
SpiderDebug.log("--文件总大小:$total") SpiderDebug.log("--文件总大小:$total")
//如果文件太小,也不走代理 //如果文件太小,也不走代理
if (total.toLong() < 1024 * 1024 * 100) { /* if (total.toLong() < 1024 * 1024 * 100) {
return proxy(url, headers) return proxy(url, headers)
} }*/
var range = var range =
if (StringUtils.isAllBlank(headers["range"])) headers["Range"] else headers["range"] if (StringUtils.isAllBlank(headers["range"])) headers["Range"] else headers["range"]
if (StringUtils.isAllBlank(range)) range = "bytes=0-"; if (StringUtils.isAllBlank(range)) range = "bytes=0-";
@ -102,30 +110,30 @@ object DownloadMT {
} }
channels[index].close() // 发送完成后关闭通道 channels[index].close() // 发送完成后关闭通道
SpiderDebug.log("---第" + index + "块下载完成" + ";Content-Range:" + res.headers()["Content-Range"]) SpiderDebug.log("---第" + index + "块下载完成" + ";Content-Range:" + res.headers()["Content-Range"])
SpiderDebug.log("---第" + index + "块下载完成" + ";content-range:" + res.headers()["content-range"])
} }
} }
} }
val outputStream = ByteArrayOutputStream(); val outputStream = ByteArrayOutputStream();
var pipedInputStream: ByteArrayInputStream? = null
var contentType: String? = ""
val res = CoroutineScope(Dispatchers.Default).async {
CoroutineScope(Dispatchers.Default).launch {
repeat(jobs.size) { index -> repeat(jobs.size) { index ->
launch {
for (bytes in channels[index]) { for (bytes in channels[index]) {
// 处理读取的数据 // 处理读取的数据
outputStream.write(bytes); outputStream.write(bytes);
} }
}
} }
// 等待所有下载完成 // 等待所有下载完成
jobs.joinAll() jobs.joinAll()
} }
res.await()
// SpiderDebug.log(" ++proxy res data:" + Json.toJson(response.body())); // SpiderDebug.log(" ++proxy res data:" + Json.toJson(response.body()));
var contentType: String? = resHeader["Content-Type"] contentType = resHeader["Content-Type"]
if (StringUtils.isAllBlank(contentType)) { if (StringUtils.isAllBlank(contentType)) {
contentType = resHeader["content-type"] contentType = resHeader["content-type"]
} }
@ -139,18 +147,22 @@ object DownloadMT {
respHeaders.put("Access-Control-Allow-Origin", "*");*/ respHeaders.put("Access-Control-Allow-Origin", "*");*/
resHeader["Content-Length"] = resHeader["Content-Length"] =
(partList[THREAD_NUM - 1][1] - partList[0][0] + 1).toString() (partList[THREAD_NUM - 1][1] - partList[0][0] + 1).toString()
// respHeaders.put("content-length", String.valueOf(bytes.length)); resHeader.remove("content-length")
resHeader["Content-Range"] = String.format( resHeader["Content-Range"] = String.format(
"bytes %s-%s/%s", partList[0][0], partList[THREAD_NUM - 1][1], total "bytes %s-%s/%s", partList[0][0], partList[THREAD_NUM - 1][1], total
) )
// respHeaders.put("content-range", String.format("bytes %s-%s/%s", partList.get(0)[0], partList.get(THREAD_NUM - 1)[1], total)); resHeader.remove("content-range")
SpiderDebug.log("----proxy res contentType:$contentType") SpiderDebug.log("----proxy res contentType:$contentType")
// SpiderDebug.log("++proxy res body:" + response.body()); // SpiderDebug.log("++proxy res body:" + response.body());
SpiderDebug.log("----proxy res respHeaders:" + Json.toJson(resHeader)) SpiderDebug.log("----proxy res respHeaders:" + Json.toJson(resHeader))
val pipedInputStream = ByteArrayInputStream(outputStream.toByteArray()); pipedInputStream = ByteArrayInputStream(outputStream.toByteArray());
outputStream.close() outputStream.close()
return arrayOf(206, contentType, pipedInputStream, resHeader) return arrayOf(206, contentType, pipedInputStream, resHeader)
} catch (e: Exception) { } catch (e: Exception) {
SpiderDebug.log("proxyMultiThread error:" + e.message) SpiderDebug.log("proxyMultiThread error:" + e.message)
e.printStackTrace() e.printStackTrace()

View File

@ -15,7 +15,8 @@ public class ProxyVideoTest {
// ProxyVideo.proxyMultiThread() // ProxyVideo.proxyMultiThread()
Server.get().start(); Server.get().start();
String url = ProxyVideo.buildCommonProxyUrl( String url = ProxyVideo.buildCommonProxyUrl(
"http://172.16.1.217:18089/ng-grid/video.mp4", new HashMap<>()); // "https://js.shipin520.com/pc/images/new/banner20250225.mp4", new HashMap<>());
"https://video.shipin520.com/videos/42/33/21/b_hsTXjZv04HeM1613423321_v1.mp4", new HashMap<>());
System.out.println(url); System.out.println(url);
while (true) { while (true) {

Binary file not shown.

View File

@ -1 +1 @@
3ae6f55c9957262dfad22101c095d543 3db9db0315bf011ce4677d9252ce0f13

View File

@ -1,5 +1,5 @@
{ {
"spider": "https://ghproxy.net/https://raw.githubusercontent.com/lushunming/AndroidCatVodSpider/multiThreadkt/jar/custom_spider.jar;md5;3ae6f55c9957262dfad22101c095d543", "spider": "https://ghproxy.net/https://raw.githubusercontent.com/lushunming/AndroidCatVodSpider/multiThreadkt/jar/custom_spider.jar;md5;3db9db0315bf011ce4677d9252ce0f13",
"lives": [ "lives": [
{ {
"name": "电视直播", "name": "电视直播",