pull/2311/head
			
			
		
		
							parent
							
								
									b22e1ffb34
								
							
						
					
					
						commit
						c695985b75
					
				| @ -0,0 +1,72 @@ | ||||
| package io.legado.app.help.source | ||||
| 
 | ||||
| import io.legado.app.data.entities.BookSource | ||||
| import io.legado.app.data.entities.rule.ExploreKind | ||||
| import io.legado.app.utils.* | ||||
| import kotlinx.coroutines.Dispatchers | ||||
| import kotlinx.coroutines.sync.Mutex | ||||
| import kotlinx.coroutines.sync.withLock | ||||
| import kotlinx.coroutines.withContext | ||||
| 
 | ||||
| private val mutexMap by lazy { hashMapOf<String, Mutex>() } | ||||
| private val exploreKindsMap by lazy { hashMapOf<String, List<ExploreKind>>() } | ||||
| private val aCache by lazy { ACache.get("explore") } | ||||
| 
 | ||||
| private fun BookSource.exploreKindsKey(): String { | ||||
|     return MD5Utils.md5Encode(bookSourceUrl + exploreUrl) | ||||
| } | ||||
| 
 | ||||
| suspend fun BookSource.exploreKinds(): List<ExploreKind> { | ||||
|     val exploreKindsKey = exploreKindsKey() | ||||
|     exploreKindsMap[exploreKindsKey]?.let { return it } | ||||
|     val exploreUrl = exploreUrl | ||||
|     if (exploreUrl.isNullOrBlank()) { | ||||
|         return emptyList() | ||||
|     } | ||||
|     val mutex = mutexMap[bookSourceUrl] ?: Mutex().apply { mutexMap[bookSourceUrl] = this } | ||||
|     mutex.withLock { | ||||
|         exploreKindsMap[exploreKindsKey()]?.let { return it } | ||||
|         val kinds = arrayListOf<ExploreKind>() | ||||
|         var ruleStr: String = exploreUrl | ||||
|         withContext(Dispatchers.IO) { | ||||
|             kotlin.runCatching { | ||||
|                 if (exploreUrl.startsWith("<js>", false) | ||||
|                     || exploreUrl.startsWith("@js:", false) | ||||
|                 ) { | ||||
|                     ruleStr = aCache.getAsString(bookSourceUrl) ?: "" | ||||
|                     if (ruleStr.isBlank()) { | ||||
|                         val jsStr = if (exploreUrl.startsWith("@")) { | ||||
|                             exploreUrl.substring(4) | ||||
|                         } else { | ||||
|                             exploreUrl.substring(4, exploreUrl.lastIndexOf("<")) | ||||
|                         } | ||||
|                         ruleStr = evalJS(jsStr).toString().trim() | ||||
|                         aCache.put(exploreKindsKey, ruleStr) | ||||
|                     } | ||||
|                 } | ||||
|                 if (ruleStr.isJsonArray()) { | ||||
|                     GSON.fromJsonArray<ExploreKind>(ruleStr).getOrThrow()?.let { | ||||
|                         kinds.addAll(it) | ||||
|                     } | ||||
|                 } else { | ||||
|                     ruleStr.split("(&&|\n)+".toRegex()).forEach { kindStr -> | ||||
|                         val kindCfg = kindStr.split("::") | ||||
|                         kinds.add(ExploreKind(kindCfg.first(), kindCfg.getOrNull(1))) | ||||
|                     } | ||||
|                 } | ||||
|             }.onFailure { | ||||
|                 kinds.add(ExploreKind("ERROR:${it.localizedMessage}", it.stackTraceToString())) | ||||
|                 it.printOnDebug() | ||||
|             } | ||||
|         } | ||||
|         exploreKindsMap[exploreKindsKey] = kinds | ||||
|         return kinds | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| suspend fun BookSource.clearExploreKindsCache() { | ||||
|     withContext(Dispatchers.IO) { | ||||
|         aCache.remove(bookSourceUrl) | ||||
|         exploreKindsMap.remove(exploreKindsKey()) | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,51 @@ | ||||
| package io.legado.app.help.source | ||||
| 
 | ||||
| import io.legado.app.data.entities.RssSource | ||||
| import io.legado.app.utils.ACache | ||||
| import io.legado.app.utils.MD5Utils | ||||
| import kotlinx.coroutines.Dispatchers | ||||
| import kotlinx.coroutines.withContext | ||||
| 
 | ||||
| private val aCache by lazy { ACache.get("rssSortUrl") } | ||||
| 
 | ||||
| private fun RssSource.sortUrlsKey(): String { | ||||
|     return MD5Utils.md5Encode(sourceUrl + sortUrl) | ||||
| } | ||||
| 
 | ||||
| suspend fun RssSource.sortUrls(): List<Pair<String, String>> = | ||||
|     arrayListOf<Pair<String, String>>().apply { | ||||
|         val sortUrlsKey = sortUrlsKey() | ||||
|         withContext(Dispatchers.IO) { | ||||
|             kotlin.runCatching { | ||||
|                 var a = sortUrl | ||||
|                 if (sortUrl?.startsWith("<js>", false) == true | ||||
|                     || sortUrl?.startsWith("@js:", false) == true | ||||
|                 ) { | ||||
|                     a = aCache.getAsString(sortUrlsKey) ?: "" | ||||
|                     if (a.isBlank()) { | ||||
|                         val jsStr = if (sortUrl!!.startsWith("@")) { | ||||
|                             sortUrl!!.substring(4) | ||||
|                         } else { | ||||
|                             sortUrl!!.substring(4, sortUrl!!.lastIndexOf("<")) | ||||
|                         } | ||||
|                         a = evalJS(jsStr).toString() | ||||
|                         aCache.put(sortUrlsKey, a) | ||||
|                     } | ||||
|                 } | ||||
|                 a?.split("(&&|\n)+".toRegex())?.forEach { c -> | ||||
|                     val d = c.split("::") | ||||
|                     if (d.size > 1) | ||||
|                         add(Pair(d[0], d[1])) | ||||
|                 } | ||||
|                 if (isEmpty()) { | ||||
|                     add(Pair("", sourceUrl)) | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| suspend fun RssSource.removeSortCache() { | ||||
|     withContext(Dispatchers.IO) { | ||||
|         aCache.remove(sortUrlsKey()) | ||||
|     } | ||||
| } | ||||
| @ -1,4 +1,4 @@ | ||||
| package io.legado.app.help | ||||
| package io.legado.app.help.source | ||||
| 
 | ||||
| import androidx.annotation.Keep | ||||
| import com.jayway.jsonpath.JsonPath | ||||
| @ -1,8 +1,10 @@ | ||||
| package io.legado.app.help | ||||
| package io.legado.app.help.source | ||||
| 
 | ||||
| import io.legado.app.constant.AppLog | ||||
| import io.legado.app.data.entities.BaseSource | ||||
| import io.legado.app.exception.NoStackTraceException | ||||
| import io.legado.app.help.CacheManager | ||||
| import io.legado.app.help.IntentData | ||||
| import io.legado.app.ui.association.VerificationCodeActivity | ||||
| import io.legado.app.ui.browser.WebViewActivity | ||||
| import io.legado.app.utils.startActivity | ||||
					Loading…
					
					
				
		Reference in new issue