Skip to content

Commit

Permalink
3.1.9 Fixed libread + rr
Browse files Browse the repository at this point in the history
  • Loading branch information
LagradOst committed Jun 26, 2024
1 parent 6c5bd3f commit aa38114
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 60 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ android {
minSdkVersion 21
targetSdkVersion 34
versionCode 55
versionName "3.1.8"
versionName "3.1.9"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down
52 changes: 30 additions & 22 deletions app/src/main/java/com/lagradost/quicknovel/APIRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.lagradost.quicknovel

import com.lagradost.quicknovel.mvvm.Resource
import com.lagradost.quicknovel.mvvm.logError
import com.lagradost.quicknovel.mvvm.normalSafeApiCall
import com.lagradost.quicknovel.mvvm.safeApiCall
import com.lagradost.quicknovel.util.Coroutines.threadSafeListOf

Expand Down Expand Up @@ -52,34 +51,43 @@ class APIRepository(val api: MainAPI) {

suspend fun load(url: String, allowCache: Boolean = true): Resource<LoadResponse> {
return safeApiCall {
val fixedUrl = api.fixUrl(url)
val lookingForHash = api.name to fixedUrl

if (allowCache) {
synchronized(cache) {
for (item in cache) {
// 10 min save
if (item.hash == lookingForHash && (unixTime - item.unixTime) < cacheTimeSec) {
return@safeApiCall item.response
}
}
try {
if (api.hasRateLimit) {
api.rateLimitMutex.lock()
}
}
val fixedUrl = api.fixUrl(url)
val lookingForHash = api.name to fixedUrl

api.load(fixedUrl)?.also { response ->
// Remove all blank tags as early as possible
val add = SavedLoadResponse(unixTime, response, lookingForHash)
if (allowCache) {
synchronized(cache) {
if (cache.size > cacheSize) {
cache[cacheIndex] = add // rolling cache
cacheIndex = (cacheIndex + 1) % cacheSize
} else {
cache.add(add)
for (item in cache) {
// 10 min save
if (item.hash == lookingForHash && (unixTime - item.unixTime) < cacheTimeSec) {
return@safeApiCall item.response
}
}
}
}

api.load(fixedUrl)?.also { response ->
// Remove all blank tags as early as possible
val add = SavedLoadResponse(unixTime, response, lookingForHash)
if (allowCache) {
synchronized(cache) {
if (cache.size > cacheSize) {
cache[cacheIndex] = add // rolling cache
cacheIndex = (cacheIndex + 1) % cacheSize
} else {
cache.add(add)
}
}
}
} ?: throw ErrorLoadingException("No data")
} finally {
if (api.hasRateLimit) {
api.rateLimitMutex.unlock()
}
} ?: throw ErrorLoadingException("No data")
}
}
}

Expand Down
70 changes: 43 additions & 27 deletions app/src/main/java/com/lagradost/quicknovel/BookDownloader2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -578,19 +578,28 @@ object BookDownloader2Helper {
}
rFile.parentFile?.mkdirs()
if (rFile.isDirectory) rFile.delete()

val rateLimit = api.rateLimitTime > 0
for (i in 0..maxTries) {
val page = api.loadHtml(data.url)

if (!page.isNullOrBlank()) {
rFile.createNewFile() // only create the file when actually needed
rFile.writeText("${data.name}\n${page}")
return@withContext true
} else {
delay(5000) // ERROR
if (rateLimit) {
api.api.rateLimitMutex.lock()
}
if (api.rateLimitTime > 0) {
delay(api.rateLimitTime)
try {
val page = api.loadHtml(data.url)

if (!page.isNullOrBlank()) {
rFile.createNewFile() // only create the file when actually needed
rFile.writeText("${data.name}\n${page}")
return@withContext true
} else {
delay(5000) // ERROR
}
if (api.rateLimitTime > 0) {
delay(api.rateLimitTime)
}
} finally {
if (rateLimit) {
api.api.rateLimitMutex.unlock()
}
}
}
return@withContext false
Expand Down Expand Up @@ -852,7 +861,6 @@ object NotificationHelper {
.setColor(context.colorFromAttribute(R.attr.colorPrimary))
.setContentText(
if (stateProgressState.total > 1) {

val extra = if (progressInBytes) {
val bytesToKiloBytes = 1024
"${stateProgressState.progress / bytesToKiloBytes} Kb/${stateProgressState.total / bytesToKiloBytes} Kb"
Expand Down Expand Up @@ -985,23 +993,29 @@ object ImageDownloader {
private val cachedBitmaps = hashMapOf<String, Bitmap>()

suspend fun getImageBitmapFromUrl(url: String): Bitmap? {
cachedBitmapMutex.withLock {
if (cachedBitmaps.containsKey(url)) {
return cachedBitmaps[url]
try {
cachedBitmapMutex.withLock {
if (cachedBitmaps.containsKey(url)) {
return cachedBitmaps[url]
}
}
}

val bitmap =
withContext(Dispatchers.IO) {
Glide.with(activity ?: return@withContext null)
.asBitmap()
.load(url).submit(720, 720).get()
} ?: return null
val bitmap =
withContext(Dispatchers.IO) {
Glide.with(activity ?: return@withContext null)
.asBitmap()
.load(url).submit(720, 720).get()
} ?: return null

cachedBitmapMutex.withLock {
cachedBitmaps[url] = bitmap
cachedBitmapMutex.withLock {
cachedBitmaps[url] = bitmap
}
return bitmap
} catch (t: Throwable) {
logError(t)
return null
}
return bitmap

}
}

Expand Down Expand Up @@ -1681,8 +1695,8 @@ object BookDownloader2 {
pFile.writeBytes(bytes)
}
}
} catch (e: Exception) {
logError(e)
} catch (t: Throwable) {
logError(t)
//delay(1000)
}
}
Expand Down Expand Up @@ -1801,6 +1815,8 @@ object BookDownloader2 {
progressState
)
}
} catch (t: Throwable) {
logError(t)
} finally {
currentDownloadsMutex.withLock {
currentDownloads -= id
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/lagradost/quicknovel/MainAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.lagradost.quicknovel.MainActivity.Companion.app
import com.lagradost.quicknovel.mvvm.logError
import com.lagradost.quicknovel.ui.UiImage
import com.lagradost.quicknovel.ui.img
import kotlinx.coroutines.sync.Mutex
import org.jsoup.Jsoup

const val USER_AGENT =
Expand All @@ -18,6 +19,8 @@ abstract class MainAPI {
open val lang = "en" // ISO_639_1 check SubtitleHelper

open val rateLimitTime: Long = 0
val hasRateLimit : Boolean get() = rateLimitTime > 0L
val rateLimitMutex : Mutex = Mutex()

open val usesCloudFlareKiller = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class FreewebnovelProvider : LibReadProvider() {
tag: String?
): HeadMainPageResponse {
val url =
if (tag.isNullOrBlank()) "$mainUrl/latest-release/$page" else "$mainUrl/genres/$tag/$page"
if (tag.isNullOrBlank()) "$mainUrl/latest-release-novels/$page" else "$mainUrl/genres/$tag/$page"
val document = app.get(url).document
val headers = document.select("div.ul-list1.ul-list1-2.ss-custom > div.li-row")
val returnValue = headers.mapNotNull { h ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ open class LibReadProvider : MainAPI() {
override val name = "LibRead"
override val mainUrl = "https://libread.com"
override val hasMainPage = true

open val removeHtml = false // because the two sites use .html or not for no reason

override val iconId = R.drawable.icon_libread
Expand Down Expand Up @@ -156,7 +157,7 @@ open class LibReadProvider : MainAPI() {
?.get(0)
?.text()
?.splitToSequence(", ")?.toList()
posterUrl = document.select(" div.pic > img").attr("src")
posterUrl = fixUrlNull(document.select(" div.pic > img").attr("src"))
synopsis = document.selectFirst("div.inner")?.text()
val votes = document.selectFirst("div.m-desc > div.score > p:nth-child(2)")
if (votes != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@ package com.lagradost.quicknovel.providers

import android.annotation.SuppressLint
import com.fasterxml.jackson.annotation.JsonProperty
import com.lagradost.quicknovel.*
import com.lagradost.quicknovel.ErrorLoadingException
import com.lagradost.quicknovel.HeadMainPageResponse
import com.lagradost.quicknovel.LoadResponse
import com.lagradost.quicknovel.MainAPI
import com.lagradost.quicknovel.MainActivity.Companion.app
import com.lagradost.quicknovel.R
import com.lagradost.quicknovel.STATUS_COMPLETE
import com.lagradost.quicknovel.STATUS_DROPPED
import com.lagradost.quicknovel.STATUS_ONGOING
import com.lagradost.quicknovel.STATUS_PAUSE
import com.lagradost.quicknovel.SearchResponse
import com.lagradost.quicknovel.UserReview
import com.lagradost.quicknovel.fixUrlNull
import com.lagradost.quicknovel.mvvm.logError
import com.lagradost.quicknovel.newChapterData
import com.lagradost.quicknovel.newSearchResponse
import com.lagradost.quicknovel.newStreamResponse
import org.jsoup.Jsoup
import java.lang.Exception
import java.util.*
import kotlin.collections.ArrayList
import java.util.Date

class RoyalRoadProvider : MainAPI() {
override val name = "Royal Road"
override val mainUrl = "https://www.royalroad.com"

override val rateLimitTime = 5000L
override val hasMainPage = true

override val iconId = R.drawable.big_icon_royalroad
Expand Down Expand Up @@ -311,9 +323,8 @@ class RoyalRoadProvider : MainAPI() {
override suspend fun load(url: String): LoadResponse? {
val response = app.get(url)
val document = response.document

val name = document.selectFirst("h1.font-white")?.text() ?: return null

val name = document.selectFirst("h1.font-white")?.text()
?: throw ErrorLoadingException("Null name")
val fictionId =
response.text.substringAfter("window.fictionId = ").substringBefore(";").toIntOrNull()

Expand Down

0 comments on commit aa38114

Please sign in to comment.