From ee75c3e40e4d0306355beec1e45d0373ac9ab0ec Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 6 Nov 2020 20:12:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/data/AppDatabase.kt | 11 +++++-- .../legado/app/data/entities/BookChapter.kt | 9 ++++-- .../app/model/webBook/BookChapterList.kt | 24 +++++++------- .../io/legado/app/model/webBook/WebBook.kt | 32 +++++++++++-------- .../read/page/provider/ChapterProvider.kt | 2 +- .../java/io/legado/app/utils/NetworkUtils.kt | 7 ++-- 6 files changed, 53 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt index f81783c27..706d08094 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -18,7 +18,7 @@ import java.util.* ReplaceRule::class, SearchBook::class, SearchKeyword::class, Cookie::class, RssSource::class, Bookmark::class, RssArticle::class, RssReadRecord::class, RssStar::class, TxtTocRule::class, ReadRecord::class, HttpTTS::class], - version = 22, + version = 23, exportSchema = true ) abstract class AppDatabase : RoomDatabase() { @@ -57,7 +57,8 @@ abstract class AppDatabase : RoomDatabase() { migration_18_19, migration_19_20, migration_20_21, - migration_21_22 + migration_21_22, + migration_22_23 ) .allowMainThreadQueries() .addCallback(dbCallback) @@ -194,6 +195,12 @@ abstract class AppDatabase : RoomDatabase() { database.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS `index_books_name_author` ON `books` (`name`, `author`) ") } } + + private val migration_22_23 = object : Migration(22, 23) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("ALTER TABLE chapters ADD baseUrl TEXT NOT NULL DEFAULT ''") + } + } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/BookChapter.kt b/app/src/main/java/io/legado/app/data/entities/BookChapter.kt index 4c120c0c9..ecc00633f 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookChapter.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookChapter.kt @@ -6,6 +6,7 @@ import androidx.room.ForeignKey import androidx.room.Ignore import androidx.room.Index import io.legado.app.utils.GSON +import io.legado.app.utils.NetworkUtils import io.legado.app.utils.fromJsonObject import kotlinx.android.parcel.IgnoredOnParcel import kotlinx.android.parcel.Parcelize @@ -26,13 +27,14 @@ import kotlinx.android.parcel.Parcelize ) // 删除书籍时自动删除章节 data class BookChapter( var url: String = "", // 章节地址 - var title: String = "", // 章节标题 + var title: String = "", // 章节标题 + var baseUrl: String = "", //用来拼接相对url var bookUrl: String = "", // 书籍地址 var index: Int = 0, // 章节序号 var resourceUrl: String? = null, // 音频真实URL var tag: String? = null, // var start: Long? = null, // 章节起始位置 - var end: Long? = null, // 章节终止位置 + var end: Long? = null, // 章节终止位置 var variable: String? = null //变量 ) : Parcelable { @@ -57,5 +59,8 @@ data class BookChapter( return false } + fun getAbsoluteURL(): String { + return NetworkUtils.getAbsoluteURL(baseUrl, url)!! + } } 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 42092355f..f50f36cf9 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 @@ -65,16 +65,16 @@ object BookChapterList { headerMapF = bookSource.getHeaderMap() ).getResponseAwait(bookSource.bookSourceUrl) .body?.let { nextBody -> - chapterData = analyzeChapterList( - book, nextUrl, nextBody, tocRule, listRule, bookSource - ) - nextUrl = if (chapterData.nextUrl.isNotEmpty()) { - chapterData.nextUrl[0] - } else "" - chapterData.chapterList?.let { - chapterList.addAll(it) + chapterData = analyzeChapterList( + book, nextUrl, nextBody, tocRule, listRule, bookSource + ) + nextUrl = if (chapterData.nextUrl.isNotEmpty()) { + chapterData.nextUrl[0] + } else "" + chapterData.chapterList?.let { + chapterList.addAll(it) + } } - } } Debug.log(bookSource.bookSourceUrl, "◇目录总页数:${nextUrlList.size}") block.resume(finish(book, chapterList, reverse)) @@ -241,13 +241,15 @@ object BookChapterList { var isVip: String? for (item in elements) { analyzeRule.setContent(item) - val bookChapter = BookChapter(bookUrl = book.bookUrl) + val bookChapter = BookChapter(bookUrl = book.bookUrl, baseUrl = baseUrl) analyzeRule.chapter = bookChapter bookChapter.title = analyzeRule.getString(nameRule) bookChapter.url = analyzeRule.getString(urlRule) bookChapter.tag = analyzeRule.getString(update) isVip = analyzeRule.getString(vipRule) - if (bookChapter.url.isEmpty()) bookChapter.url = baseUrl + if (bookChapter.url.isEmpty()) { + bookChapter.url = baseUrl + } if (bookChapter.title.isNotEmpty()) { if (isVip.isNotEmpty() && isVip != "null" && isVip != "false" && isVip != "0") { bookChapter.title = "\uD83D\uDD12" + bookChapter.title 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 3ae38d16e..5bbd695fd 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 @@ -128,18 +128,24 @@ class WebBook(val bookSource: BookSource) { ): Coroutine> { return Coroutine.async(scope, context) { book.type = bookSource.bookSourceType - val body = - if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) { - book.tocHtml - } else { - AnalyzeUrl( - book = book, - ruleUrl = book.tocUrl, - baseUrl = book.bookUrl, - headerMapF = bookSource.getHeaderMap() - ).getResponseAwait(bookSource.bookSourceUrl).body - } - BookChapterList.analyzeChapterList(this, book, body, bookSource, book.tocUrl) + if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) { + BookChapterList.analyzeChapterList( + this, + book, + book.tocHtml, + bookSource, + book.tocUrl + ) + } else { + val res = AnalyzeUrl( + book = book, + ruleUrl = book.tocUrl, + baseUrl = book.bookUrl, + headerMapF = bookSource.getHeaderMap() + ).getResponseAwait(bookSource.bookSourceUrl) + BookChapterList.analyzeChapterList(this, book, res.body, bookSource, res.url) + } + } } @@ -178,7 +184,7 @@ class WebBook(val bookSource: BookSource) { book.tocHtml } else { val analyzeUrl = AnalyzeUrl( - ruleUrl = bookChapter.url, + ruleUrl = bookChapter.getAbsoluteURL(), baseUrl = book.tocUrl, headerMapF = bookSource.getHeaderMap(), book = book, diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt index d1d40d6fc..7ea1e428a 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt @@ -102,7 +102,7 @@ object ChapterProvider { return TextChapter( bookChapter.index, bookChapter.title, - bookChapter.url, + bookChapter.getAbsoluteURL(), textPages, pageLines, pageLengths, diff --git a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt index 3b9ae2ed8..bc4436394 100644 --- a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt +++ b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt @@ -11,9 +11,10 @@ import java.util.regex.Pattern @Suppress("unused", "MemberVisibilityCanBePrivate") object NetworkUtils { fun getUrl(response: Response<*>): String { - val networkResponse = response.raw().networkResponse() - return networkResponse?.request()?.url()?.toString() - ?: response.raw().request().url().toString() + response.raw().networkResponse()?.let { + return it.request().url().toString() + } + return response.raw().request().url().toString() } private val notNeedEncoding: BitSet by lazy {