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) mWebView = createAjaxWebView(params, this)
} }
MSG_SUCCESS -> { MSG_SUCCESS -> {
ajaxWebView.callback?.onResult(msg.obj as Response) ajaxWebView.callback?.onResult(msg.obj as Res)
destroyWebView() destroyWebView()
} }
MSG_ERROR -> { MSG_ERROR -> {
@ -92,11 +92,10 @@ class AjaxWebView {
mHandler.obtainMessage(DESTROY_WEB_VIEW) 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 requestMethod = RequestMethod.GET
var postData: ByteArray? = null var postData: ByteArray? = null
var headerMap: Map<String, String>? = null var headerMap: Map<String, String>? = null
var cookieStore: CookieStore? = null
var sourceRegex: String? = null var sourceRegex: String? = null
var javaScript: String? = null var javaScript: String? = null
@ -116,9 +115,9 @@ class AjaxWebView {
get() = !TextUtils.isEmpty(sourceRegex) get() = !TextUtils.isEmpty(sourceRegex)
fun setCookie(url: String) { fun setCookie(url: String) {
if (cookieStore != null) { if (tag != null) {
val cookie = CookieManager.getInstance().getCookie(url) val cookie = CookieManager.getInstance().getCookie(url)
cookieStore?.setCookie(tag, cookie) CookieStore.setCookie(tag, cookie)
} }
} }
@ -185,7 +184,7 @@ class AjaxWebView {
mWebView.get()?.evaluateJavascript(mJavaScript) { mWebView.get()?.evaluateJavascript(mJavaScript) {
if (it.isNotEmpty() && it != "null") { if (it.isNotEmpty() && it != "null") {
val content = StringEscapeUtils.unescapeJson(it) val content = StringEscapeUtils.unescapeJson(it)
handler.obtainMessage(MSG_SUCCESS, Response(url, content)) handler.obtainMessage(MSG_SUCCESS, Res(url, content))
.sendToTarget() .sendToTarget()
handler.removeCallbacks(this) handler.removeCallbacks(this)
return@evaluateJavascript return@evaluateJavascript
@ -211,7 +210,7 @@ class AjaxWebView {
override fun onLoadResource(view: WebView, url: String) { override fun onLoadResource(view: WebView, url: String) {
params.sourceRegex?.let { params.sourceRegex?.let {
if (url.matches(it.toRegex())) { 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() .sendToTarget()
} }
} }
@ -270,8 +269,6 @@ class AjaxWebView {
} }
} }
data class Response(val url: String, val content: String)
companion object { companion object {
const val MSG_AJAX_START = 0 const val MSG_AJAX_START = 0
const val MSG_SNIFF_START = 1 const val MSG_SNIFF_START = 1
@ -282,7 +279,7 @@ class AjaxWebView {
} }
abstract class Callback { abstract class Callback {
abstract fun onResult(response: Response) abstract fun onResult(response: Res)
abstract fun onError(error: Throwable) 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 -> suspendCancellableCoroutine { block ->
val webView = AjaxWebView() val webView = AjaxWebView()
block.invokeOnCancellation { block.invokeOnCancellation {
webView.destroyWebView() webView.destroyWebView()
} }
webView.callback = object : AjaxWebView.Callback() { webView.callback = object : AjaxWebView.Callback() {
override fun onResult(response: AjaxWebView.Response) { override fun onResult(response: Res) {
if (!block.isCompleted) if (!block.isCompleted)
block.resume(response) block.resume(response)
} }
override fun onError(error: Throwable) { override fun onError(error: Throwable) {
if (!block.isCompleted) if (!block.isCompleted)
block.resume(AjaxWebView.Response(params.url, error.localizedMessage)) block.resume(Res(params.url, error.localizedMessage))
} }
} }
webView.load(params) 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>> { ): Coroutine<MutableList<RssArticle>> {
return Coroutine.async(scope, context) { return Coroutine.async(scope, context) {
val analyzeUrl = AnalyzeUrl(rssSource.sourceUrl) val analyzeUrl = AnalyzeUrl(rssSource.sourceUrl)
val body = if (analyzeUrl.useWebView) { val body = analyzeUrl.getResponseAwait(rssSource.sourceUrl).body
analyzeUrl.getResultByWebView(rssSource.sourceUrl).content
} else {
analyzeUrl.getResponseAwait().body()
}
RssParserByRule.parseXML(body, rssSource) 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.BookContent
import io.legado.app.model.webbook.BookInfo import io.legado.app.model.webbook.BookInfo
import io.legado.app.model.webbook.BookList import io.legado.app.model.webbook.BookList
import io.legado.app.utils.NetworkUtils
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
@ -38,21 +37,12 @@ class WebBook(val bookSource: BookSource) {
baseUrl = sourceUrl, baseUrl = sourceUrl,
headerMapF = bookSource.getHeaderMap() 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() val res = analyzeUrl.getResponseAwait()
baseUrl = NetworkUtils.getUrl(res)
res.body()
}
BookList.analyzeBookList( BookList.analyzeBookList(
body, res.body,
bookSource, bookSource,
analyzeUrl, analyzeUrl,
baseUrl, res.url,
true true
) )
} ?: arrayListOf() } ?: arrayListOf()
@ -75,21 +65,12 @@ class WebBook(val bookSource: BookSource) {
baseUrl = sourceUrl, baseUrl = sourceUrl,
headerMapF = bookSource.getHeaderMap() 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() val res = analyzeUrl.getResponseAwait()
baseUrl = NetworkUtils.getUrl(res)
res.body()
}
BookList.analyzeBookList( BookList.analyzeBookList(
body, res.body,
bookSource, bookSource,
analyzeUrl, analyzeUrl,
baseUrl, res.url,
false false
) )
} }
@ -114,12 +95,7 @@ class WebBook(val bookSource: BookSource) {
baseUrl = sourceUrl, baseUrl = sourceUrl,
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
) )
if (analyzeUrl.useWebView) { analyzeUrl.getResponseAwait().body
bookSource.getContentRule()
analyzeUrl.getResultByWebView(bookSource.bookSourceUrl).content
} else {
analyzeUrl.getResponseAwait().body()
}
} }
BookInfo.analyzeBookInfo(book, body, bookSource, book.bookUrl) BookInfo.analyzeBookInfo(book, body, bookSource, book.bookUrl)
book book
@ -139,17 +115,12 @@ class WebBook(val bookSource: BookSource) {
val body = if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) { val body = if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) {
book.tocHtml book.tocHtml
} else { } else {
val analyzeUrl = AnalyzeUrl( AnalyzeUrl(
book = book, book = book,
ruleUrl = book.tocUrl, ruleUrl = book.tocUrl,
baseUrl = book.bookUrl, baseUrl = book.bookUrl,
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
) ).getResponseAwait().body
if (analyzeUrl.useWebView) {
analyzeUrl.getResultByWebView(bookSource.bookSourceUrl).content
} else {
analyzeUrl.getResponseAwait().body()
}
} }
BookChapterList.analyzeChapterList(this, book, body, bookSource, book.tocUrl) BookChapterList.analyzeChapterList(this, book, body, bookSource, book.tocUrl)
} }
@ -179,15 +150,11 @@ class WebBook(val bookSource: BookSource) {
baseUrl = book.tocUrl, baseUrl = book.tocUrl,
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
) )
if (analyzeUrl.useWebView) { analyzeUrl.getResponseAwait(
analyzeUrl.getResultByWebView(
bookSource.bookSourceUrl, bookSource.bookSourceUrl,
jsStr = bookSource.getContentRule().webJs, jsStr = bookSource.getContentRule().webJs,
sourceRegex = bookSource.getContentRule().sourceRegex sourceRegex = bookSource.getContentRule().sourceRegex
).content ).body
} else {
analyzeUrl.getResponseAwait().body()
}
} }
BookContent.analyzeContent( BookContent.analyzeContent(
this, this,

@ -13,13 +13,13 @@ import io.legado.app.help.JsExtensions
import io.legado.app.help.http.AjaxWebView import io.legado.app.help.http.AjaxWebView
import io.legado.app.help.http.HttpHelper import io.legado.app.help.http.HttpHelper
import io.legado.app.help.http.RequestMethod import io.legado.app.help.http.RequestMethod
import io.legado.app.help.http.Res
import io.legado.app.utils.* import io.legado.app.utils.*
import okhttp3.FormBody import okhttp3.FormBody
import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.RequestBody.Companion.toRequestBody
import retrofit2.Call import retrofit2.Call
import retrofit2.Response
import java.net.URLEncoder import java.net.URLEncoder
import java.util.* import java.util.*
import java.util.regex.Pattern import java.util.regex.Pattern
@ -266,8 +266,21 @@ class AnalyzeUrl(
} }
@Throws(Exception::class) @Throws(Exception::class)
suspend fun getResponseAwait(): Response<String> { suspend fun getResponseAwait(
return when { 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 -> { method == RequestMethod.POST -> {
if (fieldMap.isNotEmpty()) { if (fieldMap.isNotEmpty()) {
HttpHelper HttpHelper
@ -290,20 +303,7 @@ class AnalyzeUrl(
.getMapAsync(url, fieldMap, headerMap) .getMapAsync(url, fieldMap, headerMap)
.await() .await()
} }
} return Res(NetworkUtils.getUrl(res), res.body())
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)
} }
} }

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

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

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

Loading…
Cancel
Save