Skip to content

Commit

Permalink
feat: 使用okhttp+jsoup的方式代替jsoup自带的http请求
Browse files Browse the repository at this point in the history
  • Loading branch information
muedsa committed Nov 12, 2024
1 parent 4e4487f commit d33457e
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 46 deletions.
2 changes: 1 addition & 1 deletion TvBoxPlugin
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
android:value="com.muedsa.tvbox" />
<meta-data
android:name="tv_box_plugin_api_version"
android:value="4" />
android:value="5" />
<meta-data
android:name="tv_box_plugin_entry_point_impl"
android:value="com.muedsa.tvbox.an1me.An1mePlugin" />
Expand Down
20 changes: 11 additions & 9 deletions app/src/main/java/com/muedsa/tvbox/an1me/An1mePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,37 @@ import com.muedsa.tvbox.api.service.IMainScreenService
import com.muedsa.tvbox.api.service.IMediaDetailService
import com.muedsa.tvbox.api.service.IMediaSearchService
import com.muedsa.tvbox.tool.PluginCookieJar
import com.muedsa.tvbox.tool.PluginCookieStore
import com.muedsa.tvbox.tool.SharedCookieSaver
import com.muedsa.tvbox.tool.createOkHttpClient

class An1mePlugin(tvBoxContext: TvBoxContext) : IPlugin(tvBoxContext = tvBoxContext) {
override var options: PluginOptions = PluginOptions(enableDanDanPlaySearch = true)

private val cookieSaver by lazy { SharedCookieSaver(store = tvBoxContext.store) }
private val cookieStore by lazy { PluginCookieStore(saver = cookieSaver) }
private val cookieJar by lazy { PluginCookieJar(saver = cookieSaver) }
private val okHttpClient by lazy { createOkHttpClient(debug = tvBoxContext.debug, cookieJar = cookieJar) }
private val an1meService by lazy { An1meService() }
private val okHttpClient by lazy {
createOkHttpClient(
debug = tvBoxContext.debug,
cookieJar = PluginCookieJar(
saver = SharedCookieSaver(store = tvBoxContext.store)
)
)
}
private val an1meService by lazy { An1meService(okHttpClient = okHttpClient) }
private val mainScreenService by lazy {
MainScreenService(
an1meService = an1meService,
cookieStore = cookieStore
okHttpClient = okHttpClient
)
}
private val mediaDetailService by lazy {
MediaDetailService(
an1meService = an1meService,
cookieStore = cookieStore,
okHttpClient = okHttpClient
)
}
private val mediaSearchService by lazy {
MediaSearchService(
an1meService = an1meService,
cookieStore = cookieStore
okHttpClient = okHttpClient
)
}
override fun provideMainScreenService(): IMainScreenService = mainScreenService
Expand Down
16 changes: 12 additions & 4 deletions app/src/main/java/com/muedsa/tvbox/an1me/service/An1meService.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package com.muedsa.tvbox.an1me.service

import com.muedsa.tvbox.tool.feignChrome
import com.muedsa.tvbox.tool.get
import com.muedsa.tvbox.tool.parseHtml
import com.muedsa.tvbox.tool.toRequestBuild
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import okhttp3.HttpUrl.Companion.toHttpUrl
import org.jsoup.Jsoup
import okhttp3.OkHttpClient
import timber.log.Timber

class An1meService {
class An1meService(
private val okHttpClient: OkHttpClient
) {
private var siteUrl: String? = null
private val mutex = Mutex()

suspend fun getSiteUrl(): String = mutex.withLock {
if (siteUrl == null) {
val body = Jsoup.connect("https://1anime.org/")
val body = "https://1anime.org/".toRequestBuild()
.feignChrome()
.get()
.header("Connection", "close") // 这里使用http1.1
.header("http.protocol", "http/1.1") // 这里使用http1.1
.get(okHttpClient = okHttpClient)
.parseHtml()
.body()
val links = body.select(".release-main .links ul li a[href]")
.map { it.attr("href") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ import com.muedsa.tvbox.api.data.MediaCardRow
import com.muedsa.tvbox.api.data.MediaCardType
import com.muedsa.tvbox.api.service.IMainScreenService
import com.muedsa.tvbox.tool.feignChrome
import org.jsoup.Jsoup
import com.muedsa.tvbox.tool.get
import com.muedsa.tvbox.tool.parseHtml
import com.muedsa.tvbox.tool.toRequestBuild
import okhttp3.OkHttpClient
import org.jsoup.nodes.Element
import java.net.CookieStore

class MainScreenService(
private val an1meService: An1meService,
private val cookieStore: CookieStore
private val okHttpClient: OkHttpClient
) : IMainScreenService {

override suspend fun getRowsData(): List<MediaCardRow> {
val body = Jsoup.connect("${an1meService.getSiteUrl()}/")
.feignChrome(cookieStore = cookieStore)
.get()
val body = "${an1meService.getSiteUrl()}/".toRequestBuild()
.feignChrome()
.get(okHttpClient = okHttpClient)
.parseHtml()
.body()
val rows: MutableList<MediaCardRow> = mutableListOf()
body.select("#index-main >.content >.module")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ import com.muedsa.tvbox.tool.ChromeUserAgent
import com.muedsa.tvbox.tool.LenientJson
import com.muedsa.tvbox.tool.decodeBase64ToStr
import com.muedsa.tvbox.tool.feignChrome
import com.muedsa.tvbox.tool.get
import com.muedsa.tvbox.tool.parseHtml
import com.muedsa.tvbox.tool.toRequestBuild
import okhttp3.OkHttpClient
import okhttp3.Request
import org.jsoup.Jsoup
import org.jsoup.nodes.Element
import timber.log.Timber
import java.net.CookieStore

class MediaDetailService(
private val an1meService: An1meService,
private val cookieStore: CookieStore,
private val okHttpClient: OkHttpClient
) : IMediaDetailService {

override suspend fun getDetailData(mediaId: String, detailUrl: String): MediaDetail {
if (!detailUrl.startsWith("/voddetail/")) {
throw RuntimeException("不支持的类型 $detailUrl")
}
val body = Jsoup.connect("${an1meService.getSiteUrl()}$detailUrl")
.feignChrome(cookieStore = cookieStore)
.get()
val body = "${an1meService.getSiteUrl()}$detailUrl".toRequestBuild()
.feignChrome()
.get(okHttpClient = okHttpClient)
.parseHtml()
.body()
val boxEl = body.selectFirst("#main .content .box")!!
val imgUrl = boxEl.selectFirst(".module-item-cover .module-item-pic img")!!
Expand Down Expand Up @@ -163,9 +163,10 @@ class MediaDetailService(
throw RuntimeException("不支持的播放地址-> ${episode.flag5}")
}
val url = "${an1meService.getSiteUrl()}${episode.flag5}"
val body = Jsoup.connect(url)
.feignChrome(cookieStore = cookieStore)
.get()
val body = url.toRequestBuild()
.feignChrome()
.get(okHttpClient = okHttpClient)
.parseHtml()
.body()
val scriptEl = body.selectFirst("#main .player-wrapper script")!!
val result = An1meConst.PLAYER_INFO_REGEX.find(scriptEl.outerHtml())
Expand Down Expand Up @@ -298,14 +299,11 @@ class MediaDetailService(
referer: String? = null,
failureMsg: String = "请求失败"
): String {
val req = Request.Builder()
.url(url)
.apply {
referer?.let { addHeader("Referer", it) }
}
.addHeader("User-Agent", ChromeUserAgent)
.build()
val resp = okHttpClient.newCall(req).execute()
val resp = url.toRequestBuild()
.feignChrome(referer = referer)
.header("Connection", "close") // 这里使用http1.1
.header("http.protocol", "http/1.1") // 这里使用http1.1
.get(okHttpClient = okHttpClient)
if (!resp.isSuccessful) throw RuntimeException(failureMsg)
val bodyStr = resp.body?.string() ?: throw RuntimeException(failureMsg)
return bodyStr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import com.muedsa.tvbox.api.data.MediaCard
import com.muedsa.tvbox.api.data.MediaCardRow
import com.muedsa.tvbox.api.service.IMediaSearchService
import com.muedsa.tvbox.tool.feignChrome
import org.jsoup.Jsoup
import java.net.CookieStore
import com.muedsa.tvbox.tool.get
import com.muedsa.tvbox.tool.parseHtml
import com.muedsa.tvbox.tool.toRequestBuild
import okhttp3.OkHttpClient

class MediaSearchService(
private val an1meService: An1meService,
private val cookieStore: CookieStore
private val okHttpClient: OkHttpClient
) : IMediaSearchService {

override suspend fun searchMedias(query: String): MediaCardRow {
val body = Jsoup.connect("${an1meService.getSiteUrl()}/vodsearch/-------------.html?wd=$query")
.feignChrome(cookieStore = cookieStore)
.get()
val body = "${an1meService.getSiteUrl()}/vodsearch/-------------.html?wd=$query".toRequestBuild()
.feignChrome()
.get(okHttpClient = okHttpClient)
.parseHtml()
.body()
val moduleEl = body.selectFirst("#main .content .module")
?: return MediaCardRow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import org.junit.Test

class PlaySourceValidator {

private val an1meService = An1meService()
private val okHttpClient = createOkHttpClient(debug = true)
private val an1meService = An1meService(okHttpClient = okHttpClient)

@Test
fun addonsDpPlayerIndexPhp_valid() = runTest {
Expand Down

0 comments on commit d33457e

Please sign in to comment.