diff --git a/app/src/main/java/io/legado/app/help/http/StrResponse.kt b/app/src/main/java/io/legado/app/help/http/StrResponse.kt index 1b1f53395..0e11ea3db 100644 --- a/app/src/main/java/io/legado/app/help/http/StrResponse.kt +++ b/app/src/main/java/io/legado/app/help/http/StrResponse.kt @@ -21,11 +21,16 @@ class StrResponse { } constructor(url: String, body: String?) { + val request = try { + Request.Builder().url(url).build() + } catch (e: Exception) { + Request.Builder().url("http://localhost/").build() + } raw = Builder() .code(200) .message("OK") .protocol(Protocol.HTTP_1_1) - .request(Request.Builder().url(url).build()) + .request(request) .build() this.body = body } diff --git a/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt b/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt index 817038732..9d7729005 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt @@ -6,6 +6,7 @@ import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.rule.TocRule +import io.legado.app.help.http.StrResponse import io.legado.app.model.Debug import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeUrl @@ -21,16 +22,17 @@ object BookChapterList { suspend fun analyzeChapterList( scope: CoroutineScope, - book: Book, - body: String?, + strResponse: StrResponse, bookSource: BookSource, - baseUrl: String, + book: Book, redirectUrl: String ): List { - val chapterList = ArrayList() + val baseUrl = strResponse.url + val body = strResponse.body body ?: throw Exception( appCtx.getString(R.string.error_get_web_content, baseUrl) ) + val chapterList = ArrayList() Debug.log(bookSource.bookSourceUrl, "≡获取成功:${baseUrl}") Debug.log(bookSource.bookSourceUrl, body, state = 30) val tocRule = bookSource.getTocRule() diff --git a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt index 1d71effc6..f29f9dd81 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt @@ -7,6 +7,7 @@ import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.rule.ContentRule import io.legado.app.help.BookHelp +import io.legado.app.help.http.StrResponse import io.legado.app.model.Debug import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeUrl @@ -24,14 +25,15 @@ object BookContent { @Throws(Exception::class) suspend fun analyzeContent( scope: CoroutineScope, - body: String?, + strResponse: StrResponse, + bookSource: BookSource, book: Book, bookChapter: BookChapter, - bookSource: BookSource, - baseUrl: String, redirectUrl: String, nextChapterUrl: String? = null ): String { + val baseUrl = strResponse.url + val body = strResponse.body body ?: throw Exception( appCtx.getString(R.string.error_get_web_content, baseUrl) ) diff --git a/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt b/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt index df12185fc..625c27a5f 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt @@ -4,6 +4,7 @@ import io.legado.app.R import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookSource import io.legado.app.help.BookHelp +import io.legado.app.help.http.StrResponse import io.legado.app.model.Debug import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.utils.HtmlFormatter @@ -18,13 +19,14 @@ object BookInfo { @Throws(Exception::class) fun analyzeBookInfo( scope: CoroutineScope, - book: Book, - body: String?, + strResponse: StrResponse, bookSource: BookSource, - baseUrl: String, + book: Book, redirectUrl: String, canReName: Boolean, ) { + val baseUrl = strResponse.url + val body = strResponse.body body ?: throw Exception( appCtx.getString(R.string.error_get_web_content, baseUrl) ) diff --git a/app/src/main/java/io/legado/app/model/webBook/BookList.kt b/app/src/main/java/io/legado/app/model/webBook/BookList.kt index 2c1faab41..1fc3f9565 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookList.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookList.kt @@ -6,6 +6,7 @@ import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.rule.BookListRule import io.legado.app.help.BookHelp +import io.legado.app.help.http.StrResponse import io.legado.app.model.Debug import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeUrl @@ -21,20 +22,21 @@ object BookList { @Throws(Exception::class) fun analyzeBookList( scope: CoroutineScope, - body: String?, + strResponse: StrResponse, bookSource: BookSource, analyzeUrl: AnalyzeUrl, - baseUrl: String, variableBook: SearchBook, isSearch: Boolean = true, ): ArrayList { - val bookList = ArrayList() + val baseUrl = strResponse.url + val body = strResponse.body body ?: throw Exception( appCtx.getString( R.string.error_get_web_content, analyzeUrl.ruleUrl ) ) + val bookList = ArrayList() Debug.log(bookSource.bookSourceUrl, "≡获取成功:${analyzeUrl.ruleUrl}") Debug.log(bookSource.bookSourceUrl, body, state = 10) val analyzeRule = AnalyzeRule(variableBook) diff --git a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt index 75def476a..74e558aca 100644 --- a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt @@ -5,6 +5,7 @@ import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.SearchBook import io.legado.app.help.coroutine.Coroutine +import io.legado.app.help.http.StrResponse import io.legado.app.model.Debug import io.legado.app.model.analyzeRule.AnalyzeUrl import kotlinx.coroutines.CoroutineScope @@ -47,15 +48,7 @@ class WebBook(val bookSource: BookSource) { book = variableBook ) val res = analyzeUrl.getStrResponse(bookSource.bookSourceUrl) - return BookList.analyzeBookList( - scope, - res.body, - bookSource, - analyzeUrl, - res.url, - variableBook, - true - ) + return BookList.analyzeBookList(scope, res, bookSource, analyzeUrl, variableBook, true) } return arrayListOf() } @@ -88,15 +81,7 @@ class WebBook(val bookSource: BookSource) { headerMapF = bookSource.getHeaderMap() ) val res = analyzeUrl.getStrResponse(bookSource.bookSourceUrl) - return BookList.analyzeBookList( - scope, - res.body, - bookSource, - analyzeUrl, - res.url, - variableBook, - false - ) + return BookList.analyzeBookList(scope, res, bookSource, analyzeUrl, variableBook, false) } /** @@ -120,16 +105,8 @@ class WebBook(val bookSource: BookSource) { ): Book { book.type = bookSource.bookSourceType if (!book.infoHtml.isNullOrEmpty()) { - book.infoHtml - BookInfo.analyzeBookInfo( - scope, - book, - book.infoHtml, - bookSource, - book.bookUrl, - book.bookUrl, - canReName - ) + val strResponse = StrResponse(book.bookUrl, book.infoHtml) + BookInfo.analyzeBookInfo(scope, strResponse, bookSource, book, book.bookUrl, canReName) } else { val res = AnalyzeUrl( ruleUrl = book.bookUrl, @@ -137,15 +114,7 @@ class WebBook(val bookSource: BookSource) { headerMapF = bookSource.getHeaderMap(), book = book ).getStrResponse(bookSource.bookSourceUrl) - BookInfo.analyzeBookInfo( - scope, - book, - res.body, - bookSource, - book.bookUrl, - res.url, - canReName - ) + BookInfo.analyzeBookInfo(scope, res, bookSource, book, book.bookUrl, canReName) } return book } @@ -169,14 +138,8 @@ class WebBook(val bookSource: BookSource) { ): List { book.type = bookSource.bookSourceType return if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) { - BookChapterList.analyzeChapterList( - scope, - book, - book.tocHtml, - bookSource, - book.tocUrl, - book.tocUrl - ) + val strResponse = StrResponse(book.tocUrl, book.tocHtml) + BookChapterList.analyzeChapterList(scope, strResponse, bookSource, book, book.tocUrl) } else { val res = AnalyzeUrl( book = book, @@ -184,14 +147,7 @@ class WebBook(val bookSource: BookSource) { baseUrl = book.bookUrl, headerMapF = bookSource.getHeaderMap() ).getStrResponse(bookSource.bookSourceUrl) - BookChapterList.analyzeChapterList( - scope, - book, - res.body, - bookSource, - book.tocUrl, - res.url - ) + BookChapterList.analyzeChapterList(scope, res, bookSource, book, book.tocUrl) } } @@ -221,13 +177,13 @@ class WebBook(val bookSource: BookSource) { return bookChapter.url } return if (bookChapter.url == book.bookUrl && !book.tocHtml.isNullOrEmpty()) { + val strResponse = StrResponse(bookChapter.getAbsoluteURL(), book.tocHtml) BookContent.analyzeContent( scope, - book.tocHtml, + strResponse, + bookSource, book, bookChapter, - bookSource, - bookChapter.getAbsoluteURL(), bookChapter.getAbsoluteURL(), nextChapterUrl ) @@ -245,12 +201,11 @@ class WebBook(val bookSource: BookSource) { ) BookContent.analyzeContent( scope, - res.body, + res, + bookSource, book, bookChapter, - bookSource, bookChapter.getAbsoluteURL(), - res.url, nextChapterUrl ) }