pull/32/head
gedoor 6 years ago
parent 998ee9b92f
commit ce4d2853f7
  1. 3
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  2. 12
      app/src/main/java/io/legado/app/data/dao/SourceCookieDao.kt
  3. 12
      app/src/main/java/io/legado/app/data/entities/SourceCookie.kt
  4. 215
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSonPath.kt
  5. 37
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeHeaders.kt
  6. 27
      app/src/main/res/values/pref_key_value.xml

@ -11,7 +11,7 @@ import io.legado.app.data.entities.*
@Database(
entities = [Book::class, BookGroup::class, BookSource::class, Chapter::class, ReplaceRule::class, SearchBook::class, SearchKeyword::class],
entities = [Book::class, BookGroup::class, BookSource::class, Chapter::class, ReplaceRule::class, SearchBook::class, SearchKeyword::class, SourceCookie::class],
version = 1,
exportSchema = true
)
@ -53,4 +53,5 @@ abstract class AppDatabase : RoomDatabase() {
abstract fun replaceRuleDao(): ReplaceRuleDao
abstract fun searchBookDao(): SearchBookDao
abstract fun searchKeywordDao(): SearchKeywordDao
abstract fun sourceCookieDao(): SourceCookieDao
}

@ -0,0 +1,12 @@
package io.legado.app.data.dao
import androidx.room.Dao
import androidx.room.Query
@Dao
interface SourceCookieDao {
@Query("SELECT cookie FROM cookies Where url = :url")
fun getCookieByUrl(url: String): String?
}

@ -0,0 +1,12 @@
package io.legado.app.data.entities
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
@Entity(tableName = "cookies", indices = [(Index(value = ["url"], unique = true))])
data class SourceCookie(
@PrimaryKey
var url: String = "",
var cookie: String = ""
)

