Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
LagradOst committed Jan 29, 2024
1 parent 149a434 commit 6b80aae
Show file tree
Hide file tree
Showing 45 changed files with 4,279 additions and 6,110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class APIRepository(val api: MainAPI) {
suspend fun load(url: String, allowCache: Boolean = true): Resource<LoadResponse> {
return safeApiCall {
val fixedUrl = api.fixUrl(url)
val lookingForHash = Pair(api.name, fixedUrl)
val lookingForHash = api.name to fixedUrl

if (allowCache) {
synchronized(cache) {
Expand Down
33 changes: 18 additions & 15 deletions app/src/main/java/com/lagradost/quicknovel/BookDownloader2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ object BookDownloader2Helper {
val count =
if (epub.exists()) {
val length = epub.length()
if(length > LOCAL_EPUB_MIN_SIZE) {
if (length > LOCAL_EPUB_MIN_SIZE) {
1
} else {
0
Expand All @@ -316,7 +316,7 @@ object BookDownloader2Helper {
)
).listFiles()?.count { it.name.endsWith(".txt") } ?: return null
}
if(count <= 0) return null
if (count <= 0) return null

/*var sStart = start
if (sStart == -1) { // CACHE DATA
Expand Down Expand Up @@ -457,7 +457,8 @@ object BookDownloader2Helper {
}

val settingsManager = PreferenceManager.getDefaultSharedPreferences(activity)
val subDir = activity.getBasePath().first ?: getDefaultDir(activity) ?: throw IOException("No file")
val subDir =
activity.getBasePath().first ?: getDefaultDir(activity) ?: throw IOException("No file")

//val subDir = baseFile.gotoDirectory("Epub", createMissingDirectories = false) ?: return false
val displayName = "${sanitizeFilename(name)}.epub"
Expand Down Expand Up @@ -493,7 +494,7 @@ object BookDownloader2Helper {
return true
}

// val relativePath = (Environment.DIRECTORY_DOWNLOADS + "${fs}Epub${fs}")
// val relativePath = (Environment.DIRECTORY_DOWNLOADS + "${fs}Epub${fs}")

try {
val intent = Intent()
Expand Down Expand Up @@ -609,7 +610,8 @@ object BookDownloader2Helper {
val sName = sanitizeFilename(name)
val id = "$sApiName$sAuthor$sName".hashCode()

val subDir = activity.getBasePath().first ?: getDefaultDir(activity) ?: throw IOException("No file")
val subDir = activity.getBasePath().first ?: getDefaultDir(activity)
?: throw IOException("No file")

//val subDir = baseFile.gotoDirectoryOrThrow("Epub", createMissingDirectories = true)
val displayName = "${sanitizeFilename(name)}.epub"
Expand All @@ -618,7 +620,8 @@ object BookDownloader2Helper {
subDir.findFile(displayName)?.delete()
val file = subDir.createFileOrThrow(displayName)

val fileStream = file.openOutputStream(append = false) ?: throw IOException("No outputfile")
val fileStream =
file.openOutputStream(append = false) ?: throw IOException("No outputfile")

/*if (isScopedStorage()) {
val cr = activity.contentResolver ?: return false
Expand Down Expand Up @@ -1247,7 +1250,7 @@ object BookDownloader2 {
}
}

downloadProgressChanged.invoke(Pair(id, data ?: return null))
downloadProgressChanged.invoke(id to (data ?: return null))
return data
}

Expand Down Expand Up @@ -1321,7 +1324,7 @@ object BookDownloader2 {
downloadProgress[card.id]?.apply {
state = DownloadState.IsPending
lastUpdatedMs = System.currentTimeMillis()
downloadProgressChanged.invoke(Pair(card.id, this))
downloadProgressChanged.invoke(card.id to this)
}
}

Expand Down Expand Up @@ -1360,7 +1363,7 @@ object BookDownloader2 {
downloadProgress[card.id]?.apply {
state = DownloadState.IsFailed
lastUpdatedMs = System.currentTimeMillis()
downloadProgressChanged.invoke(Pair(card.id, this))
downloadProgressChanged.invoke(card.id to this)
}
}
}
Expand Down Expand Up @@ -1410,13 +1413,13 @@ object BookDownloader2 {

downloadInfoMutex.withLock {
downloadData[id] = currentDownloadData
downloadDataChanged.invoke(Pair(id, currentDownloadData))
downloadDataChanged.invoke(id to currentDownloadData)

downloadProgress[id]?.apply {
state = DownloadState.IsPending
lastUpdatedMs = System.currentTimeMillis()
this.total = total
downloadProgressChanged.invoke(Pair(id, this))
downloadProgressChanged.invoke(id to this)
} ?: run {
downloadProgress[id] = DownloadProgressState(
DownloadState.IsPending,
Expand All @@ -1425,7 +1428,7 @@ object BookDownloader2 {
System.currentTimeMillis(),
null
).also {
downloadProgressChanged.invoke(Pair(id, it))
downloadProgressChanged.invoke(id to it)
}
}
}
Expand All @@ -1444,8 +1447,8 @@ object BookDownloader2 {
suspend fun downloadAsync(load: EpubResponse, api: APIRepository) {
val filesDir = activity?.filesDir ?: return
val sApiName = BookDownloader2Helper.sanitizeFilename(api.name)
val sAuthor =
if (load.author == null) "" else BookDownloader2Helper.sanitizeFilename(load.author)
val author = load.author
val sAuthor = BookDownloader2Helper.sanitizeFilename(load.author ?: "")
val sName = BookDownloader2Helper.sanitizeFilename(load.name)
val id = generateId(load, api.name)

Expand Down Expand Up @@ -1666,7 +1669,7 @@ object BookDownloader2 {
val filesDir = activity?.filesDir ?: return@ioSafe
val sApiName = BookDownloader2Helper.sanitizeFilename(api.name)
val sAuthor =
if (load.author == null) "" else BookDownloader2Helper.sanitizeFilename(load.author)
BookDownloader2Helper.sanitizeFilename(load.author ?: "")
val sName = BookDownloader2Helper.sanitizeFilename(load.name)
val id = generateId(load, api.name)

Expand Down
136 changes: 101 additions & 35 deletions app/src/main/java/com/lagradost/quicknovel/MainAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,28 @@ data class MainPageResponse(
data class SearchResponse(
val name: String,
val url: String,
val posterUrl: String? = null,
val rating: Int? = null,
val latestChapter: String? = null,
var posterUrl: String? = null,
var rating: Int? = null,
var latestChapter: String? = null,
val apiName: String,
var posterHeaders: Map<String, String>? = null
) {
val image get() = img(posterUrl, posterHeaders)
}

fun MainAPI.newSearchResponse(
name: String,
url: String,
fix: Boolean = true,
initializer: SearchResponse.() -> Unit = { },
): SearchResponse {
val builder =
SearchResponse(name = name, url = if (fix) fixUrl(url) else url, apiName = this.name)
builder.initializer()

return builder
}

const val STATUS_NULL = 0
const val STATUS_ONGOING = 1
const val STATUS_COMPLETE = 2
Expand All @@ -189,36 +202,57 @@ const val STATUS_DROPPED = 4
interface LoadResponse {
val url: String
val name: String
val author: String?
val posterUrl: String?
var author: String?
var posterUrl: String?

//RATING IS FROM 0-1000
val rating: Int?
val peopleVoted: Int?
val views: Int?
val synopsis: String?
val tags: List<String>?
val status: Int? // 0 = null - implemented but not found, 1 = Ongoing, 2 = Complete, 3 = Pause/HIATUS, 4 = Dropped
var rating: Int?
var peopleVoted: Int?
var views: Int?
var synopsis: String?
var tags: List<String>?
var status: Int? // 0 = null - implemented but not found, 1 = Ongoing, 2 = Complete, 3 = Pause/HIATUS, 4 = Dropped
var posterHeaders: Map<String, String>?

val image : UiImage? get() = img(url = posterUrl, headers = posterHeaders)
val image: UiImage? get() = img(url = posterUrl, headers = posterHeaders)
val apiName: String
}

data class StreamResponse(
override val url: String,
override val name: String,
val data: List<ChapterData>,
override val author: String? = null,
override val posterUrl: String? = null,
override val rating: Int? = null,
override val peopleVoted: Int? = null,
override val views: Int? = null,
override val synopsis: String? = null,
override val tags: List<String>? = null,
override val status: Int? = null,
override val apiName: String,
override var author: String? = null,
override var posterUrl: String? = null,
override var rating: Int? = null,
override var peopleVoted: Int? = null,
override var views: Int? = null,
override var synopsis: String? = null,
override var tags: List<String>? = null,
override var status: Int? = null,
override var posterHeaders: Map<String, String>? = null,
var nextChapter: ChapterData? = null,
) : LoadResponse

fun MainAPI.newStreamResponse(
name: String,
url: String,
data: List<ChapterData>,
fix: Boolean = true,
initializer: StreamResponse.() -> Unit = { },
): StreamResponse {
val builder = StreamResponse(
name = name,
url = if (fix) fixUrl(url) else url,
apiName = this.name,
data = data
)
builder.initializer()

return builder
}

data class DownloadLink(
override val url: String,
override val name: String,
Expand All @@ -227,7 +261,7 @@ data class DownloadLink(
val params: Map<String, String> = mapOf(),
val cookies: Map<String, String> = mapOf(),
/// used for sorting, here you input the approx download speed in kb/s
val kbPerSec : Long = 1,
val kbPerSec: Long = 1,
) : DownloadLinkType

data class DownloadExtractLink(
Expand All @@ -239,17 +273,17 @@ data class DownloadExtractLink(
val cookies: Map<String, String> = mapOf(),
) : DownloadLinkType

fun makeLinkSafe(url : String) : String {
fun makeLinkSafe(url: String): String {
return url.replace("http://", "https://")
}

@WorkerThread
suspend fun DownloadExtractLink.get() : NiceResponse {
suspend fun DownloadExtractLink.get(): NiceResponse {
return app.get(makeLinkSafe(url), headers, referer, params, cookies)
}

@WorkerThread
suspend fun DownloadLink.get() : NiceResponse {
suspend fun DownloadLink.get(): NiceResponse {
return app.get(makeLinkSafe(url), headers, referer, params, cookies)
}

Expand All @@ -261,23 +295,55 @@ interface DownloadLinkType {
data class EpubResponse(
override val url: String,
override val name: String,
override val author: String? = null,
override val posterUrl: String? = null,
override val rating: Int? = null,
override val peopleVoted: Int? = null,
override val views: Int? = null,
override val synopsis: String? = null,
override val tags: List<String>? = null,
override val status: Int? = null,
override var author: String? = null,
override var posterUrl: String? = null,
override var rating: Int? = null,
override var peopleVoted: Int? = null,
override var views: Int? = null,
override var synopsis: String? = null,
override var tags: List<String>? = null,
override var status: Int? = null,
override var posterHeaders: Map<String, String>? = null,
val links: List<DownloadLinkType>,
override val apiName: String,
) : LoadResponse

fun MainAPI.newEpubResponse(
name: String,
url: String,
links: List<DownloadLinkType>,
fix: Boolean = true,
initializer: EpubResponse.() -> Unit = { },
): EpubResponse {
val builder = EpubResponse(
name = name,
url = if (fix) fixUrl(url) else url,
apiName = this.name,
links = links
)
builder.initializer()

return builder
}

data class ChapterData(
val name: String,
val url: String,
val dateOfRelease: String? = null,
var dateOfRelease: String? = null,
val views: Int? = null,
val regerer: String? = null
//val regerer: String? = null
//val index : Int,
)
)


fun MainAPI.newChapterData(
name: String,
url: String,
fix: Boolean = true,
initializer: ChapterData.() -> Unit = { },
): ChapterData {
val builder = ChapterData(name = name, url = if (fix) fixUrl(url) else url)
builder.initializer()

return builder
}
Loading

0 comments on commit 6b80aae

Please sign in to comment.