From 506843c9e0c3958a397e8fe30297f54aa388c5b6 Mon Sep 17 00:00:00 2001 From: Mike <51273546+SnakeDoc83@users.noreply.github.com> Date: Tue, 17 Mar 2020 08:17:30 -0400 Subject: [PATCH] xkcd - parse image from html (#2435) xkcd - parse image from html --- src/en/xkcd/build.gradle | 8 +-- .../tachiyomi/extension/en/xkcd/Xkcd.kt | 57 ++++++------------- 2 files changed, 19 insertions(+), 46 deletions(-) diff --git a/src/en/xkcd/build.gradle b/src/en/xkcd/build.gradle index dc43e2de6..a8864177e 100644 --- a/src/en/xkcd/build.gradle +++ b/src/en/xkcd/build.gradle @@ -5,14 +5,8 @@ ext { appName = 'Tachiyomi: xkcd' pkgNameSuffix = 'en.xkcd' extClass = '.Xkcd' - extVersionCode = 5 + extVersionCode = 6 libVersion = '1.2' } -dependencies { - compileOnly project(':duktape-stub') - compileOnly 'com.google.code.gson:gson:2.8.2' - compileOnly 'com.github.salomonbrys.kotson:kotson:2.5.0' -} - apply from: "$rootDir/common.gradle" diff --git a/src/en/xkcd/src/eu/kanade/tachiyomi/extension/en/xkcd/Xkcd.kt b/src/en/xkcd/src/eu/kanade/tachiyomi/extension/en/xkcd/Xkcd.kt index 7016e4b91..233a66351 100644 --- a/src/en/xkcd/src/eu/kanade/tachiyomi/extension/en/xkcd/Xkcd.kt +++ b/src/en/xkcd/src/eu/kanade/tachiyomi/extension/en/xkcd/Xkcd.kt @@ -1,17 +1,14 @@ package eu.kanade.tachiyomi.extension.en.xkcd -import com.github.salomonbrys.kotson.int -import com.github.salomonbrys.kotson.string -import com.google.gson.JsonParser import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.source.model.* import eu.kanade.tachiyomi.source.online.ParsedHttpSource import okhttp3.Request -import okhttp3.Response import org.jsoup.nodes.Document import org.jsoup.nodes.Element import rx.Observable import java.text.SimpleDateFormat +import java.util.Locale class Xkcd : ParsedHttpSource() { @@ -23,7 +20,6 @@ class Xkcd : ParsedHttpSource() { override val supportsLatest = false - override fun fetchPopularManga(page: Int): Observable { val manga = SManga.create() manga.setUrlWithoutDomain("/archive") @@ -39,52 +35,40 @@ class Xkcd : ParsedHttpSource() { override fun fetchSearchManga(page: Int, query: String, filters: FilterList): Observable = Observable.empty() - override fun fetchMangaDetails(manga: SManga): Observable { - return Observable.just(manga) - } - + override fun fetchMangaDetails(manga: SManga): Observable = Observable.just(manga) override fun chapterListSelector() = "div#middleContainer.box a" override fun chapterFromElement(element: Element): SChapter { - val chapter = SChapter.create() chapter.url = element.attr("href") val number = chapter.url.removeSurrounding("/") chapter.chapter_number = number.toFloat() chapter.name = number + " - " + element.text() chapter.date_upload = element.attr("title").let { - SimpleDateFormat("yyyy-MM-dd").parse(it).time + SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).parse(it).time } return chapter } - override fun pageListRequest(chapter: SChapter) = GET(baseUrl + chapter.url + "info.0.json") + override fun pageListParse(document: Document): List { + val titleWords: Sequence + val altTextWords: Sequence - override fun pageListParse(response: Response): List { - var jsonData = response.body()!!.string() - jsonData = jsonData.replace("\\u00e2\\u0080\\u0094", "\\u2014") - .replace("\\u00c3\\u00a9", "\\u00e9") - .replace("\\u00e2\\u0080\\u0093", "\\u2014") - .replace("\\u00c3\\u00b3", "\\u00F3") - .replace("#", "%23") - .replace("é", "\\u00e9") - val json = JsonParser().parse(jsonData).asJsonObject - - //the comic get hd if 1084 or higher - var imageUrl = json["img"].string - val number = json["num"].int - if (number >= 1084) { - imageUrl = imageUrl.replace(defaultExt, comicsAfter1084Ext) + // transforming filename from info.0.json isn't guaranteed to work, stick to html + // if an HD image is available it'll be the srcset attribute + val image = document.select("div#comic img").let { + if (it.hasAttr("srcset")) it.attr("abs:srcset").substringBefore(" ") + else it.attr("abs:src") } - val pages = mutableListOf() - pages.add(Page(0, "", imageUrl)) - //create a text image for the alt text - var titleWords = json["title"].string.splitToSequence(" ") - var altTextWords = json["alt"].string.splitToSequence(" ") + // create a text image for the alt text + document.select("div#comic img").let { + titleWords = it.attr("alt").splitToSequence(" ") + altTextWords = it.attr("title").splitToSequence(" ") + } - var builder = StringBuilder() + val builder = StringBuilder() var count = 0 for (i in titleWords) { @@ -107,12 +91,9 @@ class Xkcd : ParsedHttpSource() { charCount += i.length + 1 } - pages.add(Page(1, "", baseAltTextUrl + builder.toString() + baseAltTextPostUrl)) - return pages + return listOf(Page(0, "", image), Page(1, "", baseAltTextUrl + builder.toString() + baseAltTextPostUrl)) } - override fun pageListParse(document: Document): List = throw Exception("Not used") - override fun imageUrlRequest(page: Page) = GET(page.url) override fun imageUrlParse(document: Document) = throw Exception("Not used") @@ -147,8 +128,6 @@ class Xkcd : ParsedHttpSource() { const val thumbnailUrl = "https://fakeimg.pl/550x780/ffffff/6E7B91/?text=xkcd&font=museo" const val baseAltTextUrl = "https://fakeimg.pl/1500x2126/ffffff/000000/?text=" const val baseAltTextPostUrl = "&font_size=42&font=museo" - const val comicsAfter1084Ext = "_2x.png" - const val defaultExt = ".png" } }