pull/32/head
kunfei 5 years ago
parent a15ab83abb
commit 65b1001d1b
  1. 31
      app/src/main/java/io/legado/app/data/entities/rule/ExploreRule.kt
  2. 100
      app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt
  3. 68
      app/src/main/java/io/legado/app/model/webbook/BookContent.kt
  4. 16
      app/src/main/java/io/legado/app/model/webbook/BookInfo.kt
  5. 98
      app/src/main/java/io/legado/app/model/webbook/BookList.kt

@ -1,15 +1,22 @@
package io.legado.app.data.entities.rule
data class ExploreRule(
var exploreUrl: String? = null,
override var bookList: String? = null,
override var name: String? = null,
override var author: String? = null,
override var intro: String? = null,
override var kind: String? = null,
override var lastChapter: String? = null,
override var updateTime: String? = null,
override var bookUrl: String? = null,
override var coverUrl: String? = null,
override var wordCount: String? = null
) : BookListRule
var exploreUrl: String? = null,
override var bookList: String? = null,
override var name: String? = null,
override var author: String? = null,
override var intro: String? = null,
override var kind: String? = null,
override var lastChapter: String? = null,
override var updateTime: String? = null,
override var bookUrl: String? = null,
override var coverUrl: String? = null,
override var wordCount: String? = null
) : BookListRule {
data class ExploreUrl(
var title: String,
var url: String
)
}

