Merge pull request #369 from 1552980358/master

Modify code style of package `io.legado.app.data`
pull/370/head
kunfei 4 years ago committed by GitHub
commit 7d3faace74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 49
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  2. 78
      app/src/main/java/io/legado/app/data/entities/Book.kt
  3. 11
      app/src/main/java/io/legado/app/data/entities/BookChapter.kt
  4. 199
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  5. 43
      app/src/main/java/io/legado/app/data/entities/RssArticle.kt
  6. 105
      app/src/main/java/io/legado/app/data/entities/RssSource.kt
  7. 4
      app/src/main/java/io/legado/app/data/entities/RssStar.kt
  8. 75
      app/src/main/java/io/legado/app/data/entities/SearchBook.kt

@ -19,14 +19,14 @@ import io.legado.app.data.entities.*
version = 20, version = 20,
exportSchema = true exportSchema = true
) )
abstract class AppDatabase : RoomDatabase() { abstract class AppDatabase: RoomDatabase() {
companion object { companion object {
private const val DATABASE_NAME = "legado.db" private const val DATABASE_NAME = "legado.db"
fun createDatabase(context: Context): AppDatabase { fun createDatabase(context: Context) =
return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME) Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME)
.fallbackToDestructiveMigration() .fallbackToDestructiveMigration()
.addMigrations( .addMigrations(
migration_10_11, migration_10_11,
@ -41,9 +41,8 @@ abstract class AppDatabase : RoomDatabase() {
) )
.allowMainThreadQueries() .allowMainThreadQueries()
.build() .build()
}
private val migration_10_11 = object: Migration(10, 11) {
private val migration_10_11 = object : Migration(10, 11) {
override fun migrate(database: SupportSQLiteDatabase) { override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("DROP TABLE txtTocRules") database.execSQL("DROP TABLE txtTocRules")
database.execSQL( database.execSQL(
@ -55,20 +54,20 @@ abstract class AppDatabase : RoomDatabase() {
) )
} }
} }
private val migration_11_12 = object : Migration(11, 12) { private val migration_11_12 = object: Migration(11, 12) {
override fun migrate(database: SupportSQLiteDatabase) { override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE rssSources ADD style TEXT ") database.execSQL("ALTER TABLE rssSources ADD style TEXT ")
} }
} }
private val migration_12_13 = object : Migration(12, 13) { private val migration_12_13 = object: Migration(12, 13) {
override fun migrate(database: SupportSQLiteDatabase) { override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE rssSources ADD articleStyle INTEGER NOT NULL DEFAULT 0 ") database.execSQL("ALTER TABLE rssSources ADD articleStyle INTEGER NOT NULL DEFAULT 0 ")
} }
} }
private val migration_13_14 = object : Migration(13, 14) { private val migration_13_14 = object: Migration(13, 14) {
override fun migrate(database: SupportSQLiteDatabase) { override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL( database.execSQL(
""" """
@ -86,26 +85,26 @@ abstract class AppDatabase : RoomDatabase() {
database.execSQL("ALTER TABLE books_new RENAME TO books") database.execSQL("ALTER TABLE books_new RENAME TO books")
} }
} }
private val migration_14_15 = object : Migration(14, 15) { private val migration_14_15 = object: Migration(14, 15) {
override fun migrate(database: SupportSQLiteDatabase) { override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE bookmarks ADD bookAuthor TEXT NOT NULL DEFAULT ''") database.execSQL("ALTER TABLE bookmarks ADD bookAuthor TEXT NOT NULL DEFAULT ''")
} }
} }
private val migration_15_17 = object : Migration(15, 17) { private val migration_15_17 = object: Migration(15, 17) {
override fun migrate(database: SupportSQLiteDatabase) { override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("CREATE TABLE IF NOT EXISTS `readRecord` (`bookName` TEXT NOT NULL, `readTime` INTEGER NOT NULL, PRIMARY KEY(`bookName`))") database.execSQL("CREATE TABLE IF NOT EXISTS `readRecord` (`bookName` TEXT NOT NULL, `readTime` INTEGER NOT NULL, PRIMARY KEY(`bookName`))")
} }
} }
private val migration_17_18 = object : Migration(17, 18) { private val migration_17_18 = object: Migration(17, 18) {
override fun migrate(database: SupportSQLiteDatabase) { override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("CREATE TABLE IF NOT EXISTS `httpTTS` (`id` INTEGER NOT NULL, `name` TEXT NOT NULL, `url` TEXT NOT NULL, PRIMARY KEY(`id`))") database.execSQL("CREATE TABLE IF NOT EXISTS `httpTTS` (`id` INTEGER NOT NULL, `name` TEXT NOT NULL, `url` TEXT NOT NULL, PRIMARY KEY(`id`))")
} }
} }
private val migration_18_19 = object : Migration(18, 19) { private val migration_18_19 = object: Migration(18, 19) {
override fun migrate(database: SupportSQLiteDatabase) { override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("CREATE TABLE IF NOT EXISTS `readRecordNew` (`androidId` TEXT NOT NULL, `bookName` TEXT NOT NULL, `readTime` INTEGER NOT NULL, PRIMARY KEY(`androidId`, `bookName`))") database.execSQL("CREATE TABLE IF NOT EXISTS `readRecordNew` (`androidId` TEXT NOT NULL, `bookName` TEXT NOT NULL, `readTime` INTEGER NOT NULL, PRIMARY KEY(`androidId`, `bookName`))")
database.execSQL("INSERT INTO readRecordNew(androidId, bookName, readTime) select '${App.androidId}' as androidId, bookName, readTime from readRecord") database.execSQL("INSERT INTO readRecordNew(androidId, bookName, readTime) select '${App.androidId}' as androidId, bookName, readTime from readRecord")
@ -113,13 +112,13 @@ abstract class AppDatabase : RoomDatabase() {
database.execSQL("ALTER TABLE readRecordNew RENAME TO readRecord") database.execSQL("ALTER TABLE readRecordNew RENAME TO readRecord")
} }
} }
private val migration_19_20 = object : Migration(19,20) { private val migration_19_20 = object: Migration(19, 20) {
override fun migrate(database: SupportSQLiteDatabase) { override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE book_sources ADD bookSourceComment TEXT") database.execSQL("ALTER TABLE book_sources ADD bookSourceComment TEXT")
} }
} }
} }
abstract fun bookDao(): BookDao abstract fun bookDao(): BookDao
abstract fun bookGroupDao(): BookGroupDao abstract fun bookGroupDao(): BookGroupDao
abstract fun bookSourceDao(): BookSourceDao abstract fun bookSourceDao(): BookSourceDao

