@ -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,57 +42,41 @@ 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 ( ) : SearchRule {
fun getSearchRule ( ) = ruleSearch ?: SearchRule ( )
return ruleSearch ?: SearchRule ( )
}
fun getExploreRule ( ) : ExploreRule {
fun getExploreRule ( ) = ruleExplore ?: ExploreRule ( )
return ruleExplore ?: ExploreRule ( )
}
fun getBookInfoRule ( ) : BookInfoRule {
fun getBookInfoRule ( ) = ruleBookInfo ?: BookInfoRule ( )
return ruleBookInfo ?: BookInfoRule ( )
}
fun getTocRule ( ) : TocRule {
fun getTocRule ( ) = ruleToc ?: TocRule ( )
return ruleToc ?: TocRule ( )
}
fun getContentRule ( ) : ContentRule {
fun getContentRule ( ) = ruleContent ?: ContentRule ( )
return ruleContent ?: ContentRule ( )
}
fun addGroup ( group : String ) {
fun addGroup ( group : String ) {
bookSourceGroup ?. let {
bookSourceGroup ?. let {
@ -111,8 +95,7 @@ data class BookSource(
}
}
}
}
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,14 +118,13 @@ 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
}
}
/ * *
/ * *
@ -155,29 +137,26 @@ data class BookSource(
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 ,
@ -186,54 +165,34 @@ data class BookSource(
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 )
}
}
}
}
}