pull/49/head
kunfei 5 years ago
parent 6c2ab77cbe
commit faa279f26e
  1. 17
      app/src/main/java/io/legado/app/help/http/AjaxWebView.kt
  2. 6
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt
  3. 3
      app/src/main/java/io/legado/app/help/http/Res.kt
  4. 6
      app/src/main/java/io/legado/app/model/Rss.kt
  5. 51
      app/src/main/java/io/legado/app/model/WebBook.kt
  6. 34
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  7. 6
      app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt
  8. 22
      app/src/main/java/io/legado/app/model/webbook/BookContent.kt
  9. 9
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt

@ -38,7 +38,7 @@ class AjaxWebView {
mWebView = createAjaxWebView(params, this)
}
MSG_SUCCESS -> {
ajaxWebView.callback?.onResult(msg.obj as Response)
ajaxWebView.callback?.onResult(msg.obj as Res)
destroyWebView()
}
MSG_ERROR -> {
@ -92,11 +92,10 @@ class AjaxWebView {
mHandler.obtainMessage(DESTROY_WEB_VIEW)
}
class AjaxParams(val url: String, private val tag: String) {
class AjaxParams(val url: String, private val tag: String?) {
var requestMethod = RequestMethod.GET
var postData: ByteArray? = null
var headerMap: Map<String, String>? = null
var cookieStore: CookieStore? = null
var sourceRegex: String? = null
var javaScript: String? = null
@ -116,9 +115,9 @@ class AjaxWebView {
get() = !TextUtils.isEmpty(sourceRegex)
fun setCookie(url: String) {
if (cookieStore != null) {
if (tag != null) {
val cookie = CookieManager.getInstance().getCookie(url)
cookieStore?.setCookie(tag, cookie)
CookieStore.setCookie(tag, cookie)
}
}
@ -185,7 +184,7 @@ class AjaxWebView {
mWebView.get()?.evaluateJavascript(mJavaScript) {
if (it.isNotEmpty() && it != "null") {
val content = StringEscapeUtils.unescapeJson(it)
handler.obtainMessage(MSG_SUCCESS, Response(url, content))
handler.obtainMessage(MSG_SUCCESS, Res(url, content))
.sendToTarget()
handler.removeCallbacks(this)
return@evaluateJavascript
@ -211,7 +210,7 @@ class AjaxWebView {
override fun onLoadResource(view: WebView, url: String) {
params.sourceRegex?.let {
if (url.matches(it.toRegex())) {
handler.obtainMessage(MSG_SUCCESS, Response(view.url ?: params.url, url))
handler.obtainMessage(MSG_SUCCESS, Res(view.url ?: params.url, url))
.sendToTarget()
}
}
@ -270,8 +269,6 @@ class AjaxWebView {
}
}
data class Response(val url: String, val content: String)
companion object {
const val MSG_AJAX_START = 0
const val MSG_SNIFF_START = 1
@ -282,7 +279,7 @@ class AjaxWebView {
}
abstract class Callback {
abstract fun onResult(response: Response)
abstract fun onResult(response: Res)
abstract fun onError(error: Throwable)
}
}

@ -74,21 +74,21 @@ object HttpHelper {
}
}
suspend fun ajax(params: AjaxWebView.AjaxParams): AjaxWebView.Response =
suspend fun ajax(params: AjaxWebView.AjaxParams): Res =
suspendCancellableCoroutine { block ->
val webView = AjaxWebView()
block.invokeOnCancellation {
webView.destroyWebView()
}
webView.callback = object : AjaxWebView.Callback() {
override fun onResult(response: AjaxWebView.Response) {
override fun onResult(response: Res) {
if (!block.isCompleted)
block.resume(response)
}
override fun onError(error: Throwable) {
if (!block.isCompleted)
block.resume(AjaxWebView.Response(params.url, error.localizedMessage))
block.resume(Res(params.url, error.localizedMessage))
}
}
webView.load(params)

@ -0,0 +1,3 @@
package io.legado.app.help.http
data class Res(val url: String, val body: String?)

@ -18,11 +18,7 @@ object Rss {
): Coroutine<MutableList<RssArticle>> {
return Coroutine.async(scope, context) {
val analyzeUrl = AnalyzeUrl(rssSource.sourceUrl)
val body = if (analyzeUrl.useWebView) {
analyzeUrl.getResultByWebView(rssSource.sourceUrl).content
} else {
analyzeUrl.getResponseAwait().body()
}
val body = analyzeUrl.getResponseAwait(rssSource.sourceUrl).body
RssParserByRule.parseXML(body, rssSource)
}
}

@ -10,7 +10,6 @@ import io.legado.app.model.webbook.BookChapterList
import io.legado.app.model.webbook.BookContent
import io.legado.app.model.webbook.BookInfo
import io.legado.app.model.webbook.BookList
import io.legado.app.utils.NetworkUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlin.coroutines.CoroutineContext
@ -38,21 +37,12 @@ class WebBook(val bookSource: BookSource) {
baseUrl = sourceUrl,
headerMapF = bookSource.getHeaderMap()
)
val baseUrl: String
val body = if (analyzeUrl.useWebView) {
val res = analyzeUrl.getResultByWebView(bookSource.bookSourceUrl)
baseUrl = res.url
res.content
} else {
val res = analyzeUrl.getResponseAwait()
baseUrl = NetworkUtils.getUrl(res)
res.body()
}
BookList.analyzeBookList(
body,
res.body,
bookSource,
analyzeUrl,
baseUrl,
res.url,
true
)
} ?: arrayListOf()
@ -75,21 +65,12 @@ class WebBook(val bookSource: BookSource) {
baseUrl = sourceUrl,
headerMapF = bookSource.getHeaderMap()
)
val baseUrl: String
val body = if (analyzeUrl.useWebView) {
val res = analyzeUrl.getResultByWebView(bookSource.bookSourceUrl)
baseUrl = res.url
res.content
} else {
val res = analyzeUrl.getResponseAwait()
baseUrl = NetworkUtils.getUrl(res)
res.body()
}
BookList.analyzeBookList(
body,
res.body,
bookSource,
analyzeUrl,
baseUrl,
res.url,
false
)
}
@ -114,12 +95,7 @@ class WebBook(val bookSource: BookSource) {
baseUrl = sourceUrl,
headerMapF = bookSource.getHeaderMap()
)
if (analyzeUrl.useWebView) {
bookSource.getContentRule()
analyzeUrl.getResultByWebView(bookSource.bookSourceUrl).content
} else {
analyzeUrl.getResponseAwait().body()
}
analyzeUrl.getResponseAwait().body
}
BookInfo.analyzeBookInfo(book, body, bookSource, book.bookUrl)
book
@ -139,17 +115,12 @@ class WebBook(val bookSource: BookSource) {
val body = if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) {
book.tocHtml
} else {
val analyzeUrl = AnalyzeUrl(
AnalyzeUrl(
book = book,
ruleUrl = book.tocUrl,
baseUrl = book.bookUrl,
headerMapF = bookSource.getHeaderMap()
)
if (analyzeUrl.useWebView) {
analyzeUrl.getResultByWebView(bookSource.bookSourceUrl).content
} else {
analyzeUrl.getResponseAwait().body()
}
).getResponseAwait().body
}
BookChapterList.analyzeChapterList(this, book, body, bookSource, book.tocUrl)
}
@ -179,15 +150,11 @@ class WebBook(val bookSource: BookSource) {
baseUrl = book.tocUrl,
headerMapF = bookSource.getHeaderMap()
)
if (analyzeUrl.useWebView) {
analyzeUrl.getResultByWebView(
analyzeUrl.getResponseAwait(
bookSource.bookSourceUrl,
jsStr = bookSource.getContentRule().webJs,
sourceRegex = bookSource.getContentRule().sourceRegex
).content
} else {
analyzeUrl.getResponseAwait().body()
}
).body
}
BookContent.analyzeContent(
this,

@ -13,13 +13,13 @@ import io.legado.app.help.JsExtensions
import io.legado.app.help.http.AjaxWebView
import io.legado.app.help.http.HttpHelper
import io.legado.app.help.http.RequestMethod
import io.legado.app.help.http.Res
import io.legado.app.utils.*
import okhttp3.FormBody
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import retrofit2.Call
import retrofit2.Response
import java.net.URLEncoder
import java.util.*
import java.util.regex.Pattern
@ -266,8 +266,21 @@ class AnalyzeUrl(
}
@Throws(Exception::class)
suspend fun getResponseAwait(): Response<String> {
return when {
suspend fun getResponseAwait(
tag: String? = null,
jsStr: String? = null,
sourceRegex: String? = null
): Res {
if (useWebView) {
val params = AjaxWebView.AjaxParams(url, tag)
params.headerMap = headerMap
params.requestMethod = method
params.javaScript = jsStr
params.sourceRegex = sourceRegex
params.postData = bodyTxt?.toByteArray()
return HttpHelper.ajax(params)
}
val res = when {
method == RequestMethod.POST -> {
if (fieldMap.isNotEmpty()) {
HttpHelper
@ -290,20 +303,7 @@ class AnalyzeUrl(
.getMapAsync(url, fieldMap, headerMap)
.await()
}
}
suspend fun getResultByWebView(
tag: String,
jsStr: String? = null,
sourceRegex: String? = null
): AjaxWebView.Response {
val params = AjaxWebView.AjaxParams(url, tag)
params.headerMap = headerMap
params.requestMethod = method
params.javaScript = jsStr
params.sourceRegex = sourceRegex
params.postData = bodyTxt?.toByteArray()
return HttpHelper.ajax(params)
return Res(NetworkUtils.getUrl(res), res.body())
}
}

@ -50,7 +50,7 @@ object BookChapterList {
AnalyzeUrl(
ruleUrl = nextUrl, book = book, headerMapF = bookSource.getHeaderMap()
).getResponseAwait()
.body()?.let { nextBody ->
.body?.let { nextBody ->
chapterData = analyzeChapterList(
nextBody, nextUrl, tocRule, listRule,
book, bookSource, log = false
@ -76,7 +76,7 @@ object BookChapterList {
ruleUrl = item.nextUrl,
book = book,
headerMapF = bookSource.getHeaderMap()
).getResponseAwait().body() ?: ""
).getResponseAwait().body
val nextChapterData = analyzeChapterList(
nextBody, item.nextUrl, tocRule, listRule, book, bookSource
)
@ -109,7 +109,7 @@ object BookChapterList {
private fun analyzeChapterList(
body: String,
body: String?,
baseUrl: String,
tocRule: TocRule,
listRule: String,

@ -55,16 +55,11 @@ object BookContent {
book = book,
headerMapF = bookSource.getHeaderMap()
).getResponseAwait()
.body()?.let { nextBody ->
.body?.let { nextBody ->
contentData =
analyzeContent(
nextBody,
contentRule,
book,
bookChapter,
bookSource,
baseUrl,
false
nextBody, contentRule, book,
bookChapter, bookSource, baseUrl, false
)
nextUrl =
if (contentData.nextUrl.isNotEmpty()) contentData.nextUrl[0] else ""
@ -85,16 +80,11 @@ object BookContent {
book = book,
headerMapF = bookSource.getHeaderMap()
).getResponseAwait()
.body()?.let {
.body?.let {
contentData =
analyzeContent(
it,
contentRule,
book,
bookChapter,
bookSource,
item.nextUrl,
false
it, contentRule, book, bookChapter,
bookSource, item.nextUrl, false
)
item.content = contentData.content
}

@ -54,11 +54,9 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
private fun loadContent(rssArticle: RssArticle, ruleContent: String) {
execute {
val analyzeUrl = AnalyzeUrl(rssArticle.link, baseUrl = rssArticle.origin)
val content = if (analyzeUrl.useWebView) {
analyzeUrl.getResultByWebView(rssArticle.origin).content
} else analyzeUrl.getResponseAwait().body()
content?.let { body ->
val body = AnalyzeUrl(rssArticle.link, baseUrl = rssArticle.origin)
.getResponseAwait()
.body
AnalyzeRule().apply {
setContent(
body,
@ -70,7 +68,6 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
}
}
}
}
fun upRssArticle(rssArticle: RssArticle, success: () -> Unit) {
execute {

Loading…
Cancel
Save