@ -17,11 +17,11 @@ import retrofit2.Response
object BookChapterList {
suspend fun analyzeChapterList(
coroutineScope: CoroutineScope,
book: Book,
response: Response<String>,
bookSource: BookSource,
analyzeUrl: AnalyzeUrl
coroutineScope: CoroutineScope,
book: Book,
response: Response<String>,
bookSource: BookSource,
analyzeUrl: AnalyzeUrl
): List<BookChapter> {
var chapterList = arrayListOf<BookChapter>()
val baseUrl: String = NetworkUtils.getUrl(response)
@ -40,7 +40,7 @@ object BookChapterList {
listRule = listRule.substring(1)
}
var chapterData =
analyzeChapterList(body, baseUrl, tocRule, listRule, book, bookSource, printLog = true)
analyzeChapterList(body, baseUrl, tocRule, listRule, book, bookSource, printLog = true)
chapterData.chapterList?.let {
chapterList.addAll(it)
}
@ -49,26 +49,26 @@ object BookChapterList {
while (nextUrl.isNotEmpty() && !nextUrlList.contains(nextUrl)) {
nextUrlList.add(nextUrl)
AnalyzeUrl(
ruleUrl = nextUrl,
book = book,
headerMapF = bookSource.getHeaderMap()
ruleUrl = nextUrl,
book = book,
headerMapF = bookSource.getHeaderMap()
).getResponseAsync().await()
.body()?.let { nextBody ->
chapterData = analyzeChapterList(
nextBody,
nextUrl,
tocRule,
listRule,
book,
bookSource
)
nextUrl = if (chapterData.nextUrl.isNotEmpty())
chapterData.nextUrl[0]
else ""
chapterData.chapterList?.let {
chapterList.addAll(it)
}
.body()?.let { nextBody ->
chapterData = analyzeChapterList(
nextBody,
nextUrl,
tocRule,
listRule,
book,
bookSource
)
nextUrl = if (chapterData.nextUrl.isNotEmpty())
chapterData.nextUrl[0]
else ""
chapterData.chapterList?.let {
chapterList.addAll(it)
}
}
}
} else if (chapterData.nextUrl.size > 1) {
val chapterDataList = arrayListOf<ChapterData<String>>()
@ -79,18 +79,18 @@ object BookChapterList {
for (item in chapterDataList) {
withContext(coroutineScope.coroutineContext) {
val nextResponse = AnalyzeUrl(
ruleUrl = item.nextUrl,
book = book,
headerMapF = bookSource.getHeaderMap()
ruleUrl = item.nextUrl,
book = book,
headerMapF = bookSource.getHeaderMap()
).getResponseAsync().await()
val nextChapterData = analyzeChapterList(
nextResponse.body() ?: "",
item.nextUrl,
tocRule,
listRule,
book,
bookSource,
getNextUrl = false
nextResponse.body() ?: "",
item.nextUrl,
tocRule,
listRule,
book,
bookSource,
getNextUrl = false
)
item.chapterList = nextChapterData.chapterList
}
@ -121,14 +121,14 @@ object BookChapterList {
private fun analyzeChapterList(
body: String,
baseUrl: String,
tocRule: TocRule,
listRule: String,
book: Book,
bookSource: BookSource,
getNextUrl: Boolean = true,
printLog: Boolean = false
body: String,
baseUrl: String,
tocRule: TocRule,
listRule: String,
book: Book,
bookSource: BookSource,
getNextUrl: Boolean = true,
printLog: Boolean = false
): ChapterData<List<String>> {
val chapterList = arrayListOf<BookChapter>()
val nextUrlList = arrayListOf<String>()
@ -144,10 +144,10 @@ object BookChapterList {
}
}
SourceDebug.printLog(
bookSource.bookSourceUrl,
1,
TextUtils.join(",", nextUrlList),
printLog
bookSource.bookSourceUrl,
1,
TextUtils.join(",", nextUrlList),
printLog
)
}
SourceDebug.printLog(bookSource.bookSourceUrl, 1, "解析目录列表", printLog)
@ -169,10 +169,10 @@ object BookChapterList {
}
}
SourceDebug.printLog(
bookSource.bookSourceUrl,
1,
"${chapterList[0].title}${chapterList[0].url}",
printLog
bookSource.bookSourceUrl,
1,
"${chapterList[0].title}${chapterList[0].url}",
printLog
)
}
return ChapterData(chapterList, nextUrlList)

@ -18,20 +18,20 @@ object BookContent {
@Throws(Exception::class)
suspend fun analyzeContent(
coroutineScope: CoroutineScope,
response: Response<String>,
book: Book,
bookChapter: BookChapter,
bookSource: BookSource,
nextChapterUrlF: String? = null
coroutineScope: CoroutineScope,
response: Response<String>,
book: Book,
bookChapter: BookChapter,
bookSource: BookSource,
nextChapterUrlF: String? = null
): String {
val baseUrl: String = NetworkUtils.getUrl(response)
val body: String? = response.body()
body ?: throw Exception(
App.INSTANCE.getString(
R.string.error_get_web_content,
baseUrl
)
App.INSTANCE.getString(
R.string.error_get_web_content,
baseUrl
)
)
SourceDebug.printLog(bookSource.bookSourceUrl, 1, "获取成功:$baseUrl")
val content = StringBuilder()
@ -47,21 +47,21 @@ object BookContent {
App.db.bookChapterDao().getChapter(book.bookUrl, bookChapter.index + 1)?.url
while (nextUrl.isNotEmpty() && !nextUrlList.contains(nextUrl)) {
if (!nextChapterUrl.isNullOrEmpty()
&& NetworkUtils.getAbsoluteURL(baseUrl, nextUrl)
== NetworkUtils.getAbsoluteURL(baseUrl, nextChapterUrl)
&& NetworkUtils.getAbsoluteURL(baseUrl, nextUrl)
== NetworkUtils.getAbsoluteURL(baseUrl, nextChapterUrl)
) break
nextUrlList.add(nextUrl)
AnalyzeUrl(
ruleUrl = nextUrl,
book = book,
headerMapF = bookSource.getHeaderMap()
ruleUrl = nextUrl,
book = book,
headerMapF = bookSource.getHeaderMap()
).getResponseAsync().await()
.body()?.let { nextBody ->
contentData = analyzeContent(nextBody, contentRule, book, baseUrl)
nextUrl =
if (contentData.nextUrl.isNotEmpty()) contentData.nextUrl[0] else ""
content.append(contentData.content)
}
.body()?.let { nextBody ->
contentData = analyzeContent(nextBody, contentRule, book, baseUrl)
nextUrl =
if (contentData.nextUrl.isNotEmpty()) contentData.nextUrl[0] else ""
content.append(contentData.content)
}
}
} else if (contentData.nextUrl.size > 1) {
val contentDataList = arrayListOf<ContentData<String>>()
@ -72,15 +72,15 @@ object BookContent {
for (item in contentDataList) {
withContext(coroutineScope.coroutineContext) {
val nextResponse = AnalyzeUrl(
ruleUrl = item.nextUrl,
book = book,
headerMapF = bookSource.getHeaderMap()
ruleUrl = item.nextUrl,
book = book,
headerMapF = bookSource.getHeaderMap()
).getResponseAsync().await()
val nextContentData = analyzeContent(
nextResponse.body() ?: "",
contentRule,
book,
item.nextUrl
nextResponse.body() ?: "",
contentRule,
book,
item.nextUrl
)
item.content = nextContentData.content
}
@ -92,8 +92,8 @@ object BookContent {
if (content.isNotEmpty()) {
if (!content[0].toString().startsWith(bookChapter.title)) {
content
.insert(0, "\n")
.insert(0, bookChapter.title)
.insert(0, "\n")
.insert(0, bookChapter.title)
}
}
return content.toString()
@ -101,10 +101,10 @@ object BookContent {
@Throws(Exception::class)
private fun analyzeContent(
body: String,
contentRule: ContentRule,
book: Book,
baseUrl: String
body: String,
contentRule: ContentRule,
book: Book,
baseUrl: String
): ContentData<List<String>> {
val nextUrlList = arrayListOf<String>()
val analyzeRule = AnalyzeRule(book)

@ -11,17 +11,17 @@ object BookInfo {
@Throws(Exception::class)
fun analyzeBookInfo(
book: Book,
body: String?,
bookSource: BookSource,
analyzeUrl: AnalyzeUrl
book: Book,
body: String?,
bookSource: BookSource,
analyzeUrl: AnalyzeUrl
) {
val baseUrl = analyzeUrl.url
body ?: throw Exception(
App.INSTANCE.getString(
R.string.error_get_web_content,
baseUrl
)
App.INSTANCE.getString(
R.string.error_get_web_content,
baseUrl
)
)
SourceDebug.printLog(bookSource.bookSourceUrl, 1, "获取成功:$baseUrl")
val infoRule = bookSource.getBookInfoRule()

@ -14,19 +14,19 @@ object BookList {
@Throws(Exception::class)
fun analyzeBookList(
response: Response<String>,
bookSource: BookSource,
analyzeUrl: AnalyzeUrl,
isSearch: Boolean = true
response: Response<String>,
bookSource: BookSource,
analyzeUrl: AnalyzeUrl,
isSearch: Boolean = true
): ArrayList<SearchBook> {
val bookList = ArrayList<SearchBook>()
val baseUrl: String = NetworkUtils.getUrl(response)
val body: String? = response.body()
body ?: throw Exception(
App.INSTANCE.getString(
R.string.error_get_web_content,
baseUrl
)
App.INSTANCE.getString(
R.string.error_get_web_content,
baseUrl
)
)
SourceDebug.printLog(bookSource.bookSourceUrl, 1, "获取成功:$baseUrl")
val analyzeRule = AnalyzeRule(null)
@ -72,19 +72,19 @@ object BookList {
SourceDebug.printLog(bookSource.bookSourceUrl, 1, "列表数为${collections.size}")
for ((index, item) in collections.withIndex()) {
getSearchItem(
item,
analyzeRule,
bookSource,
baseUrl,
index == 0,
ruleName = ruleName,
ruleBookUrl = ruleBookUrl,
ruleAuthor = ruleAuthor,
ruleCoverUrl = ruleCoverUrl,
ruleIntro = ruleIntro,
ruleKind = ruleKind,
ruleLastChapter = ruleLastChapter,
ruleWordCount = ruleWordCount
item,
analyzeRule,
bookSource,
baseUrl,
index == 0,
ruleName = ruleName,
ruleBookUrl = ruleBookUrl,
ruleAuthor = ruleAuthor,
ruleCoverUrl = ruleCoverUrl,
ruleIntro = ruleIntro,
ruleKind = ruleKind,
ruleLastChapter = ruleLastChapter,
ruleWordCount = ruleWordCount
)?.let { searchBook ->
if (baseUrl == searchBook.bookUrl) {
searchBook.bookInfoHtml = body
@ -99,7 +99,11 @@ object BookList {
return bookList
}
private fun getInfoItem(analyzeRule: AnalyzeRule, bookSource: BookSource, baseUrl: String): SearchBook? {
private fun getInfoItem(
analyzeRule: AnalyzeRule,
bookSource: BookSource,
baseUrl: String
): SearchBook? {
val searchBook = SearchBook()
searchBook.bookUrl = baseUrl
searchBook.origin = bookSource.bookSourceUrl
@ -125,8 +129,10 @@ object BookList {
SourceDebug.printLog(bookSource.bookSourceUrl, 1, searchBook.kind ?: "")
SourceDebug.printLog(bookSource.bookSourceUrl, 1, "获取简介")
searchBook.intro = analyzeRule.getString(intro ?: "")
SourceDebug.printLog(bookSource.bookSourceUrl, 1, searchBook.intro
?: "", isHtml = true)
SourceDebug.printLog(
bookSource.bookSourceUrl, 1, searchBook.intro
?: "", isHtml = true
)
SourceDebug.printLog(bookSource.bookSourceUrl, 1, "获取字数")
searchBook.wordCount = analyzeRule.getString(wordCount ?: "")
SourceDebug.printLog(bookSource.bookSourceUrl, 1, searchBook.wordCount ?: "")
@ -135,8 +141,10 @@ object BookList {
SourceDebug.printLog(bookSource.bookSourceUrl, 1, searchBook.coverUrl ?: "")
SourceDebug.printLog(bookSource.bookSourceUrl, 1, "获取最新章节")
searchBook.latestChapterTitle = analyzeRule.getString(lastChapter ?: "")
SourceDebug.printLog(bookSource.bookSourceUrl, 1, searchBook.latestChapterTitle
?: "")
SourceDebug.printLog(
bookSource.bookSourceUrl, 1, searchBook.latestChapterTitle
?: ""
)
return searchBook
}
}
@ -144,19 +152,19 @@ object BookList {
}
private fun getSearchItem(
item: Any,
analyzeRule: AnalyzeRule,
bookSource: BookSource,
baseUrl: String,
printLog: Boolean,
ruleName: List<AnalyzeRule.SourceRule>,
ruleBookUrl: List<AnalyzeRule.SourceRule>,
ruleAuthor: List<AnalyzeRule.SourceRule>,
ruleKind: List<AnalyzeRule.SourceRule>,
ruleCoverUrl: List<AnalyzeRule.SourceRule>,
ruleWordCount: List<AnalyzeRule.SourceRule>,
ruleIntro: List<AnalyzeRule.SourceRule>,
ruleLastChapter: List<AnalyzeRule.SourceRule>
item: Any,
analyzeRule: AnalyzeRule,
bookSource: BookSource,
baseUrl: String,
printLog: Boolean,
ruleName: List<AnalyzeRule.SourceRule>,
ruleBookUrl: List<AnalyzeRule.SourceRule>,
ruleAuthor: List<AnalyzeRule.SourceRule>,
ruleKind: List<AnalyzeRule.SourceRule>,
ruleCoverUrl: List<AnalyzeRule.SourceRule>,
ruleWordCount: List<AnalyzeRule.SourceRule>,
ruleIntro: List<AnalyzeRule.SourceRule>,
ruleLastChapter: List<AnalyzeRule.SourceRule>
): SearchBook? {
val searchBook = SearchBook()
searchBook.origin = bookSource.bookSourceUrl
@ -179,8 +187,10 @@ object BookList {
SourceDebug.printLog(bookSource.bookSourceUrl, 1, searchBook.kind ?: "", printLog)
SourceDebug.printLog(bookSource.bookSourceUrl, 1, "获取简介", printLog)
searchBook.intro = analyzeRule.getString(ruleIntro)
SourceDebug.printLog(bookSource.bookSourceUrl, 1, searchBook.intro
?: "", printLog, true)
SourceDebug.printLog(
bookSource.bookSourceUrl, 1, searchBook.intro
?: "", printLog, true
)
SourceDebug.printLog(bookSource.bookSourceUrl, 1, "获取字数", printLog)
searchBook.wordCount = analyzeRule.getString(ruleWordCount)
SourceDebug.printLog(bookSource.bookSourceUrl, 1, searchBook.wordCount ?: "", printLog)
@ -189,8 +199,10 @@ object BookList {
SourceDebug.printLog(bookSource.bookSourceUrl, 1, searchBook.coverUrl ?: "", printLog)
SourceDebug.printLog(bookSource.bookSourceUrl, 1, "获取最新章节", printLog)
searchBook.latestChapterTitle = analyzeRule.getString(ruleLastChapter)
SourceDebug.printLog(bookSource.bookSourceUrl, 1, searchBook.latestChapterTitle
?: "", printLog)
SourceDebug.printLog(
bookSource.bookSourceUrl, 1, searchBook.latestChapterTitle
?: "", printLog
)
return searchBook
}
return null

Loading…
Cancel
Save