@ -55,93 +55,91 @@ data class Book(
var originOrder: Int = 0, //书源排序 var originOrder: Int = 0, //书源排序
var useReplaceRule: Boolean = AppConfig.replaceEnableDefault, // 正文使用净化替换规则 var useReplaceRule: Boolean = AppConfig.replaceEnableDefault, // 正文使用净化替换规则
var variable: String? = null // 自定义书籍变量信息(用于书源规则检索书籍信息) var variable: String? = null // 自定义书籍变量信息(用于书源规则检索书籍信息)
) : Parcelable, BaseBook { ): Parcelable, BaseBook {
fun isLocalBook(): Boolean { fun isLocalBook(): Boolean {
return origin == BookType.local return origin == BookType.local
} }
fun isLocalTxt(): Boolean { fun isLocalTxt(): Boolean {
return isLocalBook() && originName.endsWith(".txt", true) return isLocalBook() && originName.endsWith(".txt", true)
} }
fun isEpub(): Boolean { fun isEpub(): Boolean {
return originName.endsWith(".epub", true) return originName.endsWith(".epub", true)
} }
fun isOnLineTxt(): Boolean { fun isOnLineTxt(): Boolean {
return !isLocalBook() && type == 0 return !isLocalBook() && type == 0
} }
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (other is Book) { if (other is Book) {
return other.bookUrl == bookUrl return other.bookUrl == bookUrl
} }
return false return false
} }
override fun hashCode(): Int { override fun hashCode(): Int {
return bookUrl.hashCode() return bookUrl.hashCode()
} }
@delegate:Transient @delegate:Transient
@delegate:Ignore @delegate:Ignore
@IgnoredOnParcel @IgnoredOnParcel
override val variableMap by lazy { override val variableMap by lazy {
GSON.fromJsonObject<HashMap<String, String>>(variable) ?: HashMap() GSON.fromJsonObject<HashMap<String, String>>(variable) ?: HashMap()
} }
override fun putVariable(key: String, value: String) { override fun putVariable(key: String, value: String) {
variableMap[key] = value variableMap[key] = value
variable = GSON.toJson(variableMap) variable = GSON.toJson(variableMap)
} }
@Ignore @Ignore
@IgnoredOnParcel @IgnoredOnParcel
override var infoHtml: String? = null override var infoHtml: String? = null
@Ignore @Ignore
@IgnoredOnParcel @IgnoredOnParcel
override var tocHtml: String? = null override var tocHtml: String? = null
fun getRealAuthor() = author.replace(AppPattern.authorRegex, "") fun getRealAuthor() = author.replace(AppPattern.authorRegex, "")
fun getUnreadChapterNum() = max(totalChapterNum - durChapterIndex - 1, 0) fun getUnreadChapterNum() = max(totalChapterNum - durChapterIndex - 1, 0)
fun getDisplayCover() = if (customCoverUrl.isNullOrEmpty()) coverUrl else customCoverUrl fun getDisplayCover() = if (customCoverUrl.isNullOrEmpty()) coverUrl else customCoverUrl
fun getDisplayIntro() = if (customIntro.isNullOrEmpty()) intro else customIntro fun getDisplayIntro() = if (customIntro.isNullOrEmpty()) intro else customIntro
fun fileCharset(): Charset { fun fileCharset(): Charset {
return charset(charset ?: "UTF-8") return charset(charset ?: "UTF-8")
} }
fun getFolderName(): String { fun getFolderName(): String {
return name.replace(AppPattern.fileNameRegex, "") + MD5Utils.md5Encode16(bookUrl) return name.replace(AppPattern.fileNameRegex, "") + MD5Utils.md5Encode16(bookUrl)
} }
fun toSearchBook(): SearchBook { fun toSearchBook() = SearchBook(
return SearchBook( name = name,
name = name, author = author,
author = author, kind = kind,
kind = kind, bookUrl = bookUrl,
bookUrl = bookUrl, origin = origin,
origin = origin, originName = originName,
originName = originName, type = type,
type = type, wordCount = wordCount,
wordCount = wordCount, latestChapterTitle = latestChapterTitle,
latestChapterTitle = latestChapterTitle, coverUrl = coverUrl,
coverUrl = coverUrl, intro = intro,
intro = intro, tocUrl = tocUrl,
tocUrl = tocUrl, originOrder = originOrder,
originOrder = originOrder, variable = variable
variable = variable ).apply {
).apply { this.infoHtml = this@Book.infoHtml
this.infoHtml = this@Book.infoHtml this.tocHtml = this@Book.tocHtml
this.tocHtml = this@Book.tocHtml
}
} }
fun changeTo(newBook: Book) { fun changeTo(newBook: Book) {
newBook.group = group newBook.group = group
newBook.order = order newBook.order = order
@ -153,7 +151,7 @@ data class Book(
delete() delete()
App.db.bookDao().insert(newBook) App.db.bookDao().insert(newBook)
} }
fun delete() { fun delete() {
if (ReadBook.book?.bookUrl == bookUrl) { if (ReadBook.book?.bookUrl == bookUrl) {
ReadBook.book = null ReadBook.book = null

@ -52,16 +52,9 @@ data class BookChapter(
variable = GSON.toJson(variableMap) variable = GSON.toJson(variableMap)
} }
override fun hashCode(): Int { override fun hashCode() = url.hashCode()
return url.hashCode()
}
override fun equals(other: Any?): Boolean { override fun equals(other: Any?) = if (other is BookChapter) other.url == url else false
if (other is BookChapter) {
return other.url == url
}
return false
}
} }

@ -11,8 +11,8 @@ import io.legado.app.data.entities.rule.*
import io.legado.app.help.JsExtensions import io.legado.app.help.JsExtensions
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.android.parcel.Parcelize import kotlinx.android.parcel.Parcelize
import java.util.*
import javax.script.SimpleBindings import javax.script.SimpleBindings
import kotlin.collections.HashMap
@Parcelize @Parcelize
@TypeConverters(BookSource.Converters::class) @TypeConverters(BookSource.Converters::class)
@ -42,58 +42,42 @@ data class BookSource(
var ruleBookInfo: BookInfoRule? = null, // 书籍信息页规则 var ruleBookInfo: BookInfoRule? = null, // 书籍信息页规则
var ruleToc: TocRule? = null, // 目录页规则 var ruleToc: TocRule? = null, // 目录页规则
var ruleContent: ContentRule? = null // 正文页规则 var ruleContent: ContentRule? = null // 正文页规则
) : Parcelable, JsExtensions { ): Parcelable, JsExtensions {
override fun hashCode(): Int { override fun hashCode(): Int {
return bookSourceUrl.hashCode() return bookSourceUrl.hashCode()
} }
override fun equals(other: Any?): Boolean { override fun equals(other: Any?) = if (other is BookSource) other.bookSourceUrl == bookSourceUrl else false
if (other is BookSource) {
return other.bookSourceUrl == bookSourceUrl
}
return false
}
@Throws(Exception::class) @Throws(Exception::class)
fun getHeaderMap(): Map<String, String> { fun getHeaderMap() = (HashMap<String, String>().apply {
val headerMap = HashMap<String, String>() this[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: userAgent
headerMap[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: userAgent
header?.let { header?.let {
val header1 = when { GSON.fromJsonObject<Map<String, String>>(
it.startsWith("@js:", true) -> when {
evalJS(it.substring(4)).toString() it.startsWith("@js:", true) ->
it.startsWith("<js>", true) -> evalJS(it.substring(4)).toString()
evalJS(it.substring(4, it.lastIndexOf("<"))).toString() it.startsWith("<js>", true) ->
else -> it evalJS(it.substring(4, it.lastIndexOf("<"))).toString()
} else -> it
GSON.fromJsonObject<Map<String, String>>(header1)?.let { map -> }
headerMap.putAll(map) )?.let { map ->
putAll(map)
} }
} }
return headerMap }) as Map<String, String>
}
fun getSearchRule() = ruleSearch ?: SearchRule()
fun getSearchRule(): SearchRule {
return ruleSearch ?: SearchRule() fun getExploreRule() = ruleExplore ?: ExploreRule()
}
fun getBookInfoRule() = ruleBookInfo ?: BookInfoRule()
fun getExploreRule(): ExploreRule {
return ruleExplore ?: ExploreRule() fun getTocRule() = ruleToc ?: TocRule()
}
fun getContentRule() = ruleContent ?: ContentRule()
fun getBookInfoRule(): BookInfoRule {
return ruleBookInfo ?: BookInfoRule()
}
fun getTocRule(): TocRule {
return ruleToc ?: TocRule()
}
fun getContentRule(): ContentRule {
return ruleContent ?: ContentRule()
}
fun addGroup(group: String) { fun addGroup(group: String) {
bookSourceGroup?.let { bookSourceGroup?.let {
if (!it.contains(group)) { if (!it.contains(group)) {
@ -103,16 +87,15 @@ data class BookSource(
bookSourceGroup = group bookSourceGroup = group
} }
} }
fun removeGroup(group: String) { fun removeGroup(group: String) {
bookSourceGroup?.splitNotBlank("[,;,;]".toRegex())?.toHashSet()?.let { bookSourceGroup?.splitNotBlank("[,;,;]".toRegex())?.toHashSet()?.let {
it.remove(group) it.remove(group)
bookSourceGroup = TextUtils.join(",", it) bookSourceGroup = TextUtils.join(",", it)
} }
} }
fun getExploreKinds(): ArrayList<ExploreKind>? { fun getExploreKinds() = arrayListOf<ExploreKind>().apply {
val exploreKinds = arrayListOf<ExploreKind>()
exploreUrl?.let { exploreUrl?.let {
var a = it var a = it
if (a.isNotBlank()) { if (a.isNotBlank()) {
@ -135,16 +118,15 @@ data class BookSource(
b.forEach { c -> b.forEach { c ->
val d = c.split("::") val d = c.split("::")
if (d.size > 1) if (d.size > 1)
exploreKinds.add(ExploreKind(d[0], d[1])) add(ExploreKind(d[0], d[1]))
} }
} catch (e: Exception) { } catch (e: Exception) {
exploreKinds.add(ExploreKind(e.localizedMessage ?: "")) add(ExploreKind(e.localizedMessage ?: ""))
} }
} }
} }
return exploreKinds
} }
/** /**
* 执行JS * 执行JS
*/ */
@ -154,86 +136,63 @@ data class BookSource(
bindings["java"] = this bindings["java"] = this
return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings) return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings)
} }
fun equal(source: BookSource): Boolean { fun equal(source: BookSource) =
return equal(bookSourceName, source.bookSourceName) equal(bookSourceName, source.bookSourceName)
&& equal(bookSourceUrl, source.bookSourceUrl) && equal(bookSourceUrl, source.bookSourceUrl)
&& equal(bookSourceGroup, source.bookSourceGroup) && equal(bookSourceGroup, source.bookSourceGroup)
&& bookSourceType == source.bookSourceType && bookSourceType == source.bookSourceType
&& equal(bookUrlPattern, source.bookUrlPattern) && equal(bookUrlPattern, source.bookUrlPattern)
&& equal(bookSourceComment, source.bookSourceComment) && equal(bookSourceComment, source.bookSourceComment)
&& enabled == source.enabled && enabled == source.enabled
&& enabledExplore == source.enabledExplore && enabledExplore == source.enabledExplore
&& equal(header, source.header) && equal(header, source.header)
&& equal(loginUrl, source.loginUrl) && equal(loginUrl, source.loginUrl)
&& equal(exploreUrl, source.exploreUrl) && equal(exploreUrl, source.exploreUrl)
&& equal(searchUrl, source.searchUrl) && equal(searchUrl, source.searchUrl)
&& getSearchRule() == source.getSearchRule() && getSearchRule() == source.getSearchRule()
&& getExploreRule() == source.getExploreRule() && getExploreRule() == source.getExploreRule()
&& getBookInfoRule() == source.getBookInfoRule() && getBookInfoRule() == source.getBookInfoRule()
&& getTocRule() == source.getTocRule() && getTocRule() == source.getTocRule()
&& getContentRule() == source.getContentRule() && getContentRule() == source.getContentRule()
}
private fun equal(a: String?, b: String?) = a == b || (a.isNullOrEmpty() && b.isNullOrEmpty())
private fun equal(a: String?, b: String?): Boolean {
return a == b || (a.isNullOrEmpty() && b.isNullOrEmpty())
}
data class ExploreKind( data class ExploreKind(
var title: String, var title: String,
var url: String? = null var url: String? = null
) )
class Converters { class Converters {
@TypeConverter @TypeConverter
fun exploreRuleToString(exploreRule: ExploreRule?): String? { fun exploreRuleToString(exploreRule: ExploreRule?) = GSON.toJson(exploreRule)
return GSON.toJson(exploreRule)
}
@TypeConverter @TypeConverter
fun stringToExploreRule(json: String?): ExploreRule? { fun stringToExploreRule(json: String?) = GSON.fromJsonObject<ExploreRule>(json)
return GSON.fromJsonObject<ExploreRule>(json)
}
@TypeConverter @TypeConverter
fun searchRuleToString(searchRule: SearchRule?): String? { fun searchRuleToString(searchRule: SearchRule?) = GSON.toJson(searchRule)
return GSON.toJson(searchRule)
}
@TypeConverter @TypeConverter
fun stringToSearchRule(json: String?): SearchRule? { fun stringToSearchRule(json: String?) = GSON.fromJsonObject<SearchRule>(json)
return GSON.fromJsonObject<SearchRule>(json)
}
@TypeConverter @TypeConverter
fun bookInfoRuleToString(bookInfoRule: BookInfoRule?): String? { fun bookInfoRuleToString(bookInfoRule: BookInfoRule?) = GSON.toJson(bookInfoRule)
return GSON.toJson(bookInfoRule)
}
@TypeConverter @TypeConverter
fun stringToBookInfoRule(json: String?): BookInfoRule? { fun stringToBookInfoRule(json: String?) = GSON.fromJsonObject<BookInfoRule>(json)
return GSON.fromJsonObject<BookInfoRule>(json)
}
@TypeConverter @TypeConverter
fun tocRuleToString(tocRule: TocRule?): String? { fun tocRuleToString(tocRule: TocRule?) = GSON.toJson(tocRule)
return GSON.toJson(tocRule)
}
@TypeConverter @TypeConverter
fun stringToTocRule(json: String?): TocRule? { fun stringToTocRule(json: String?) = GSON.fromJsonObject<TocRule>(json)
return GSON.fromJsonObject<TocRule>(json)
}
@TypeConverter @TypeConverter
fun contentRuleToString(contentRule: ContentRule?): String? { fun contentRuleToString(contentRule: ContentRule?) = GSON.toJson(contentRule)
return GSON.toJson(contentRule)
}
@TypeConverter @TypeConverter
fun stringToContentRule(json: String?): ContentRule? { fun stringToContentRule(json: String?) = GSON.fromJsonObject<ContentRule>(json)
return GSON.fromJsonObject<ContentRule>(json)
}
} }
} }

@ -19,32 +19,23 @@ data class RssArticle(
var image: String? = null, var image: String? = null,
var read: Boolean = false var read: Boolean = false
) { ) {
override fun hashCode(): Int { override fun hashCode() = link.hashCode()
return link.hashCode()
}
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (other == null) { other ?: return false
return false return if (other is RssArticle) origin == other.origin && link == other.link else false
}
if (other is RssArticle) {
return origin == other.origin && link == other.link
}
return false
}
fun toStar(): RssStar {
return RssStar(
origin = origin,
sort = sort,
title = title,
starTime = System.currentTimeMillis(),
link = link,
pubDate = pubDate,
description = description,
content = content,
image = image
)
} }
fun toStar() = RssStar(
origin = origin,
sort = sort,
title = title,
starTime = System.currentTimeMillis(),
link = link,
pubDate = pubDate,
description = description,
content = content,
image = image
)
} }

