ComicFury refactor chapters and dates (#412)

This commit is contained in:
Mike 2024-01-20 06:00:57 -05:00 committed by Draff
parent c3f277e342
commit 846e783195
2 changed files with 54 additions and 35 deletions

View File

@ -2,7 +2,7 @@ ext {
extName = 'Comic Fury' extName = 'Comic Fury'
pkgNameSuffix = 'all.comicfury' pkgNameSuffix = 'all.comicfury'
extClass = '.ComicFuryFactory' extClass = '.ComicFuryFactory'
extVersionCode = 2 extVersionCode = 3
isNsfw = true isNsfw = true
} }

View File

@ -20,6 +20,7 @@ import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.Request import okhttp3.Request
import okhttp3.Response import okhttp3.Response
import org.jsoup.nodes.Element
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
@ -33,8 +34,6 @@ class ComicFury(
override val baseUrl: String = "https://comicfury.com" override val baseUrl: String = "https://comicfury.com"
override val name: String = "Comic Fury$extraName" // Used for No Text override val name: String = "Comic Fury$extraName" // Used for No Text
override val supportsLatest: Boolean = true override val supportsLatest: Boolean = true
private val dateFormat = SimpleDateFormat("dd MMM yyyy hh:mm aa", Locale.US)
private val dateFormatSlim = SimpleDateFormat("dd MMM yyyy", Locale.US)
override val client = super.client.newBuilder().addInterceptor(TextInterceptor()).build() override val client = super.client.newBuilder().addInterceptor(TextInterceptor()).build()
@ -69,35 +68,43 @@ class ComicFury(
* Chapter Number is handled as Chapter dot Comic. Ex. Chapter 6, Comic 4: chapter_number = 6.4 * Chapter Number is handled as Chapter dot Comic. Ex. Chapter 6, Comic 4: chapter_number = 6.4
* *
*/ */
private val archiveSelector = "a:has(div.archive-chapter)"
private val chapterSelector = "a:has(div.archive-comic)"
private val nextArchivePageSelector = "#scroll-content > .onsite-viewer-back-link + .archive-pages a"
private lateinit var currentPage: org.jsoup.nodes.Document
private fun Element.toSManga(): SChapter {
return SChapter.create().apply {
setUrlWithoutDomain(this@toSManga.attr("abs:href"))
name = this@toSManga.select(".archive-comic-title").text()
date_upload = this@toSManga.select(".archive-comic-date").text().toDate()
}
}
private fun collect(url: String): List<SChapter> {
return client.newCall(GET(url, headers)).execute().asJsoup()
.also { currentPage = it }
.select(chapterSelector)
.map { element -> element.toSManga() }
}
override fun chapterListParse(response: Response): List<SChapter> { override fun chapterListParse(response: Response): List<SChapter> {
val jsp = response.asJsoup() val jsp = response.asJsoup()
if (jsp.selectFirst("div.archive-chapter") != null) {
val chapters: MutableList<SChapter> = arrayListOf() return if (jsp.selectFirst(archiveSelector) != null) {
for (chapter in jsp.select("div.archive-chapter").parents().reversed()) { val chapters = mutableListOf<SChapter>()
val name = chapter.text() jsp.select(archiveSelector).eachAttr("abs:href").map { url ->
chapters.addAll( chapters.addAll(collect(url))
client.newCall( currentPage.select(nextArchivePageSelector).eachAttr("abs:href")
GET("$baseUrl${chapter.attr("href")}"), .mapNotNull { nextUrl -> chapters.addAll(collect(nextUrl)) }
).execute()
.use { chapterListParse(it) }
.mapIndexed { i, it ->
it.apply {
scanlator = name
chapter_number += i
}
},
)
} }
return chapters chapters
.mapIndexed { index, sChapter -> sChapter.apply { chapter_number = index.toFloat() } }
.reversed()
} else { } else {
return jsp.select("div.archive-comic").mapIndexed { i, it -> jsp.select(chapterSelector).mapIndexed { i, element ->
SChapter.create().apply { element.toSManga().apply { chapter_number = "0.$i".toFloat() }
url = it.parent()!!.attr("href") }.reversed()
name = it.child(0).ownText()
date_upload = it.child(1).ownText().toDate()
chapter_number = "0.$i".toFloat()
}
}.toList().reversed()
} }
} }
@ -195,6 +202,8 @@ class ComicFury(
return Request.Builder().url(req.build()).build() return Request.Builder().url(req.build()).build()
} }
private fun Boolean.toInt(): Int = if (this) { 0 } else { 1 }
// START OF AUTHOR NOTES // // START OF AUTHOR NOTES //
private val preferences: SharedPreferences by lazy { private val preferences: SharedPreferences by lazy {
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000) Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
@ -278,14 +287,24 @@ class ComicFury(
override fun imageUrlParse(response: Response): String = override fun imageUrlParse(response: Response): String =
throw UnsupportedOperationException() throw UnsupportedOperationException()
// Date stuff
private fun String.toDate(): Long { private fun String.toDate(): Long {
val ret = this.replace("st", "") // remove st nd rd th (e.g. from 4th) but not from AuguST, and commas
.replace("nd", "") val ret = this.replace(Regex("(?<=\\d)(st|nd|rd|th)|,"), "")
.replace("rd", "")
.replace("th", "") return when {
.replace(",", "") ret.contains(":") -> date[0].parseTime(ret)
return dateFormat.parse(ret)?.time ?: dateFormatSlim.parse(ret)!!.time this.matches(Regex("\\d{1,2}\\s?\\w{3,9}\\s?\\w{2,4}")) -> date[1].parseTime(ret)
this.matches(Regex("\\w{3,9}\\s?\\d{1,2}\\s?\\d{2,4}")) -> date[2].parseTime(ret)
else -> 0
}
} }
private fun Boolean.toInt(): Int = if (this) { 0 } else { 1 } private val date = listOf("dd MMM yyyy hh:mm aa", "dd MMM yyyy", "MMM dd yyyy")
.map { SimpleDateFormat(it, Locale.US) }
private fun SimpleDateFormat.parseTime(string: String): Long {
return this.parse(string)?.time ?: 0
}
} }