@ -11,8 +11,8 @@ import io.legado.app.data.entities.rule.*
import io.legado.app.help.JsExtensions
import io.legado.app.utils.*
import kotlinx.android.parcel.Parcelize
import java.util.*
import javax.script.SimpleBindings
import kotlin.collections.HashMap
@Parcelize
@TypeConverters ( BookSource . Converters :: class )
@ -42,57 +42,41 @@ data class BookSource(
var ruleBookInfo : BookInfoRule ? = null , // 书籍信息页规则
var ruleToc : TocRule ? = null , // 目录页规则
var ruleContent : ContentRule ? = null // 正文页规则
) : Parcelable , JsExtensions {
) : Parcelable , JsExtensions {
override fun hashCode ( ) : Int {
return bookSourceUrl . hashCode ( )
}
override fun equals ( other : Any ? ) : Boolean {
if ( other is BookSource ) {
return other . bookSourceUrl == bookSourceUrl
}
return false
}
override fun equals ( other : Any ? ) = if ( other is BookSource ) other . bookSourceUrl == bookSourceUrl else false
@Throws ( Exception :: class )
fun getHeaderMap ( ) : Map < String , String > {
val headerMap = HashMap < String , String > ( )
headerMap [ AppConst . UA _NAME ] = App . INSTANCE . getPrefString ( " user_agent " ) ?: userAgent
fun getHeaderMap ( ) = ( HashMap < String , String > ( ) . apply {
this [ AppConst . UA _NAME ] = App . INSTANCE . getPrefString ( " user_agent " ) ?: userAgent
header ?. let {
val header1 = when {
it . startsWith ( " @js: " , true ) ->
evalJS ( it . substring ( 4 ) ) . toString ( )
it . startsWith ( " <js> " , true ) ->
evalJS ( it . substring ( 4 , it . lastIndexOf ( " < " ) ) ) . toString ( )
else -> it
}
GSON . fromJsonObject < Map < String , String > > ( header1 ) ?. let { map ->
headerMap . putAll ( map )
GSON . fromJsonObject < Map < String , String > > (
when {
it . startsWith ( " @js: " , true ) ->
evalJS ( it . substring ( 4 ) ) . toString ( )
it . startsWith ( " <js> " , true ) ->
evalJS ( it . substring ( 4 , it . lastIndexOf ( " < " ) ) ) . toString ( )
else -> it
}
) ?. let { map ->
putAll ( map )
}
}
return headerMap
}
} ) as Map < String , String >
fun getSearchRule ( ) : SearchRule {
return ruleSearch ?: SearchRule ( )
}
fun getSearchRule ( ) = ruleSearch ?: SearchRule ( )
fun getExploreRule ( ) : ExploreRule {
return ruleExplore ?: ExploreRule ( )
}
fun getExploreRule ( ) = ruleExplore ?: ExploreRule ( )
fun getBookInfoRule ( ) : BookInfoRule {
return ruleBookInfo ?: BookInfoRule ( )
}
fun getBookInfoRule ( ) = ruleBookInfo ?: BookInfoRule ( )
fun getTocRule ( ) : TocRule {
return ruleToc ?: TocRule ( )
}
fun getTocRule ( ) = ruleToc ?: TocRule ( )
fun getContentRule ( ) : ContentRule {
return ruleContent ?: ContentRule ( )
}
fun getContentRule ( ) = ruleContent ?: ContentRule ( )
fun addGroup ( group : String ) {
bookSourceGroup ?. let {
@ -111,8 +95,7 @@ data class BookSource(
}
}
fun getExploreKinds ( ) : ArrayList < ExploreKind > ? {
val exploreKinds = arrayListOf < ExploreKind > ( )
fun getExploreKinds ( ) = arrayListOf < ExploreKind > ( ) . apply {
exploreUrl ?. let {
var a = it
if ( a . isNotBlank ( ) ) {
@ -135,14 +118,13 @@ data class BookSource(
b . forEach { c ->
val d = c . split ( " :: " )
if ( d . size > 1 )
exploreKinds . add ( ExploreKind ( d [ 0 ] , d [ 1 ] ) )
add ( ExploreKind ( d [ 0 ] , d [ 1 ] ) )
}
} 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 )
}
fun equal ( source : BookSource ) : Boolean {
return equal ( bookSourceName , source . bookSourceName )
&& equal ( bookSourceUrl , source . bookSourceUrl )
&& equal ( bookSourceGroup , source . bookSourceGroup )
&& bookSourceType == source . bookSourceType
&& equal ( bookUrlPattern , source . bookUrlPattern )
&& equal ( bookSourceComment , source . bookSourceComment )
&& enabled == source . enabled
&& enabledExplore == source . enabledExplore
&& equal ( header , source . header )
&& equal ( loginUrl , source . loginUrl )
&& equal ( exploreUrl , source . exploreUrl )
&& equal ( searchUrl , source . searchUrl )
&& getSearchRule ( ) == source . getSearchRule ( )
&& getExploreRule ( ) == source . getExploreRule ( )
&& getBookInfoRule ( ) == source . getBookInfoRule ( )
&& getTocRule ( ) == source . getTocRule ( )
&& getContentRule ( ) == source . getContentRule ( )
}
private fun equal ( a : String ? , b : String ? ) : Boolean {
return a == b || ( a . isNullOrEmpty ( ) && b . isNullOrEmpty ( ) )
}
fun equal ( source : BookSource ) =
equal ( bookSourceName , source . bookSourceName )
&& equal ( bookSourceUrl , source . bookSourceUrl )
&& equal ( bookSourceGroup , source . bookSourceGroup )
&& bookSourceType == source . bookSourceType
&& equal ( bookUrlPattern , source . bookUrlPattern )
&& equal ( bookSourceComment , source . bookSourceComment )
&& enabled == source . enabled
&& enabledExplore == source . enabledExplore
&& equal ( header , source . header )
&& equal ( loginUrl , source . loginUrl )
&& equal ( exploreUrl , source . exploreUrl )
&& equal ( searchUrl , source . searchUrl )
&& getSearchRule ( ) == source . getSearchRule ( )
&& getExploreRule ( ) == source . getExploreRule ( )
&& getBookInfoRule ( ) == source . getBookInfoRule ( )
&& getTocRule ( ) == source . getTocRule ( )
&& getContentRule ( ) == source . getContentRule ( )
private fun equal ( a : String ? , b : String ? ) = a == b || ( a . isNullOrEmpty ( ) && b . isNullOrEmpty ( ) )
data class ExploreKind (
var title : String ,
@ -186,54 +165,34 @@ data class BookSource(
class Converters {
@TypeConverter
fun exploreRuleToString ( exploreRule : ExploreRule ? ) : String ? {
return GSON . toJson ( exploreRule )
}
fun exploreRuleToString ( exploreRule : ExploreRule ? ) = GSON . toJson ( exploreRule )
@TypeConverter
fun stringToExploreRule ( json : String ? ) : ExploreRule ? {
return GSON . fromJsonObject < ExploreRule > ( json )
}
fun stringToExploreRule ( json : String ? ) = GSON . fromJsonObject < ExploreRule > ( json )
@TypeConverter
fun searchRuleToString ( searchRule : SearchRule ? ) : String ? {
return GSON . toJson ( searchRule )
}
fun searchRuleToString ( searchRule : SearchRule ? ) = GSON . toJson ( searchRule )
@TypeConverter
fun stringToSearchRule ( json : String ? ) : SearchRule ? {
return GSON . fromJsonObject < SearchRule > ( json )
}
fun stringToSearchRule ( json : String ? ) = GSON . fromJsonObject < SearchRule > ( json )
@TypeConverter
fun bookInfoRuleToString ( bookInfoRule : BookInfoRule ? ) : String ? {
return GSON . toJson ( bookInfoRule )
}
fun bookInfoRuleToString ( bookInfoRule : BookInfoRule ? ) = GSON . toJson ( bookInfoRule )
@TypeConverter
fun stringToBookInfoRule ( json : String ? ) : BookInfoRule ? {
return GSON . fromJsonObject < BookInfoRule > ( json )
}
fun stringToBookInfoRule ( json : String ? ) = GSON . fromJsonObject < BookInfoRule > ( json )
@TypeConverter
fun tocRuleToString ( tocRule : TocRule ? ) : String ? {
return GSON . toJson ( tocRule )
}
fun tocRuleToString ( tocRule : TocRule ? ) = GSON . toJson ( tocRule )
@TypeConverter
fun stringToTocRule ( json : String ? ) : TocRule ? {
return GSON . fromJsonObject < TocRule > ( json )
}
fun stringToTocRule ( json : String ? ) = GSON . fromJsonObject < TocRule > ( json )
@TypeConverter
fun contentRuleToString ( contentRule : ContentRule ? ) : String ? {
return GSON . toJson ( contentRule )
}
fun contentRuleToString ( contentRule : ContentRule ? ) = GSON . toJson ( contentRule )
@TypeConverter
fun stringToContentRule ( json : String ? ) : ContentRule ? {
return GSON . fromJsonObject < ContentRule > ( json )
}
fun stringToContentRule ( json : String ? ) = GSON . fromJsonObject < ContentRule > ( json )
}
}