xkcd - parse image from html (#2435)

xkcd - parse image from html
This commit is contained in:
Mike 2020-03-17 08:17:30 -04:00 committed by GitHub
parent 6d5b43705c
commit 506843c9e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 46 deletions

View File

@ -5,14 +5,8 @@ ext {
appName = 'Tachiyomi: xkcd' appName = 'Tachiyomi: xkcd'
pkgNameSuffix = 'en.xkcd' pkgNameSuffix = 'en.xkcd'
extClass = '.Xkcd' extClass = '.Xkcd'
extVersionCode = 5 extVersionCode = 6
libVersion = '1.2' 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" apply from: "$rootDir/common.gradle"

View File

@ -1,17 +1,14 @@
package eu.kanade.tachiyomi.extension.en.xkcd 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.network.GET
import eu.kanade.tachiyomi.source.model.* import eu.kanade.tachiyomi.source.model.*
import eu.kanade.tachiyomi.source.online.ParsedHttpSource import eu.kanade.tachiyomi.source.online.ParsedHttpSource
import okhttp3.Request import okhttp3.Request
import okhttp3.Response
import org.jsoup.nodes.Document import org.jsoup.nodes.Document
import org.jsoup.nodes.Element import org.jsoup.nodes.Element
import rx.Observable import rx.Observable
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Locale
class Xkcd : ParsedHttpSource() { class Xkcd : ParsedHttpSource() {
@ -23,7 +20,6 @@ class Xkcd : ParsedHttpSource() {
override val supportsLatest = false override val supportsLatest = false
override fun fetchPopularManga(page: Int): Observable<MangasPage> { override fun fetchPopularManga(page: Int): Observable<MangasPage> {
val manga = SManga.create() val manga = SManga.create()
manga.setUrlWithoutDomain("/archive") manga.setUrlWithoutDomain("/archive")
@ -39,52 +35,40 @@ class Xkcd : ParsedHttpSource() {
override fun fetchSearchManga(page: Int, query: String, filters: FilterList): Observable<MangasPage> = Observable.empty() override fun fetchSearchManga(page: Int, query: String, filters: FilterList): Observable<MangasPage> = Observable.empty()
override fun fetchMangaDetails(manga: SManga): Observable<SManga> { override fun fetchMangaDetails(manga: SManga): Observable<SManga> = Observable.just(manga)
return Observable.just(manga)
}
override fun chapterListSelector() = "div#middleContainer.box a" override fun chapterListSelector() = "div#middleContainer.box a"
override fun chapterFromElement(element: Element): SChapter { override fun chapterFromElement(element: Element): SChapter {
val chapter = SChapter.create() val chapter = SChapter.create()
chapter.url = element.attr("href") chapter.url = element.attr("href")
val number = chapter.url.removeSurrounding("/") val number = chapter.url.removeSurrounding("/")
chapter.chapter_number = number.toFloat() chapter.chapter_number = number.toFloat()
chapter.name = number + " - " + element.text() chapter.name = number + " - " + element.text()
chapter.date_upload = element.attr("title").let { 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 return chapter
} }
override fun pageListRequest(chapter: SChapter) = GET(baseUrl + chapter.url + "info.0.json") override fun pageListParse(document: Document): List<Page> {
val titleWords: Sequence<String>
val altTextWords: Sequence<String>
override fun pageListParse(response: Response): List<Page> { // transforming filename from info.0.json isn't guaranteed to work, stick to html
var jsonData = response.body()!!.string() // if an HD image is available it'll be the srcset attribute
jsonData = jsonData.replace("\\u00e2\\u0080\\u0094", "\\u2014") val image = document.select("div#comic img").let {
.replace("\\u00c3\\u00a9", "\\u00e9") if (it.hasAttr("srcset")) it.attr("abs:srcset").substringBefore(" ")
.replace("\\u00e2\\u0080\\u0093", "\\u2014") else it.attr("abs:src")
.replace("\\u00c3\\u00b3", "\\u00F3")
.replace("#", "%23")
.replace("&eacute;", "\\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)
} }
val pages = mutableListOf<Page>()
pages.add(Page(0, "", imageUrl))
//create a text image for the alt text // create a text image for the alt text
var titleWords = json["title"].string.splitToSequence(" ") document.select("div#comic img").let {
var altTextWords = json["alt"].string.splitToSequence(" ") titleWords = it.attr("alt").splitToSequence(" ")
altTextWords = it.attr("title").splitToSequence(" ")
}
var builder = StringBuilder() val builder = StringBuilder()
var count = 0 var count = 0
for (i in titleWords) { for (i in titleWords) {
@ -107,12 +91,9 @@ class Xkcd : ParsedHttpSource() {
charCount += i.length + 1 charCount += i.length + 1
} }
pages.add(Page(1, "", baseAltTextUrl + builder.toString() + baseAltTextPostUrl)) return listOf(Page(0, "", image), Page(1, "", baseAltTextUrl + builder.toString() + baseAltTextPostUrl))
return pages
} }
override fun pageListParse(document: Document): List<Page> = throw Exception("Not used")
override fun imageUrlRequest(page: Page) = GET(page.url) override fun imageUrlRequest(page: Page) = GET(page.url)
override fun imageUrlParse(document: Document) = throw Exception("Not used") 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 thumbnailUrl = "https://fakeimg.pl/550x780/ffffff/6E7B91/?text=xkcd&font=museo"
const val baseAltTextUrl = "https://fakeimg.pl/1500x2126/ffffff/000000/?text=" const val baseAltTextUrl = "https://fakeimg.pl/1500x2126/ffffff/000000/?text="
const val baseAltTextPostUrl = "&font_size=42&font=museo" const val baseAltTextPostUrl = "&font_size=42&font=museo"
const val comicsAfter1084Ext = "_2x.png"
const val defaultExt = ".png"
} }
} }