@ -39,80 +39,67 @@ data class RssSource(
var header: String? = null, var header: String? = null,
var enableJs: Boolean = false, var enableJs: Boolean = false,
var loadWithBaseUrl: Boolean = false, var loadWithBaseUrl: Boolean = false,
var customOrder: Int = 0 var customOrder: Int = 0
) : Parcelable, JsExtensions { ): Parcelable, JsExtensions {
override fun equals(other: Any?): Boolean { override fun equals(other: Any?) = if (other is RssSource) other.sourceUrl == sourceUrl else false
if (other is RssSource) {
return other.sourceUrl == sourceUrl override fun hashCode() = sourceUrl.hashCode()
}
return false
}
override fun hashCode(): Int {
return sourceUrl.hashCode()
}
@Throws(Exception::class) @Throws(Exception::class)
fun getHeaderMap(): Map<String, String> { fun getHeaderMap() = HashMap<String, String>().apply {
val headerMap = HashMap<String, String>() this[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: AppConst.userAgent
headerMap[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: AppConst.userAgent
header?.let { header?.let {
val header1 = when { GSON.fromJsonObject<Map<String, String>>(
it.startsWith("@js:", true) -> when {
evalJS(it.substring(4)).toString() it.startsWith("@js:", true) ->
it.startsWith("<js>", true) -> evalJS(it.substring(4)).toString()
evalJS(it.substring(4, it.lastIndexOf("<"))).toString() it.startsWith("<js>", true) ->
else -> it evalJS(it.substring(4, it.lastIndexOf("<"))).toString()
} else -> it
GSON.fromJsonObject<Map<String, String>>(header1)?.let { map -> }
headerMap.putAll(map) )?.let { map ->
putAll(map)
} }
} }
return headerMap
} }
/** /**
* 执行JS * 执行JS
*/ */
@Throws(Exception::class) @Throws(Exception::class)
private fun evalJS(jsStr: String): Any { private fun evalJS(jsStr: String): Any = AppConst.SCRIPT_ENGINE.eval(jsStr, SimpleBindings().apply { this["java"] = this@RssSource })
val bindings = SimpleBindings()
bindings["java"] = this
return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings)
}
fun equal(source: RssSource): Boolean { fun equal(source: RssSource): Boolean {
return equal(sourceUrl, source.sourceUrl) return equal(sourceUrl, source.sourceUrl)
&& equal(sourceIcon, source.sourceIcon) && equal(sourceIcon, source.sourceIcon)
&& enabled == source.enabled && enabled == source.enabled
&& equal(sourceGroup, source.sourceGroup) && equal(sourceGroup, source.sourceGroup)
&& equal(ruleArticles, source.ruleArticles) && equal(ruleArticles, source.ruleArticles)
&& equal(ruleNextPage, source.ruleNextPage) && equal(ruleNextPage, source.ruleNextPage)
&& equal(ruleTitle, source.ruleTitle) && equal(ruleTitle, source.ruleTitle)
&& equal(rulePubDate, source.rulePubDate) && equal(rulePubDate, source.rulePubDate)
&& equal(ruleDescription, source.ruleDescription) && equal(ruleDescription, source.ruleDescription)
&& equal(ruleLink, source.ruleLink) && equal(ruleLink, source.ruleLink)
&& equal(ruleContent, source.ruleContent) && equal(ruleContent, source.ruleContent)
&& enableJs == source.enableJs && enableJs == source.enableJs
&& loadWithBaseUrl == source.loadWithBaseUrl && loadWithBaseUrl == source.loadWithBaseUrl
} }
private fun equal(a: String?, b: String?): Boolean { private fun equal(a: String?, b: String?): Boolean {
return a == b || (a.isNullOrEmpty() && b.isNullOrEmpty()) return a == b || (a.isNullOrEmpty() && b.isNullOrEmpty())
} }
fun sortUrls(): LinkedHashMap<String, String> { fun sortUrls(): LinkedHashMap<String, String> =
val sortMap = linkedMapOf<String, String>() linkedMapOf<String, String>().apply {
sortUrl?.split("(&&|\n)+".toRegex())?.forEach { c -> sortUrl?.split("(&&|\n)+".toRegex())?.forEach { c ->
val d = c.split("::") val d = c.split("::")
if (d.size > 1) if (d.size > 1)
sortMap[d[0]] = d[1] this[d[0]] = d[1]
} }
if (sortMap.isEmpty()) { if (isEmpty()) {
sortMap[""] = sourceUrl this[""] = sourceUrl
}
} }
return sortMap
}
} }

