Skip to content

Instantly share code, notes, and snippets.

@waptik
Created August 3, 2019 20:09
Show Gist options
  • Save waptik/3d1688bbac542981d51feb189e9c9c4f to your computer and use it in GitHub Desktop.
Save waptik/3d1688bbac542981d51feb189e9c9c4f to your computer and use it in GitHub Desktop.
portion of code for HeavenManga extension(Tachiyomi)
...
import org.jsoup.Jsoup
....
class HeavenManga : ParsedHttpSource() {
...
override val baseUrl = "http://heavenmanga.com"
...
// selector of getting the link to the first page of a chapter
private fun chapterPageSelector() = "a#l"
// skip the page that asks for viewing or downloading of image(pages)
// and go directly to the page
override fun pageListRequest(chapter: SChapter): Request {
val url = chapter.url
val newUrl = chapterUrl(url)
return GET(newUrl, headers)
}
// displaying chapter details and viewing its pages
override fun chapterFromElement(element: Element): SChapter {
val urlElement = element.select("a").first()
val timeElement = element.select("span").first()
val time = timeElement.text()
val date = time.replace("--", "-")
val url = urlElement.attr("href")
val chapter = SChapter.create()
chapter.setUrlWithoutDomain(chapterUrl(url))
chapter.name = urlElement.text()
chapter.date_upload = parseChapterDate(date.toString())
return chapter
}
// getting the direct link of a chapter
private fun chapterUrl(pageUrl: String): String {
val element = getUrlContents(pageUrl)
return element.select(chapterPageSelector()).attr("href")
}
// get contents of a url as Document
private fun getUrlContents(url: String): Document = Jsoup.connect(url).timeout(0).get()
// the following method deals with search results and filtering of genre and alphabetic order
override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request {
val search_url = "$baseUrl/buscar/$query.html"
// Filter
if(query.isBlank()) {
(if (filters.isEmpty()) getFilterList() else filters).forEach { filter ->
when(filter) {
is GenreFilter -> {
return GET(baseUrl + filter.toUriPart(), headers)
}
is AlphabeticoFilter -> {
return GET(baseUrl + filter.toUriPart(), headers)
}
}
}
}
return GET(search_url, headers)
}
/**
* count all pages(images) from the select tag
**/
override fun pageListParse(document: Document): List<Page> {
val pages = mutableListOf<Page>()
document.select(".chaptercontrols > select").first().getElementsByTag("option").forEach {
pages.add(Page(pages.size, it.attr("value")))
}
pages.getOrNull(0)?.imageUrl = imageUrlParse(document)
return pages
}
// TODO: learn how to deal with filtering genres
// Array.from(document.querySelectorAll('.categorias a')).map(a => `Pair("${a.textContent}", "${a.getAttribute('href')}")`).join(',\n')
// on http://heavenmanga.com/top/
private class GenreFilter : UriPartFilter("Géneros", arrayOf(
...
))
// Array.from(document.querySelectorAll('.letras a')).map(a => `Pair("${a.textContent}", "${a.getAttribute('href')}")`).join(',\n')
// on http://heavenmanga.com/top/
private class AlphabeticoFilter : UriPartFilter("Alfabético", arrayOf(
...
))
// display filters
override fun getFilterList() = FilterList(
GenreFilter(),
AlphabeticoFilter()
)
// easy way to display filter name and use its value
private open class UriPartFilter(displayName: String, val vals: Array<Pair<String, String>>) :
Filter.Select<String>(displayName, vals.map { it.first }.toTypedArray()) {
fun toUriPart() = vals[state].second
}
....
@waptik
Copy link
Author

waptik commented Aug 3, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment