From 57ad2e46967e2da61dac4bb9dbff48bcd5cbafd9 Mon Sep 17 00:00:00 2001 From: nulldev Date: Wed, 25 Apr 2018 15:21:32 -0400 Subject: [PATCH] Fix FoolSlide dates --- src/all/foolslide/build.gradle | 4 +- .../extension/en/foolslide/FoolSlide.kt | 95 ++++++++++++++++++- .../en/foolslide/FoolSlideFactory.kt | 54 ----------- 3 files changed, 92 insertions(+), 61 deletions(-) diff --git a/src/all/foolslide/build.gradle b/src/all/foolslide/build.gradle index 5ac5e8fbe..ff811f56c 100644 --- a/src/all/foolslide/build.gradle +++ b/src/all/foolslide/build.gradle @@ -5,8 +5,8 @@ ext { appName = 'Tachiyomi: FoolSlide' pkgNameSuffix = "all.foolslide" extClass = '.FoolSlideFactory' - extVersionCode = 2 - extVersionSuffix = 2 + extVersionCode = 3 + extVersionSuffix = 3 libVersion = '1.2' } dependencies { diff --git a/src/all/foolslide/src/eu/kanade/tachiyomi/extension/en/foolslide/FoolSlide.kt b/src/all/foolslide/src/eu/kanade/tachiyomi/extension/en/foolslide/FoolSlide.kt index 6c7528d11..66098f255 100644 --- a/src/all/foolslide/src/eu/kanade/tachiyomi/extension/en/foolslide/FoolSlide.kt +++ b/src/all/foolslide/src/eu/kanade/tachiyomi/extension/en/foolslide/FoolSlide.kt @@ -128,16 +128,91 @@ open class FoolSlide(override val name: String, override val baseUrl: String, ov return chapter } - open fun parseChapterDate(date: String): Long { + open fun parseChapterDate(date: String): Long? { + val lcDate = date.toLowerCase() + if (lcDate.endsWith(" ago")) + parseRelativeDate(lcDate)?.let { return it } + + //Handle 'yesterday' and 'today' + var relativeDate: Calendar? = null + if (lcDate.startsWith("yesterday")) { + relativeDate = Calendar.getInstance() + relativeDate.add(Calendar.DAY_OF_MONTH, -1) //yesterday + } else if (lcDate.startsWith("today")) { + relativeDate = Calendar.getInstance() + } + + relativeDate?.timeInMillis?.let { + return it + } + + var result = DATE_FORMAT_1.parseOrNull(date) + + for(dateFormat in DATE_FORMATS_WITH_ORDINAL_SUFFIXES) { + if (result == null) + result = dateFormat.parseOrNull(date) + else + break + } + + for(dateFormat in DATE_FORMATS_WITH_ORDINAL_SUFFIXES_NO_YEAR) { + if (result == null) { + result = dateFormat.parseOrNull(date) + + if(result != null) { + // Result parsed but no year, copy current year over + result = Calendar.getInstance().apply { + time = result + set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR)) + }.time + } + } else break + } + + return result?.time ?: 0L + } + + /** + * Parses dates in this form: + * `11 days ago` + */ + private fun parseRelativeDate(date: String): Long? { + val trimmedDate = date.split(" ") + + if (trimmedDate[2] != "ago") return null + + val number = trimmedDate[0].toIntOrNull() ?: return null + val unit = trimmedDate[1].removeSuffix("s") // Remove 's' suffix + + val now = Calendar.getInstance() + + // Map English unit to Java unit + val javaUnit = when (unit) { + "year", "yr" -> Calendar.YEAR + "month" -> Calendar.MONTH + "week", "wk" -> Calendar.WEEK_OF_MONTH + "day" -> Calendar.DAY_OF_MONTH + "hour", "hr" -> Calendar.HOUR + "minute", "min" -> Calendar.MINUTE + "second", "sec" -> Calendar.SECOND + else -> return null + } + + now.add(javaUnit, -number) + + return now.timeInMillis + } + + private fun SimpleDateFormat.parseOrNull(string: String): Date? { return try { - SimpleDateFormat("yyyy.MM.dd", Locale.US).parse(date).time - } catch (e: ParseException) { - 0L + parse(string) + } catch(e: ParseException) { + null } } override fun pageListRequest(chapter: SChapter) - = allowAdult(super.pageListRequest(chapter)) + = allowAdult(super.pageListRequest(chapter)) override fun pageListParse(document: Document): List { val doc = document.toString() @@ -157,4 +232,14 @@ open class FoolSlide(override val name: String, override val baseUrl: String, ov override fun imageUrlParse(document: Document) = throw UnsupportedOperationException("Not used") + companion object { + private val ORDINAL_SUFFIXES = listOf("st", "nd", "rd", "th") + private val DATE_FORMAT_1 = SimpleDateFormat("yyyy.MM.dd", Locale.US) + private val DATE_FORMATS_WITH_ORDINAL_SUFFIXES = ORDINAL_SUFFIXES.map { + SimpleDateFormat("dd'$it' MMMM, yyyy", Locale.US) + } + private val DATE_FORMATS_WITH_ORDINAL_SUFFIXES_NO_YEAR = ORDINAL_SUFFIXES.map { + SimpleDateFormat("dd'$it' MMMM", Locale.US) + } + } } \ No newline at end of file diff --git a/src/all/foolslide/src/eu/kanade/tachiyomi/extension/en/foolslide/FoolSlideFactory.kt b/src/all/foolslide/src/eu/kanade/tachiyomi/extension/en/foolslide/FoolSlideFactory.kt index 8b22826be..a62b7f988 100644 --- a/src/all/foolslide/src/eu/kanade/tachiyomi/extension/en/foolslide/FoolSlideFactory.kt +++ b/src/all/foolslide/src/eu/kanade/tachiyomi/extension/en/foolslide/FoolSlideFactory.kt @@ -10,7 +10,6 @@ import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.model.SManga import okhttp3.Request import org.jsoup.nodes.Document -import java.util.* class FoolSlideFactory : SourceFactory { override fun createSources(): List = getAllFoolSlide() @@ -66,59 +65,6 @@ class JaminisBox : FoolSlide("Jaimini's Box", "https://jaiminisbox.com", "en", " } return pages } - - override fun parseChapterDate(date: String): Long { - try { - val lcDate = date.toLowerCase() - if (lcDate.endsWith("ago")) - return parseRelativeDate(lcDate) - - //Handle 'yesterday' and 'today' - var relativeDate: Calendar? = null - if (lcDate.startsWith("yesterday")) { - relativeDate = Calendar.getInstance() - relativeDate.add(Calendar.DAY_OF_MONTH, -1) //yesterday - } else if (lcDate.startsWith("today")) { - relativeDate = Calendar.getInstance() - } - - return relativeDate?.timeInMillis - ?: super.parseChapterDate(date) - } catch(t: Throwable) { - return 0L - } - } - - /** - * Parses dates in this form: - * `11 days ago` - */ - private fun parseRelativeDate(date: String): Long { - val trimmedDate = date.split(" ") - - if (trimmedDate[2] != "ago") return 0 - - val number = trimmedDate[0].toIntOrNull() ?: return 0 - val unit = trimmedDate[1].removeSuffix("s") //Remove 's' suffix - - val now = Calendar.getInstance() - - //Map English unit to Java unit - val javaUnit = when (unit) { - "year", "yr" -> Calendar.YEAR - "month" -> Calendar.MONTH - "week" -> Calendar.WEEK_OF_MONTH - "day" -> Calendar.DAY_OF_MONTH - "hour", "hr" -> Calendar.HOUR - "minute", "min" -> Calendar.MINUTE - "second", "sec" -> Calendar.SECOND - else -> return 0 - } - - now.add(javaUnit, -number) - - return now.timeInMillis - } } class ChampionScans : FoolSlide("Champion Scans", "http://reader.championscans.com", "en")