From 78f274e27d1ac64ee7a8f639eaab9c2e117a445e Mon Sep 17 00:00:00 2001 From: Weasy Date: Mon, 21 Oct 2019 13:23:51 +0200 Subject: [PATCH] [Mangahub.io] Fix some parsing bugs (#1682) [Mangahub.io] Fix some parsing bugs --- src/en/mangahub/build.gradle | 2 +- .../extension/en/mangahub/Mangahub.kt | 66 +++++++++---------- 2 files changed, 33 insertions(+), 35 deletions(-) diff --git a/src/en/mangahub/build.gradle b/src/en/mangahub/build.gradle index 6ef62bcea..0193de141 100644 --- a/src/en/mangahub/build.gradle +++ b/src/en/mangahub/build.gradle @@ -5,7 +5,7 @@ ext { appName = 'Tachiyomi: Mangahub' pkgNameSuffix = 'en.mangahub' extClass = '.Mangahub' - extVersionCode = 2 + extVersionCode = 3 libVersion = '1.2' } diff --git a/src/en/mangahub/src/eu/kanade/tachiyomi/extension/en/mangahub/Mangahub.kt b/src/en/mangahub/src/eu/kanade/tachiyomi/extension/en/mangahub/Mangahub.kt index 991dc9ff9..8d03dac6a 100644 --- a/src/en/mangahub/src/eu/kanade/tachiyomi/extension/en/mangahub/Mangahub.kt +++ b/src/en/mangahub/src/eu/kanade/tachiyomi/extension/en/mangahub/Mangahub.kt @@ -92,56 +92,54 @@ class Mangahub : ParsedHttpSource() { } private fun parseChapterDate(date: String): Long { - return when { - "hours" in date || "just now" in date -> { - Calendar.getInstance().apply { - set(Calendar.HOUR_OF_DAY, 0) - set(Calendar.MINUTE, 0) - set(Calendar.SECOND, 0) - set(Calendar.MILLISECOND, 0) - }.timeInMillis + val now = Calendar.getInstance().apply { + set(Calendar.HOUR_OF_DAY, 0) + set(Calendar.MINUTE, 0) + set(Calendar.SECOND, 0) + set(Calendar.MILLISECOND, 0) + } + var parsedDate = 0L + when { + "just now" in date || "less than an hour" in date -> { + parsedDate = now.timeInMillis } - "days" in date -> { - val days = date.replace("days ago", "").trim().toInt() - Calendar.getInstance().apply { - add(Calendar.DAY_OF_YEAR, -days) - set(Calendar.HOUR_OF_DAY, 0) - set(Calendar.MINUTE, 0) - set(Calendar.SECOND, 0) - set(Calendar.MILLISECOND, 0) - }.timeInMillis + // parses: "1 hour ago" and "2 hours ago" + "hour" in date -> { + val hours = date.replaceAfter(" ", "").trim().toInt() + parsedDate = now.apply { add(Calendar.HOUR, -hours) }.timeInMillis } + // parses: "Yesterday" and "2 days ago" + "day" in date -> { + val days = date.replace("days ago", "").trim().toIntOrNull() ?: 1 + parsedDate = now.apply { add(Calendar.DAY_OF_YEAR, -days) }.timeInMillis + } + // parses: "2 weeks ago" "weeks" in date -> { val weeks = date.replace("weeks ago", "").trim().toInt() - Calendar.getInstance().apply { - add(Calendar.WEEK_OF_YEAR, -weeks) - set(Calendar.HOUR_OF_DAY, 0) - set(Calendar.MINUTE, 0) - set(Calendar.SECOND, 0) - set(Calendar.MILLISECOND, 0) - }.timeInMillis + parsedDate = now.apply { add(Calendar.WEEK_OF_YEAR, -weeks) }.timeInMillis } + // parses: "12-20-2019" and defaults everything that wasn't taken into account to 0 else -> { try { - SimpleDateFormat("MM-dd-yyyy", Locale.US).parse(date).time - } catch (e: ParseException) { - 0L - } + parsedDate = SimpleDateFormat("MM-dd-yyyy", Locale.US).parse(date).time + } catch (e: ParseException) { /*nothing to do, parsedDate is initialized with 0L*/ } } } + return parsedDate } override fun pageListParse(document: Document): List { val pageList = mutableListOf() - val page = document.select("div#mangareader img.PB0mN").first() - val pageUrl = page.attr("src") - val extension = pageUrl.split(".").last() + val pages = document.select("div#mangareader img.PB0mN") + val pageUrl = pages.first().attr("src") val pageRoot = pageUrl.replaceAfterLast("/", "") - val numPages = page.nextElementSibling().text().split("/").last().toInt() + val numPages = pages.first().nextElementSibling().text().split("/").last().toInt() - for (i in 1..numPages) { - pageList.add(Page(i, "", "$pageRoot$i.$extension")) + pageList.add(Page(0, "", pages.first().attr("src"))) + val extension = pages.last().attr("src").split(".").last() + for (i in 2..numPages) { + pageList.add(Page(i-1, "", "$pageRoot$i.$extension")) } return pageList