Keep compatibility with YAML sources. Reorder methods

This commit is contained in:
len 2016-09-30 21:29:03 +02:00
parent 0b3dda18d3
commit 6d0689fe6c
6 changed files with 103 additions and 107 deletions

View file

@ -54,7 +54,7 @@ abstract class OnlineSource(context: Context) : Source {
abstract val lang: Language abstract val lang: Language
/** /**
* If the Source has Support for Latest Updates * Whether the source has support for latest updates.
*/ */
abstract val supportsLatest : Boolean abstract val supportsLatest : Boolean
@ -101,17 +101,6 @@ abstract class OnlineSource(context: Context) : Source {
page page
} }
/**
* Returns an observable containing a page with a list of latest manga.
*/
open fun fetchLatestUpdates(page: MangasPage): Observable<MangasPage> = client
.newCall(latestUpdatesRequest(page))
.asObservable()
.map { response ->
latestUpdatesParse(response, page)
page
}
/** /**
* Returns the request for the popular manga given the page. Override only if it's needed to * Returns the request for the popular manga given the page. Override only if it's needed to
* send different headers or request method like POST. * send different headers or request method like POST.
@ -125,26 +114,11 @@ abstract class OnlineSource(context: Context) : Source {
return GET(page.url, headers) return GET(page.url, headers)
} }
/**
* Returns the request for latest manga given the page.
*/
open protected fun latestUpdatesRequest(page: MangasPage): Request {
if (page.page == 1) {
page.url = latestUpdatesInitialUrl()
}
return GET(page.url, headers)
}
/** /**
* Returns the absolute url of the first page to popular manga. * Returns the absolute url of the first page to popular manga.
*/ */
abstract protected fun popularMangaInitialUrl(): String abstract protected fun popularMangaInitialUrl(): String
/**
* Returns the absolute url of the first page to latest manga.
*/
abstract protected fun latestUpdatesInitialUrl(): String
/** /**
* Parse the response from the site. It should add a list of manga and the absolute url to the * Parse the response from the site. It should add a list of manga and the absolute url to the
* next page (if it has a next one) to [page]. * next page (if it has a next one) to [page].
@ -154,11 +128,6 @@ abstract class OnlineSource(context: Context) : Source {
*/ */
abstract protected fun popularMangaParse(response: Response, page: MangasPage) abstract protected fun popularMangaParse(response: Response, page: MangasPage)
/**
* Same as [popularMangaParse], but for latest manga.
*/
abstract protected fun latestUpdatesParse(response: Response, page: MangasPage)
/** /**
* Returns an observable containing a page with a list of manga. Normally it's not needed to * Returns an observable containing a page with a list of manga. Normally it's not needed to
* override this method. * override this method.
@ -206,6 +175,37 @@ abstract class OnlineSource(context: Context) : Source {
*/ */
abstract protected fun searchMangaParse(response: Response, page: MangasPage, query: String, filters: List<Filter>) abstract protected fun searchMangaParse(response: Response, page: MangasPage, query: String, filters: List<Filter>)
/**
* Returns an observable containing a page with a list of latest manga.
*/
open fun fetchLatestUpdates(page: MangasPage): Observable<MangasPage> = client
.newCall(latestUpdatesRequest(page))
.asObservable()
.map { response ->
latestUpdatesParse(response, page)
page
}
/**
* Returns the request for latest manga given the page.
*/
open protected fun latestUpdatesRequest(page: MangasPage): Request {
if (page.page == 1) {
page.url = latestUpdatesInitialUrl()
}
return GET(page.url, headers)
}
/**
* Returns the absolute url of the first page to latest manga.
*/
abstract protected fun latestUpdatesInitialUrl(): String
/**
* Same as [popularMangaParse], but for latest manga.
*/
abstract protected fun latestUpdatesParse(response: Response, page: MangasPage)
/** /**
* Returns an observable with the updated details for a manga. Normally it's not needed to * Returns an observable with the updated details for a manga. Normally it's not needed to
* override this method. * override this method.

View file

@ -37,33 +37,11 @@ abstract class ParsedOnlineSource(context: Context) : OnlineSource(context) {
} }
} }
/**
* Parse the response from the site for latest updates and fills [page].
*/
override fun latestUpdatesParse(response: Response, page: MangasPage) {
val document = response.asJsoup()
for (element in document.select(latestUpdatesSelector())) {
Manga.create(id).apply {
latestUpdatesFromElement(element, this)
page.mangas.add(this)
}
}
latestUpdatesNextPageSelector()?.let { selector ->
page.nextPageUrl = document.select(selector).first()?.absUrl("href")
}
}
/** /**
* Returns the Jsoup selector that returns a list of [Element] corresponding to each manga. * Returns the Jsoup selector that returns a list of [Element] corresponding to each manga.
*/ */
abstract protected fun popularMangaSelector(): String abstract protected fun popularMangaSelector(): String
/**
* Returns the Jsoup selector similar to [popularMangaSelector], but for latest updates.
*/
abstract protected fun latestUpdatesSelector(): String
/** /**
* Fills [manga] with the given [element]. Most sites only show the title and the url, it's * Fills [manga] with the given [element]. Most sites only show the title and the url, it's
* totally safe to fill only those two values. * totally safe to fill only those two values.
@ -73,22 +51,12 @@ abstract class ParsedOnlineSource(context: Context) : OnlineSource(context) {
*/ */
abstract protected fun popularMangaFromElement(element: Element, manga: Manga) abstract protected fun popularMangaFromElement(element: Element, manga: Manga)
/**
* Fills [manga] with the given [element]. For latest updates.
*/
abstract protected fun latestUpdatesFromElement(element: Element, manga: Manga)
/** /**
* Returns the Jsoup selector that returns the <a> tag linking to the next page, or null if * Returns the Jsoup selector that returns the <a> tag linking to the next page, or null if
* there's no next page. * there's no next page.
*/ */
abstract protected fun popularMangaNextPageSelector(): String? abstract protected fun popularMangaNextPageSelector(): String?
/**
* Returns the Jsoup selector that returns the <a> tag, like [popularMangaNextPageSelector].
*/
abstract protected fun latestUpdatesNextPageSelector(): String?
/** /**
* Parse the response from the site and fills [page]. * Parse the response from the site and fills [page].
* *
@ -130,6 +98,38 @@ abstract class ParsedOnlineSource(context: Context) : OnlineSource(context) {
*/ */
abstract protected fun searchMangaNextPageSelector(): String? abstract protected fun searchMangaNextPageSelector(): String?
/**
* Parse the response from the site for latest updates and fills [page].
*/
override fun latestUpdatesParse(response: Response, page: MangasPage) {
val document = response.asJsoup()
for (element in document.select(latestUpdatesSelector())) {
Manga.create(id).apply {
latestUpdatesFromElement(element, this)
page.mangas.add(this)
}
}
latestUpdatesNextPageSelector()?.let { selector ->
page.nextPageUrl = document.select(selector).first()?.absUrl("href")
}
}
/**
* Returns the Jsoup selector similar to [popularMangaSelector], but for latest updates.
*/
abstract protected fun latestUpdatesSelector(): String
/**
* Fills [manga] with the given [element]. For latest updates.
*/
abstract protected fun latestUpdatesFromElement(element: Element, manga: Manga)
/**
* Returns the Jsoup selector that returns the <a> tag, like [popularMangaNextPageSelector].
*/
abstract protected fun latestUpdatesNextPageSelector(): String?
/** /**
* Parse the response from the site and fills the details of [manga]. * Parse the response from the site and fills the details of [manga].
* *

View file

@ -5,7 +5,6 @@ import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.network.GET import eu.kanade.tachiyomi.data.network.GET
import eu.kanade.tachiyomi.data.network.POST import eu.kanade.tachiyomi.data.network.POST
import eu.kanade.tachiyomi.data.source.Source
import eu.kanade.tachiyomi.data.source.getLanguages import eu.kanade.tachiyomi.data.source.getLanguages
import eu.kanade.tachiyomi.data.source.model.MangasPage import eu.kanade.tachiyomi.data.source.model.MangasPage
import eu.kanade.tachiyomi.data.source.model.Page import eu.kanade.tachiyomi.data.source.model.Page
@ -15,7 +14,6 @@ import okhttp3.Request
import okhttp3.Response import okhttp3.Response
import org.jsoup.Jsoup import org.jsoup.Jsoup
import org.jsoup.nodes.Element import org.jsoup.nodes.Element
import rx.Observable
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
@ -34,7 +32,7 @@ class YamlOnlineSource(context: Context, mappings: Map<*, *>) : OnlineSource(con
getLanguages().find { code == it.code }!! getLanguages().find { code == it.code }!!
} }
override val supportsLatest = map.supportsLatest.toBoolean() override val supportsLatest = map.latestupdates != null
override val client = when(map.client) { override val client = when(map.client) {
"cloudflare" -> network.cloudflareClient "cloudflare" -> network.cloudflareClient
@ -55,20 +53,8 @@ class YamlOnlineSource(context: Context, mappings: Map<*, *>) : OnlineSource(con
} }
} }
override fun latestUpdatesRequest(page: MangasPage): Request {
if (page.page == 1) {
page.url = latestUpdatesInitialUrl()
}
return when (map.latestupdates.method?.toLowerCase()) {
"post" -> POST(page.url, headers, map.latestupdates.createForm())
else -> GET(page.url, headers)
}
}
override fun popularMangaInitialUrl() = map.popular.url override fun popularMangaInitialUrl() = map.popular.url
override fun latestUpdatesInitialUrl() = map.latestupdates.url
override fun popularMangaParse(response: Response, page: MangasPage) { override fun popularMangaParse(response: Response, page: MangasPage) {
val document = response.asJsoup() val document = response.asJsoup()
for (element in document.select(map.popular.manga_css)) { for (element in document.select(map.popular.manga_css)) {
@ -84,21 +70,6 @@ class YamlOnlineSource(context: Context, mappings: Map<*, *>) : OnlineSource(con
} }
} }
override fun latestUpdatesParse(response: Response, page: MangasPage) {
val document = response.asJsoup()
for (element in document.select(map.latestupdates.manga_css)) {
Manga.create(id).apply {
title = element.text()
setUrlWithoutDomain(element.attr("href"))
page.mangas.add(this)
}
}
map.popular.next_url_css?.let { selector ->
page.nextPageUrl = document.select(selector).first()?.absUrl("href")
}
}
override fun searchMangaRequest(page: MangasPage, query: String, filters: List<Filter>): Request { override fun searchMangaRequest(page: MangasPage, query: String, filters: List<Filter>): Request {
if (page.page == 1) { if (page.page == 1) {
page.url = searchMangaInitialUrl(query, filters) page.url = searchMangaInitialUrl(query, filters)
@ -126,6 +97,33 @@ class YamlOnlineSource(context: Context, mappings: Map<*, *>) : OnlineSource(con
} }
} }
override fun latestUpdatesRequest(page: MangasPage): Request {
if (page.page == 1) {
page.url = latestUpdatesInitialUrl()
}
return when (map.latestupdates!!.method?.toLowerCase()) {
"post" -> POST(page.url, headers, map.latestupdates.createForm())
else -> GET(page.url, headers)
}
}
override fun latestUpdatesInitialUrl() = map.latestupdates!!.url
override fun latestUpdatesParse(response: Response, page: MangasPage) {
val document = response.asJsoup()
for (element in document.select(map.latestupdates!!.manga_css)) {
Manga.create(id).apply {
title = element.text()
setUrlWithoutDomain(element.attr("href"))
page.mangas.add(this)
}
}
map.latestupdates.next_url_css?.let { selector ->
page.nextPageUrl = document.select(selector).first()?.absUrl("href")
}
}
override fun mangaDetailsParse(response: Response, manga: Manga) { override fun mangaDetailsParse(response: Response, manga: Manga) {
val document = response.asJsoup() val document = response.asJsoup()
with(map.manga) { with(map.manga) {

View file

@ -25,14 +25,12 @@ class YamlSourceNode(uncheckedMap: Map<*, *>) {
val lang: String by map val lang: String by map
val supportsLatest: String by map
val client: String? val client: String?
get() = map["client"] as? String get() = map["client"] as? String
val popular = PopularNode(toMap(map["popular"])!!) val popular = PopularNode(toMap(map["popular"])!!)
val latestupdates = LatestUpdatesNode(toMap(map["latest_updates"])!!) val latestupdates = toMap(map["latest_updates"])?.let { LatestUpdatesNode(it) }
val search = SearchNode(toMap(map["search"])!!) val search = SearchNode(toMap(map["search"])!!)

View file

@ -21,7 +21,7 @@
android:title="@string/label_catalogues" /> android:title="@string/label_catalogues" />
<item <item
android:id="@+id/nav_drawer_latest_updates" android:id="@+id/nav_drawer_latest_updates"
android:icon="@drawable/ic_watch_later" android:icon="@drawable/ic_watch_later_black_24dp"
android:title="@string/label_latest_updates" /> android:title="@string/label_latest_updates" />
<item <item
android:id="@+id/nav_drawer_downloads" android:id="@+id/nav_drawer_downloads"