offline novel

This commit is contained in:
Finnley Somdahl 2023-12-03 22:18:06 -06:00
parent 111fb16266
commit 3ded6ba87a
18 changed files with 512 additions and 212 deletions

View file

@ -31,8 +31,20 @@ abstract class NovelParser : BaseParser() {
}
suspend fun sortedSearch(mediaObj: Media): List<ShowResponse> {
val query = mediaObj.name ?: mediaObj.nameRomaji
return search(query).sortByVolume(query)
//val query = mediaObj.name ?: mediaObj.nameRomaji
//return search(query).sortByVolume(query)
val results: List<ShowResponse>
return if(mediaObj.name != null) {
val query = mediaObj.name
results = search(query).sortByVolume(query)
results.ifEmpty {
val q = mediaObj.nameRomaji
search(q).sortByVolume(q)
}
} else {
val query = mediaObj.nameRomaji
search(query).sortByVolume(query)
}
}
}

View file

@ -13,11 +13,17 @@ object NovelSources : NovelReadSources() {
suspend fun init(fromExtensions: StateFlow<List<NovelExtension.Installed>>) {
// Initialize with the first value from StateFlow
val initialExtensions = fromExtensions.first()
list = createParsersFromExtensions(initialExtensions)
list = createParsersFromExtensions(initialExtensions) + Lazier(
{ OfflineNovelParser() },
"Downloaded"
)
// Update as StateFlow emits new values
fromExtensions.collect { extensions ->
list = createParsersFromExtensions(extensions)
list = createParsersFromExtensions(extensions) + Lazier(
{ OfflineNovelParser() },
"Downloaded"
)
}
}

View file

@ -0,0 +1,86 @@
package ani.dantotsu.parsers
import android.os.Environment
import ani.dantotsu.currContext
import ani.dantotsu.download.DownloadsManager
import ani.dantotsu.logger
import ani.dantotsu.media.manga.MangaNameAdapter
import eu.kanade.tachiyomi.source.model.SChapter
import eu.kanade.tachiyomi.source.model.SManga
import me.xdrop.fuzzywuzzy.FuzzySearch
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.io.File
class OfflineNovelParser: NovelParser() {
private val downloadManager = Injekt.get<DownloadsManager>()
override val hostUrl: String = "Offline"
override val name: String = "Offline"
override val saveName: String = "Offline"
override val volumeRegex =
Regex("vol\\.? (\\d+(\\.\\d+)?)|volume (\\d+(\\.\\d+)?)", RegexOption.IGNORE_CASE)
override suspend fun loadBook(link: String, extra: Map<String, String>?): Book {
//link should be a directory
val directory = File(
currContext()?.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),
"Dantotsu/Novel/$link"
)
val chapters = mutableListOf<Book>()
if (directory.exists()) {
directory.listFiles()?.forEach {
if (it.isDirectory) {
val chapter = Book(
it.name,
it.absolutePath + "/cover.jpg",
null,
listOf(it.absolutePath + "/0.epub")
)
chapters.add(chapter)
}
}
chapters.sortBy { MangaNameAdapter.findChapterNumber(it.name) }
return chapters.first()
}
return Book(
"error",
"",
null,
listOf("error")
)
}
override suspend fun search(query: String): List<ShowResponse> {
val titles = downloadManager.novelDownloads.map { it.title }.distinct()
val returnTitles: MutableList<String> = mutableListOf()
for (title in titles) {
if (FuzzySearch.ratio(title.lowercase(), query.lowercase()) > 80) {
returnTitles.add(title)
}
}
val returnList: MutableList<ShowResponse> = mutableListOf()
for (title in returnTitles) {
//need to search the subdirectories for the ShowResponses
val directory = File(
currContext()?.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),
"Dantotsu/Novel/$title"
)
val names = mutableListOf<String>()
if (directory.exists()) {
directory.listFiles()?.forEach {
if (it.isDirectory) {
names.add(it.name)
}
}
}
val cover = currContext()?.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)?.absolutePath + "/Dantotsu/Novel/$title/cover.jpg"
names.forEach {
returnList.add(ShowResponse(it, it, cover))
}
}
return returnList
}
}