@ -18,8 +18,7 @@ data class RssStar(
var content: String? = null, var content: String? = null,
var image: String? = null var image: String? = null
) { ) {
fun toRssArticle(): RssArticle { fun toRssArticle() = RssArticle(
return RssArticle(
origin = origin, origin = origin,
sort = sort, sort = sort,
title = title, title = title,
@ -29,5 +28,4 @@ data class RssStar(
content = content, content = content,
image = image image = image
) )
}
} }

@ -36,54 +36,45 @@ data class SearchBook(
var time: Long = System.currentTimeMillis(), var time: Long = System.currentTimeMillis(),
var variable: String? = null, var variable: String? = null,
var originOrder: Int = 0 var originOrder: Int = 0
) : Parcelable, BaseBook, Comparable<SearchBook> { ): Parcelable, BaseBook, Comparable<SearchBook> {
@Ignore @Ignore
@IgnoredOnParcel @IgnoredOnParcel
override var infoHtml: String? = null override var infoHtml: String? = null
@Ignore @Ignore
@IgnoredOnParcel @IgnoredOnParcel
override var tocHtml: String? = null override var tocHtml: String? = null
override fun equals(other: Any?): Boolean { override fun equals(other: Any?) = other is SearchBook && other.bookUrl == bookUrl
if (other is SearchBook) {
if (other.bookUrl == bookUrl) { override fun hashCode() = bookUrl.hashCode()
return true
}
}
return false
}
override fun hashCode(): Int {
return bookUrl.hashCode()
}
override fun compareTo(other: SearchBook): Int { override fun compareTo(other: SearchBook): Int {
return other.originOrder - this.originOrder return other.originOrder - this.originOrder
} }
@delegate:Transient @delegate:Transient
@delegate:Ignore @delegate:Ignore
@IgnoredOnParcel @IgnoredOnParcel
override val variableMap by lazy { override val variableMap by lazy {
GSON.fromJsonObject<HashMap<String, String>>(variable) ?: HashMap() GSON.fromJsonObject<HashMap<String, String>>(variable) ?: HashMap()
} }
override fun putVariable(key: String, value: String) { override fun putVariable(key: String, value: String) {
variableMap[key] = value variableMap[key] = value
variable = GSON.toJson(variableMap) variable = GSON.toJson(variableMap)
} }
@delegate:Transient @delegate:Transient
@delegate:Ignore @delegate:Ignore
@IgnoredOnParcel @IgnoredOnParcel
val origins: LinkedHashSet<String> by lazy { linkedSetOf(origin) } val origins: LinkedHashSet<String> by lazy { linkedSetOf(origin) }
fun addOrigin(origin: String) { fun addOrigin(origin: String) {
origins.add(origin) origins.add(origin)
} }
fun getDisplayLastChapterTitle(): String { fun getDisplayLastChapterTitle(): String {
latestChapterTitle?.let { latestChapterTitle?.let {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
@ -92,26 +83,24 @@ data class SearchBook(
} }
return "无最新章节" return "无最新章节"
} }
fun toBook(): Book { fun toBook() = Book(
return Book( name = name,
name = name, author = author,
author = author, kind = kind,
kind = kind, bookUrl = bookUrl,
bookUrl = bookUrl, origin = origin,
origin = origin, originName = originName,
originName = originName, type = type,
type = type, wordCount = wordCount,
wordCount = wordCount, latestChapterTitle = latestChapterTitle,
latestChapterTitle = latestChapterTitle, coverUrl = coverUrl,
coverUrl = coverUrl, intro = intro,
intro = intro, tocUrl = tocUrl,
tocUrl = tocUrl, originOrder = originOrder,
originOrder = originOrder, variable = variable
variable = variable ).apply {
).apply { this.infoHtml = this@SearchBook.infoHtml
this.infoHtml = this@SearchBook.infoHtml this.tocUrl = this@SearchBook.tocUrl
this.tocUrl = this@SearchBook.tocUrl
}
} }
} }
Loading…
Cancel
Save