Skip to content

Commit

Permalink
兼容阅读3api,完善网页阅读
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorqin committed Aug 13, 2021
1 parent d8a70b4 commit 821a281
Show file tree
Hide file tree
Showing 105 changed files with 13,225 additions and 95 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ out

#/src/main/resources/application-prod.yml
/src/main/resources/application-default.yml

/src/main/resources/web
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ apply plugin: 'io.spring.dependency-management'
apply plugin: 'kotlin'

group = 'org.lightink'
version = '0.0.1-SNAPSHOT'
version = '1.0.0'
sourceCompatibility = '1.8'

repositories {
Expand Down
Empty file added log/qingmo-2021-08-11.0.log
Empty file.
Empty file added log/qingmo-2021-08-12.0.log
Empty file.
Empty file added log/qingmo-2021-08-13.0.log
Empty file.
27 changes: 27 additions & 0 deletions src/main/java/org/lightink/reader/api/ReturnData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.lightink.reader.api


class ReturnData {

var isSuccess: Boolean = false
private set

var errorMsg: String = "未知错误,请联系开发者!"
private set

var data: Any? = null
private set

fun setErrorMsg(errorMsg: String): ReturnData {
this.isSuccess = false
this.errorMsg = errorMsg
return this
}

fun setData(data: Any): ReturnData {
this.isSuccess = true
this.errorMsg = ""
this.data = data
return this
}
}
943 changes: 884 additions & 59 deletions src/main/java/org/lightink/reader/api/YueduApi.kt

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/main/java/org/lightink/reader/config/AppConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.lightink.reader.config

import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.stereotype.Component

@Component
@ConfigurationProperties(prefix = "qingmo.app")
class AppConfig {
lateinit var storagePath: String // 存储路径
}
34 changes: 17 additions & 17 deletions src/main/java/org/lightink/reader/service/YueduSchedule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ class YueduSchedule {
@Autowired
private lateinit var webClient: WebClient

@Scheduled(cron = "0 0 2 * * ?")
@PostConstruct
fun getBookSource() {
webClient.getAbs(shuyuanUrl)
.send {
val map = mutableMapOf<String, String>()
it.result().bodyAsJsonArray()
.forEach {
val md5Encode = MD5Utils
.md5Encode(JsonObject.mapFrom(it).getString("bookSourceUrl")).toString()
map.put(md5Encode, it.toString())
}
Shuyuan.shuyuanlist = map
logger.info("shuyuan: {}", map)
}
}
// @Scheduled(cron = "0 0 2 * * ?")
// @PostConstruct
// fun getBookSource() {
// webClient.getAbs(shuyuanUrl)
// .send {
// val map = mutableMapOf<String, String>()
// it.result().bodyAsJsonArray()
// .forEach {
// val md5Encode = MD5Utils
// .md5Encode(JsonObject.mapFrom(it).getString("bookSourceUrl")).toString()
// map.put(md5Encode, it.toString())
// }
// Shuyuan.shuyuanlist = map
// logger.info("shuyuan: {}", map)
// }

// }

companion object Shuyuan {
var shuyuanlist = mutableMapOf<String, String>()
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/lightink/reader/service/yuedu/Debug.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ object Debug {
private suspend fun tocDebug(webBook: WebBook, book: Book) {
log(msg = "︾开始解析目录页")
runCatching {
webBook.getChapterList(book.tocUrl)
webBook.getChapterList(book)
}
.onSuccess { chapterList ->
chapterList?.let {
Expand All @@ -107,7 +107,7 @@ object Debug {
nextChapterUrl: String?
) {
log(webBook.sourceUrl, "︾开始解析正文页")
runCatching { webBook.getContent(bookChapter.url, nextChapterUrl) }
runCatching { webBook.getBookContent(bookChapter.url, nextChapterUrl) }
.onSuccess {
log(webBook.sourceUrl, "︽正文页解析完成")
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/lightink/reader/service/yuedu/WebBook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class WebBook(val bookSource: BookSource) {
suspend fun getBookInfo(bookUrl: String): Book {
val book = Book()
book.bookUrl = bookUrl
book.origin = bookSource.bookSourceUrl
book.originName = bookSource.bookSourceName
book.originOrder = bookSource.customOrder
book.type = bookSource.bookSourceType
val analyzeUrl = AnalyzeUrl(
book = book,
Expand All @@ -100,11 +103,8 @@ class WebBook(val bookSource: BookSource) {
* 目录
*/
suspend fun getChapterList(
tocUrl: String
book: Book
): List<BookChapter> {
val book = Book()
book.tocUrl = tocUrl
book.type = bookSource.bookSourceType
val body = if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) {
book.tocHtml
} else {
Expand All @@ -122,7 +122,7 @@ class WebBook(val bookSource: BookSource) {
/**
* 章节内容
*/
suspend fun getContent(
suspend fun getBookContent(
// book: Book?,
// bookChapter: BookChapter,
bookChapterUrl:String,
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/org/lightink/reader/service/yuedu/entities/Book.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ data class Book(
var coverUrl: String? = null, // 封面Url(书源获取)
var customCoverUrl: String? = null, // 封面Url(用户修改)
var intro: String? = null, // 简介内容(书源获取)
// var customIntro: String? = null, // 简介内容(用户修改)
// var charset: String? = null, // 自定义字符集名称(仅适用于本地书籍)
var customIntro: String? = null, // 简介内容(用户修改)
// var charset: String? = null, // 自定义字符集名称(仅适用于本地书籍)
var type: Int = 0, // @BookType
// var group: Int = 0, // 自定义分组索引号
var group: Int = 0, // 自定义分组索引号
var latestChapterTitle: String? = null, // 最新章节标题
var latestChapterTime: Long = System.currentTimeMillis(), // 最新章节标题更新时间
var lastCheckTime: Long = System.currentTimeMillis(), // 最近一次更新书籍信息的时间
var lastCheckCount: Int = 0, // 最近一次发现新章节的数量
var totalChapterNum: Int = 0, // 书籍目录总数
// var durChapterTitle: String? = null, // 当前章节名称
// var durChapterIndex: Int = 0, // 当前章节索引
// var durChapterPos: Int = 0, // 当前阅读的进度(首行字符的索引位置)
// var durChapterTime: Long = System.currentTimeMillis(), // 最近一次阅读书籍的时间(打开正文的时间)
var durChapterTitle: String? = null, // 当前章节名称
var durChapterIndex: Int = 0, // 当前章节索引
var durChapterPos: Int = 0, // 当前阅读的进度(首行字符的索引位置)
var durChapterTime: Long = System.currentTimeMillis(), // 最近一次阅读书籍的时间(打开正文的时间)
override var wordCount: String? = null,
// var canUpdate: Boolean = true, // 刷新书架时更新书籍信息
// var order: Int = 0, // 手动排序
// var originOrder: Int = 0, //书源排序
// var order: Int = 0, // 手动排序
var originOrder: Int = 0, //书源排序
var useReplaceRule: Boolean = true, // 正文使用净化替换规则
var variable: String? = null // 自定义书籍变量信息(用于书源规则检索书籍信息)
) : BaseBook {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object HttpHelper {
dispatcher.maxRequests = 200
dispatcher.maxRequestsPerHost = 200
val logging = HttpLoggingInterceptor()
logging.level = HttpLoggingInterceptor.Level.BODY
// logging.level = HttpLoggingInterceptor.Level.BODY

val builder = OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
Expand Down
128 changes: 127 additions & 1 deletion src/main/java/org/lightink/reader/utils/VertExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.google.gson.GsonBuilder
import io.vertx.core.Handler

import io.vertx.core.json.JsonObject
import io.vertx.core.json.JsonArray
import io.vertx.ext.web.RoutingContext
import io.vertx.kotlin.mysqlclient.preparedQueryAwait
import io.vertx.mysqlclient.MySQLPool
Expand All @@ -14,7 +15,13 @@ import mu.KotlinLogging
import org.lightink.reader.entity.BasicError
import java.net.URLDecoder
import java.net.URLEncoder

import java.io.File
import org.lightink.reader.config.AppConfig
import com.google.gson.reflect.TypeToken
import kotlin.reflect.KProperty1
import kotlin.reflect.KMutableProperty
import kotlin.reflect.full.memberProperties
import io.legado.app.data.entities.Book

/**
* @Auther: zoharSoul
Expand Down Expand Up @@ -56,3 +63,122 @@ fun RoutingContext.error(throwable: Throwable) {
.setStatusCode(500)
.end(errorJson)
}

fun saveStorage(name: String, value: Any) {
val toJson: String = if (value is JsonObject || value is JsonArray) {
value.toString()
} else {
gson.toJson(value)
}

var storagePath: String = "./storage"
logger.info("storagePath: {}", storagePath)
var storageDir = File(storagePath)
if (!storageDir.exists()) {
storageDir.mkdirs()
}

val file = File(storagePath + "/${name}.json")
logger.info("storage key: {} path: {}", name, file.absoluteFile)

if (!file.parentFile.exists()) {
file.parentFile.mkdirs()
}

if (!file.exists()) {
file.createNewFile()
}
file.writeText(toJson)
}

fun getStorage(name: String): String? {
var storagePath: String = "./storage"
logger.info("storagePath: {}", storagePath)
var storageDir = File(storagePath)
if (!storageDir.exists()) {
storageDir.mkdirs()
}

val file = File(storagePath + "/${name}.json")
logger.info("storage key: {} path: {}", name, file.absoluteFile)
if (!file.exists()) {
return null
}
return file.readText()
}

fun asJsonArray(value: Any?): JsonArray? {
if (value is JsonArray) {
return value
} else if (value is String) {
return JsonArray(value)
}
return null
}

fun asJsonObject(value: Any?): JsonObject? {
if (value is JsonObject) {
return value
} else if (value is String) {
return JsonObject(value)
}
return null
}

//convert a data class to a map
fun <T> T.serializeToMap(): Map<String, Any> {
return convert()
}

//convert string to a map
fun <T> T.toMap(): Map<String, Any> {
return convert()
}

//convert a map to a data class
inline fun <reified T> Map<String, Any>.toDataClass(): T {
return convert()
}

//convert an object of type I to type O
inline fun <I, reified O> I.convert(): O {
val json = if (this is String) {
this
} else {
gson.toJson(this)
}
return gson.fromJson(json, object : TypeToken<O>() {}.type)
}

@Suppress("UNCHECKED_CAST")
fun <R> readInstanceProperty(instance: Any, propertyName: String): R {
val property = instance::class.memberProperties
// don't cast here to <Any, R>, it would succeed silently
.first { it.name == propertyName } as KProperty1<Any, *>
// force a invalid cast exception if incorrect type here
return property.get(instance) as R
}

@Suppress("UNCHECKED_CAST")
fun setInstanceProperty(instance: Any, propertyName: String, propertyValue: Any) {
val property = instance::class.memberProperties
.first { it.name == propertyName }
if(property is KMutableProperty<*>) {
property.setter.call(instance, propertyValue)
}
}

fun Book.fillData(newBook: Book, keys: List<String>): Book {
keys.let {
for (key in it) {
var current = readInstanceProperty<String>(this, key)
if (current.isNullOrEmpty()) {
var cacheValue = readInstanceProperty<String>(newBook, key)
if (!cacheValue.isNullOrEmpty()) {
setInstanceProperty(this, key, cacheValue)
}
}
}
}
return this
}
12 changes: 12 additions & 0 deletions src/main/java/org/lightink/reader/verticle/RestVerticle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,16 @@ abstract class RestVerticle : CoroutineVerticle() {
}
}
}

fun Route.coroutineHandlerWithoutRes(fn: suspend (RoutingContext) -> Any) {
handler { ctx ->
val job = launch(Dispatchers.IO) {
try {
fn(ctx)
} catch (e: Exception) {
ctx.error(e)
}
}
}
}
}
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
qingmo:
app:
storagePath: './storage'

server:
url: http://localhost:8080

Expand Down
1 change: 1 addition & 0 deletions storage/bookInfoCache.json

Large diffs are not rendered by default.

Loading

0 comments on commit 821a281

Please sign in to comment.