@ -0,0 +1,215 @@
package io.legado.app.model.analyzeRule
import android.text.TextUtils
import com.jayway.jsonpath.JsonPath
import com.jayway.jsonpath.ReadContext
import java.util.*
import java.util.regex.Pattern
class AnalyzeByJSonPath {
private var ctx: ReadContext? = null
fun parse(json: Any): AnalyzeByJSonPath {
ctx = if (json is String) {
JsonPath.parse(json)
} else {
JsonPath.parse(json)
}
return this
}
fun getString(rule: String): String? {
if (TextUtils.isEmpty(rule)) return null
var result = ""
val rules: Array<String>
val elementsType: String
if (rule.contains("&&")) {
rules = rule.split("&&".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
elementsType = "&"
} else {
rules = rule.split("\\|\\|".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
elementsType = "|"
}
if (rules.size == 1) {
if (!rule.contains("{$.")) {
ctx?.let {
try {
val ob = it.read<Any>(rule)
result = if (ob is List<*>) {
val builder = StringBuilder()
for (o in ob) {
builder.append(o).append("\n")
}
builder.toString().replace("\\n$".toRegex(), "")
} else {
ob.toString()
}
} catch (ignored: Exception) {
}
}
return result
} else {
result = rule
val matcher = jsonRulePattern.matcher(rule)
while (matcher.find()) {
result = result.replace(
String.format("{%s}", matcher.group()),
getString(matcher.group())!!
)
}
return result
}
} else {
val sb = StringBuilder()
for (rl in rules) {
val temp = getString(rl)
if (!TextUtils.isEmpty(temp)) {
sb.append(temp)
if (elementsType == "|") {
break
}
}
}
return sb.toString()
}
}
internal fun getStringList(rule: String): List<String> {
val result = ArrayList<String>()
if (TextUtils.isEmpty(rule)) return result
val rules: Array<String>
val elementsType: String
when {
rule.contains("&&") -> {
rules = rule.split("&&".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
elementsType = "&"
}
rule.contains("%%") -> {
rules = rule.split("%%".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
elementsType = "%"
}
else -> {
rules = rule.split("\\|\\|".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
elementsType = "|"
}
}
if (rules.size == 1) {
if (!rule.contains("{$.")) {
try {
val `object` = ctx!!.read<Any>(rule) ?: return result
if (`object` is List<*>) {
for (o in `object`)
result.add(o.toString())
} else {
result.add(`object`.toString())
}
} catch (ignored: Exception) {
}
return result
} else {
val matcher = jsonRulePattern.matcher(rule)
while (matcher.find()) {
val stringList = getStringList(matcher.group())
for (s in stringList) {
result.add(rule.replace(String.format("{%s}", matcher.group()), s))
}
}
return result
}
} else {
val results = ArrayList<List<String>>()
for (rl in rules) {
val temp = getStringList(rl)
if (temp.isNotEmpty()) {
results.add(temp)
if (temp.isNotEmpty() && elementsType == "|") {
break
}
}
}
if (results.size > 0) {
if ("%" == elementsType) {
for (i in 0 until results[0].size) {
for (temp in results) {
if (i < temp.size) {
result.add(temp[i])
}
}
}
} else {
for (temp in results) {
result.addAll(temp)
}
}
}
return result
}
}
internal fun getObject(rule: String): Any {
return ctx!!.read(rule)
}
internal fun getList(rule: String): ArrayList<Any>? {
val result = ArrayList<Any>()
if (TextUtils.isEmpty(rule)) return result
val elementsType: String
val rules: Array<String>
when {
rule.contains("&&") -> {
rules = rule.split("&&".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
elementsType = "&"
}
rule.contains("%%") -> {
rules = rule.split("%%".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
elementsType = "%"
}
else -> {
rules = rule.split("\\|\\|".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
elementsType = "|"
}
}
if (rules.size == 1) {
ctx?.let {
try {
return it.read<ArrayList<Any>>(rules[0])
} catch (e: Exception) {
}
}
return null
} else {
val results = ArrayList<ArrayList<*>>()
for (rl in rules) {
val temp = getList(rl)
if (temp != null && temp.isNotEmpty()) {
results.add(temp)
if (temp.isNotEmpty() && elementsType == "|") {
break
}
}
}
if (results.size > 0) {
if ("%" == elementsType) {
for (i in 0 until results[0].size) {
for (temp in results) {
if (i < temp.size) {
temp[i]?.let { result.add(it) }
}
}
}
} else {
for (temp in results) {
result.addAll(temp)
}
}
}
}
return result
}
companion object {
private val jsonRulePattern = Pattern.compile("(?<=\\{)\\$\\..+?(?=\\})")
}
}

@ -0,0 +1,37 @@
package io.legado.app.model.analyzeRule
import android.text.TextUtils.isEmpty
import io.legado.app.App
import io.legado.app.R
import io.legado.app.data.entities.BookSource
import io.legado.app.utils.GSON
import io.legado.app.utils.fromJson
import io.legado.app.utils.getPrefString
import java.util.*
/**
* Created by GKF on 2018/3/2.
* 解析Headers
*/
object AnalyzeHeaders {
private val defaultUserAgent: String
get() = App.INSTANCE.getPrefString("user_agent")
?: App.INSTANCE.getString(R.string.pv_user_agent)
fun getMap(bookSource: BookSource?): Map<String, String> {
val headerMap = HashMap<String, String>()
if (bookSource != null && !isEmpty(bookSource.header)) {
bookSource.header?.let {
val map: HashMap<String, String>? = GSON.fromJson<HashMap<String, String>>(it)
map?.let { headerMap.putAll(map) }
}
}
if (bookSource != null) {
val cookie = App.db.sourceCookieDao().getCookieByUrl(bookSource.origin)
cookie?.let { headerMap["Cookie"] = cookie }
}
return headerMap
}
}

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="pk_auto_refresh">auto_refresh</string>
<string name="pk_screen_direction">list_screen_direction</string>
<string name="pk_full_screen">full_screen</string>
<string name="pk_threads_num">threads_num</string>
<string name="pk_user_agent">user_agent</string>
<string name="pk_bookshelf_px">bookshelf_px</string>
<string name="pk_read_type">read_type</string>
<string name="pk_find_expand_group">expandGroupFind</string>
<string name="pk_default_read">defaultToRead</string>
<string name="pk_auto_download">autoDownload</string>
<string name="pk_download_path">downloadPath</string>
<string name="pk_check_update">checkUpdate</string>
<string name="icon_main">ic_launcher_round</string>
<string name="icon_book">book_launcher_round</string>
<string name="pv_user_agent">Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.2357.134 Safari/537.36</string>
<string name="source_rule_url">https://gedoor.github.io/MyBookshelf/sourcerule.html</string>
<string name="this_github_url">https://github.com/gedoor/MyBookshelf</string>
<string name="disclaimer_url">https://gedoor.github.io/MyBookshelf/disclaimer.html</string>
<string name="home_page_url">https://gedoor.github.io/MyBookshelf/</string>
<string name="latest_release_url">https://github.com/gedoor/MyBookshelf/releases/latest</string>
<string name="latest_release_api">https://api.github.com/repos/gedoor/MyBookshelf/releases/latest</string>
</resources>
Loading…
Cancel
Save