From 334356ce92c8dcc76a954b95b3ed9f110e4759f9 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 21 May 2021 08:53:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=B7=E6=96=B0=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E7=9A=84API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/api/ReaderProvider.kt | 15 +++-- ...okshelfController.kt => BookController.kt} | 59 ++++++++++++++++++- .../main/java/io/legado/app/web/HttpServer.kt | 29 ++++----- 3 files changed, 82 insertions(+), 21 deletions(-) rename app/src/main/java/io/legado/app/api/controller/{BookshelfController.kt => BookController.kt} (65%) diff --git a/app/src/main/java/io/legado/app/api/ReaderProvider.kt b/app/src/main/java/io/legado/app/api/ReaderProvider.kt index 897b0d757..27110af24 100644 --- a/app/src/main/java/io/legado/app/api/ReaderProvider.kt +++ b/app/src/main/java/io/legado/app/api/ReaderProvider.kt @@ -10,7 +10,7 @@ import android.database.Cursor import android.database.MatrixCursor import android.net.Uri import com.google.gson.Gson -import io.legado.app.api.controller.BookshelfController +import io.legado.app.api.controller.BookController import io.legado.app.api.controller.SourceController import java.util.* @@ -19,7 +19,8 @@ import java.util.* */ class ReaderProvider : ContentProvider() { private enum class RequestCode { - SaveSource, SaveSources, SaveBook, DeleteSources, GetSource, GetSources, GetBookshelf, GetChapterList, GetBookContent + SaveSource, SaveSources, SaveBook, DeleteSources, GetSource, GetSources, + GetBookshelf, RefreshToc, GetChapterList, GetBookContent } private val postBodyKey = "json" @@ -33,6 +34,7 @@ class ReaderProvider : ContentProvider() { addURI(authority, "source/query", RequestCode.GetSource.ordinal) addURI(authority, "sources/query", RequestCode.GetSources.ordinal) addURI(authority, "books/query", RequestCode.GetBookshelf.ordinal) + addURI(authority, "book/refreshToc/query", RequestCode.RefreshToc.ordinal) addURI(authority, "book/chapter/query", RequestCode.GetChapterList.ordinal) addURI(authority, "book/content/query", RequestCode.GetBookContent.ordinal) } @@ -65,7 +67,7 @@ class ReaderProvider : ContentProvider() { SourceController.saveSource(values.getAsString(postBodyKey)) } RequestCode.SaveBook -> values?.let { - BookshelfController.saveBook(values.getAsString(postBodyKey)) + BookController.saveBook(values.getAsString(postBodyKey)) } RequestCode.SaveSources -> values?.let { SourceController.saveSources(values.getAsString(postBodyKey)) @@ -91,9 +93,10 @@ class ReaderProvider : ContentProvider() { return if (sMatcher.match(uri) < 0) null else when (RequestCode.values()[sMatcher.match(uri)]) { RequestCode.GetSource -> SimpleCursor(SourceController.getSource(map)) RequestCode.GetSources -> SimpleCursor(SourceController.sources) - RequestCode.GetBookshelf -> SimpleCursor(BookshelfController.bookshelf) - RequestCode.GetBookContent -> SimpleCursor(BookshelfController.getBookContent(map)) - RequestCode.GetChapterList -> SimpleCursor(BookshelfController.getChapterList(map)) + RequestCode.GetBookshelf -> SimpleCursor(BookController.bookshelf) + RequestCode.GetBookContent -> SimpleCursor(BookController.getBookContent(map)) + RequestCode.RefreshToc -> SimpleCursor(BookController.refreshToc(map)) + RequestCode.GetChapterList -> SimpleCursor(BookController.getChapterList(map)) else -> throw IllegalStateException( "Unexpected value: " + RequestCode.values()[sMatcher.match(uri)].name ) diff --git a/app/src/main/java/io/legado/app/api/controller/BookshelfController.kt b/app/src/main/java/io/legado/app/api/controller/BookController.kt similarity index 65% rename from app/src/main/java/io/legado/app/api/controller/BookshelfController.kt rename to app/src/main/java/io/legado/app/api/controller/BookController.kt index 82005aac2..60e7c9ec8 100644 --- a/app/src/main/java/io/legado/app/api/controller/BookshelfController.kt +++ b/app/src/main/java/io/legado/app/api/controller/BookController.kt @@ -1,10 +1,12 @@ package io.legado.app.api.controller +import io.legado.app.R import io.legado.app.api.ReturnData import io.legado.app.constant.PreferKey import io.legado.app.data.appDb import io.legado.app.data.entities.Book import io.legado.app.help.BookHelp +import io.legado.app.model.localBook.LocalBook import io.legado.app.model.webBook.WebBook import io.legado.app.service.help.ReadBook import io.legado.app.utils.GSON @@ -14,8 +16,11 @@ import io.legado.app.utils.getPrefInt import kotlinx.coroutines.runBlocking import splitties.init.appCtx -object BookshelfController { +object BookController { + /** + * 书架所有书籍 + */ val bookshelf: ReturnData get() { val books = appDb.bookDao.all @@ -35,6 +40,55 @@ object BookshelfController { } } + /** + * 更新目录 + */ + fun refreshToc(parameters: Map>): ReturnData { + val returnData = ReturnData() + try { + val bookUrl = parameters["url"]?.getOrNull(0) + if (bookUrl.isNullOrEmpty()) { + return returnData.setErrorMsg("参数url不能为空,请指定书籍地址") + } + val book = appDb.bookDao.getBook(bookUrl) + ?: return returnData.setErrorMsg("bookUrl不对") + if (book.isLocalBook()) { + val toc = LocalBook.getChapterList(book) + appDb.bookChapterDao.delByBook(book.bookUrl) + appDb.bookChapterDao.insert(*toc.toTypedArray()) + appDb.bookDao.update(book) + return if (toc.isEmpty()) { + returnData.setErrorMsg(appCtx.getString(R.string.error_load_toc)) + } else { + returnData.setData(toc) + } + } else { + val bookSource = appDb.bookSourceDao.getBookSource(book.origin) + ?: return returnData.setErrorMsg("未找到对应书源,请换源") + val webBook = WebBook(bookSource) + val toc = runBlocking { + if (book.tocUrl.isBlank()) { + webBook.getBookInfoAwait(this, book) + } + webBook.getChapterListAwait(this, book) + } + appDb.bookChapterDao.delByBook(book.bookUrl) + appDb.bookChapterDao.insert(*toc.toTypedArray()) + appDb.bookDao.update(book) + return if (toc.isEmpty()) { + returnData.setErrorMsg(appCtx.getString(R.string.error_load_toc)) + } else { + returnData.setData(toc) + } + } + } catch (e: Exception) { + return returnData.setErrorMsg(e.localizedMessage ?: "refresh toc error") + } + } + + /** + * 获取目录 + */ fun getChapterList(parameters: Map>): ReturnData { val bookUrl = parameters["url"]?.getOrNull(0) val returnData = ReturnData() @@ -45,6 +99,9 @@ object BookshelfController { return returnData.setData(chapterList) } + /** + * 获取正文 + */ fun getBookContent(parameters: Map>): ReturnData { val bookUrl = parameters["url"]?.getOrNull(0) val index = parameters["index"]?.getOrNull(0)?.toInt() diff --git a/app/src/main/java/io/legado/app/web/HttpServer.kt b/app/src/main/java/io/legado/app/web/HttpServer.kt index 1b8b47eed..9b0d09e0e 100644 --- a/app/src/main/java/io/legado/app/web/HttpServer.kt +++ b/app/src/main/java/io/legado/app/web/HttpServer.kt @@ -3,7 +3,7 @@ package io.legado.app.web import com.google.gson.Gson import fi.iki.elonen.NanoHTTPD import io.legado.app.api.ReturnData -import io.legado.app.api.controller.BookshelfController +import io.legado.app.api.controller.BookController import io.legado.app.api.controller.SourceController import io.legado.app.web.utils.AssetsWeb import java.util.* @@ -32,25 +32,26 @@ class HttpServer(port: Int) : NanoHTTPD(port) { session.parseBody(files) val postData = files["postData"] - when (uri) { - "/saveSource" -> returnData = SourceController.saveSource(postData) - "/saveSources" -> returnData = SourceController.saveSources(postData) - "/saveBook" -> returnData = BookshelfController.saveBook(postData) - "/deleteSources" -> returnData = SourceController.deleteSources(postData) + returnData = when (uri) { + "/saveSource" -> SourceController.saveSource(postData) + "/saveSources" -> SourceController.saveSources(postData) + "/saveBook" -> BookController.saveBook(postData) + "/deleteSources" -> SourceController.deleteSources(postData) + else -> null } } "GET" -> { val parameters = session.parameters - when (uri) { - "/getSource" -> returnData = SourceController.getSource(parameters) - "/getSources" -> returnData = SourceController.sources - "/getBookshelf" -> returnData = BookshelfController.bookshelf - "/getChapterList" -> - returnData = BookshelfController.getChapterList(parameters) - "/getBookContent" -> - returnData = BookshelfController.getBookContent(parameters) + returnData = when (uri) { + "/getSource" -> SourceController.getSource(parameters) + "/getSources" -> SourceController.sources + "/getBookshelf" -> BookController.bookshelf + "/getChapterList" -> BookController.getChapterList(parameters) + "/refreshToc" -> BookController.refreshToc(parameters) + "/getBookContent" -> BookController.getBookContent(parameters) + else -> null } } }