Parsers fixes

This commit is contained in:
Koitharu
2020-03-14 18:41:54 +02:00
parent 3fae457ec6
commit c765e03b28
9 changed files with 88 additions and 61 deletions

View File

@@ -13,11 +13,11 @@ import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin import org.koin.core.context.startKoin
import org.koin.dsl.module import org.koin.dsl.module
import org.koitharu.kotatsu.core.db.MangaDatabase import org.koitharu.kotatsu.core.db.MangaDatabase
import org.koitharu.kotatsu.core.local.CbzFetcher
import org.koitharu.kotatsu.core.local.PagesCache
import org.koitharu.kotatsu.core.local.cookies.PersistentCookieJar import org.koitharu.kotatsu.core.local.cookies.PersistentCookieJar
import org.koitharu.kotatsu.core.local.cookies.cache.SetCookieCache import org.koitharu.kotatsu.core.local.cookies.cache.SetCookieCache
import org.koitharu.kotatsu.core.local.cookies.persistence.SharedPrefsCookiePersistor import org.koitharu.kotatsu.core.local.cookies.persistence.SharedPrefsCookiePersistor
import org.koitharu.kotatsu.core.local.CbzFetcher
import org.koitharu.kotatsu.core.local.PagesCache
import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.domain.MangaLoaderContext import org.koitharu.kotatsu.domain.MangaLoaderContext
import org.koitharu.kotatsu.utils.CacheUtils import org.koitharu.kotatsu.utils.CacheUtils
@@ -40,27 +40,25 @@ class KotatsuApp : Application() {
startKoin { startKoin {
androidLogger() androidLogger()
androidContext(applicationContext) androidContext(applicationContext)
modules(listOf( module {
module { factory {
factory { okHttp()
okHttp() .cache(CacheUtils.createHttpCache(applicationContext))
.cache(CacheUtils.createHttpCache(applicationContext)) .build()
.build()
}
single {
mangaDb().build()
}
single {
MangaLoaderContext()
}
factory {
AppSettings(applicationContext)
}
single {
PagesCache(applicationContext)
}
} }
)) single {
mangaDb().build()
}
single {
MangaLoaderContext()
}
factory {
AppSettings(applicationContext)
}
single {
PagesCache(applicationContext)
}
}
} }
} }

View File

@@ -26,7 +26,7 @@ abstract class GroupleRepository : RemoteMangaRepository() {
val doc = when { val doc = when {
!query.isNullOrEmpty() -> loaderContext.httpPost( !query.isNullOrEmpty() -> loaderContext.httpPost(
"https://$domain/search", "https://$domain/search",
mapOf("q" to query) mapOf("q" to query, "offset" to offset.toString())
) )
tag == null -> loaderContext.httpGet("https://$domain/list?sortType=${getSortKey(sortOrder)}&offset=$offset") tag == null -> loaderContext.httpGet("https://$domain/list?sortType=${getSortKey(sortOrder)}&offset=$offset")
else -> loaderContext.httpGet( else -> loaderContext.httpGet(
@@ -40,7 +40,7 @@ abstract class GroupleRepository : RemoteMangaRepository() {
return root.select("div.tile").mapNotNull { node -> return root.select("div.tile").mapNotNull { node ->
val imgDiv = node.selectFirst("div.img") ?: return@mapNotNull null val imgDiv = node.selectFirst("div.img") ?: return@mapNotNull null
val descDiv = node.selectFirst("div.desc") ?: return@mapNotNull null val descDiv = node.selectFirst("div.desc") ?: return@mapNotNull null
if (descDiv.getElementsByAttributeValue("data-type", "author").isNotEmpty()) { if (descDiv.selectFirst("i.fa-user") != null) {
return@mapNotNull null //skip author return@mapNotNull null //skip author
} }
val href = imgDiv.selectFirst("a").attr("href")?.withDomain(domain) val href = imgDiv.selectFirst("a").attr("href")?.withDomain(domain)
@@ -87,7 +87,7 @@ abstract class GroupleRepository : RemoteMangaRepository() {
val doc = loaderContext.httpGet(manga.url).parseHtml() val doc = loaderContext.httpGet(manga.url).parseHtml()
val root = doc.body().getElementById("mangaBox") ?: throw ParseException("Cannot find root") val root = doc.body().getElementById("mangaBox") ?: throw ParseException("Cannot find root")
return manga.copy( return manga.copy(
description = root.selectFirst("div.manga-description").firstChild()?.html(), description = root.selectFirst("div.manga-description")?.html(),
largeCoverUrl = root.selectFirst("div.subject-cower")?.selectFirst("img")?.attr( largeCoverUrl = root.selectFirst("div.subject-cower")?.selectFirst("img")?.attr(
"data-full" "data-full"
), ),

View File

@@ -1,9 +1,39 @@
package org.koitharu.kotatsu.core.parser.site package org.koitharu.kotatsu.core.parser.site
import org.koitharu.kotatsu.core.exceptions.ParseException
import org.koitharu.kotatsu.core.model.Manga
import org.koitharu.kotatsu.core.model.MangaChapter
import org.koitharu.kotatsu.core.model.MangaSource import org.koitharu.kotatsu.core.model.MangaSource
import org.koitharu.kotatsu.utils.ext.longHashCode
import org.koitharu.kotatsu.utils.ext.parseHtml
import org.koitharu.kotatsu.utils.ext.withDomain
class HenChanRepository : ChanRepository() { class HenChanRepository : ChanRepository() {
override val defaultDomain = "h-chan.me" override val defaultDomain = "h-chan.me"
override val source = MangaSource.HENCHAN override val source = MangaSource.HENCHAN
override suspend fun getDetails(manga: Manga): Manga {
val domain = conf.getDomain(defaultDomain)
val doc = loaderContext.httpGet(manga.url).parseHtml()
val root =
doc.body().getElementById("dle-content") ?: throw ParseException("Cannot find root")
return manga.copy(
description = root.getElementById("description")?.html()?.substringBeforeLast("<div"),
largeCoverUrl = root.getElementById("cover")?.attr("src")?.withDomain(domain),
chapters = root.getElementById("right").select("table.table_cha").flatMap { table ->
table.select("div.manga2")
}.mapNotNull { it.selectFirst("a") }.reversed().mapIndexedNotNull { i, a ->
val href = a.attr("href")
?.withDomain(domain) ?: return@mapIndexedNotNull null
MangaChapter(
id = href.longHashCode(),
name = a.text().trim(),
number = i + 1,
url = href,
source = source
)
}
)
}
} }

View File

@@ -10,6 +10,8 @@ class SourceConfig(context: Context, source: MangaSource) {
private val keyDomain = context.getString(R.string.key_parser_domain) private val keyDomain = context.getString(R.string.key_parser_domain)
fun getDomain(defaultValue: String) = prefs.getString(keyDomain, defaultValue) ?: defaultValue fun getDomain(defaultValue: String) = prefs.getString(keyDomain, defaultValue)
?.takeUnless(String::isBlank)
?: defaultValue
} }

View File

@@ -16,17 +16,14 @@ abstract class RepositoryTestEnvironment {
@BeforeClass @BeforeClass
fun initialize(source: MangaSource) { fun initialize(source: MangaSource) {
startKoin { startKoin {
modules(listOf( module {
module { factory {
factory { OkHttpClient()
OkHttpClient()
}
}, module {
single {
MangaLoaderContext()
}
} }
)) single {
MangaLoaderContext()
}
}
} }
val constructor = source.cls.getConstructor(MangaLoaderContext::class.java) val constructor = source.cls.getConstructor(MangaLoaderContext::class.java)
repository = constructor.newInstance(MangaLoaderContext()) repository = constructor.newInstance(MangaLoaderContext())

View File

@@ -8,7 +8,7 @@ import org.junit.runner.RunWith
import org.koitharu.kotatsu.core.model.MangaSource import org.koitharu.kotatsu.core.model.MangaSource
import org.koitharu.kotatsu.parsers.MangaParserTest import org.koitharu.kotatsu.parsers.MangaParserTest
import org.koitharu.kotatsu.parsers.RepositoryTestEnvironment import org.koitharu.kotatsu.parsers.RepositoryTestEnvironment
import org.koitharu.kotatsu.utils.TestUtil import org.koitharu.kotatsu.utils.AssertX
import org.mockito.junit.MockitoJUnitRunner import org.mockito.junit.MockitoJUnitRunner
@RunWith(MockitoJUnitRunner::class) @RunWith(MockitoJUnitRunner::class)
@@ -21,8 +21,8 @@ class MintMangaTest : MangaParserTest {
val item = list[40] val item = list[40]
Assert.assertTrue(item.title.isNotEmpty()) Assert.assertTrue(item.title.isNotEmpty())
Assert.assertTrue(item.rating in 0f..1f) Assert.assertTrue(item.rating in 0f..1f)
TestUtil.assertValidUrl(item.url) AssertX.assertValidUrl(item.url)
TestUtil.assertValidUrl(item.coverUrl) AssertX.assertValidUrl(item.coverUrl)
Assert.assertEquals(item.source, MangaSource.MINTMANGA) Assert.assertEquals(item.source, MangaSource.MINTMANGA)
} }
@@ -30,11 +30,11 @@ class MintMangaTest : MangaParserTest {
override fun testMangaDetails() { override fun testMangaDetails() {
val manga = getMangaItem() val manga = getMangaItem()
Assert.assertNotNull(manga.largeCoverUrl) Assert.assertNotNull(manga.largeCoverUrl)
TestUtil.assertValidUrl(manga.largeCoverUrl!!) AssertX.assertValidUrl(manga.largeCoverUrl!!)
Assert.assertNotNull(manga.chapters) Assert.assertNotNull(manga.chapters)
val chapter = manga.chapters!!.last() val chapter = manga.chapters!!.last()
Assert.assertEquals(chapter.source, MangaSource.MINTMANGA) Assert.assertEquals(chapter.source, MangaSource.MINTMANGA)
TestUtil.assertValidUrl(chapter.url) AssertX.assertValidUrl(chapter.url)
} }
@Test @Test
@@ -43,8 +43,8 @@ class MintMangaTest : MangaParserTest {
val pages = runBlocking { repository.getPages(chapter) } val pages = runBlocking { repository.getPages(chapter) }
Assert.assertFalse(pages.isEmpty()) Assert.assertFalse(pages.isEmpty())
Assert.assertEquals(pages.first().source, MangaSource.MINTMANGA) Assert.assertEquals(pages.first().source, MangaSource.MINTMANGA)
TestUtil.assertValidUrl(runBlocking { repository.getPageFullUrl(pages.first()) }) AssertX.assertValidUrl(runBlocking { repository.getPageFullUrl(pages.first()) })
TestUtil.assertValidUrl(runBlocking { repository.getPageFullUrl(pages.last()) }) AssertX.assertValidUrl(runBlocking { repository.getPageFullUrl(pages.last()) })
} }
@Test @Test
@@ -54,7 +54,7 @@ class MintMangaTest : MangaParserTest {
val tag = tags.first() val tag = tags.first()
Assert.assertFalse(tag.title.isBlank()) Assert.assertFalse(tag.title.isBlank())
Assert.assertEquals(tag.source, MangaSource.MINTMANGA) Assert.assertEquals(tag.source, MangaSource.MINTMANGA)
TestUtil.assertValidUrl("https://mintmanga.live/list/genre/${tag.key}") AssertX.assertValidUrl("https://mintmanga.live/list/genre/${tag.key}")
} }
companion object : RepositoryTestEnvironment() { companion object : RepositoryTestEnvironment() {

View File

@@ -8,7 +8,7 @@ import org.junit.runner.RunWith
import org.koitharu.kotatsu.core.model.MangaSource import org.koitharu.kotatsu.core.model.MangaSource
import org.koitharu.kotatsu.parsers.MangaParserTest import org.koitharu.kotatsu.parsers.MangaParserTest
import org.koitharu.kotatsu.parsers.RepositoryTestEnvironment import org.koitharu.kotatsu.parsers.RepositoryTestEnvironment
import org.koitharu.kotatsu.utils.TestUtil import org.koitharu.kotatsu.utils.AssertX
import org.mockito.junit.MockitoJUnitRunner import org.mockito.junit.MockitoJUnitRunner
@RunWith(MockitoJUnitRunner::class) @RunWith(MockitoJUnitRunner::class)
@@ -21,8 +21,8 @@ class ReadmangaRuTest : MangaParserTest {
val item = list[40] val item = list[40]
Assert.assertTrue(item.title.isNotEmpty()) Assert.assertTrue(item.title.isNotEmpty())
Assert.assertTrue(item.rating in 0f..1f) Assert.assertTrue(item.rating in 0f..1f)
TestUtil.assertValidUrl(item.url) AssertX.assertValidUrl(item.url)
TestUtil.assertValidUrl(item.coverUrl) AssertX.assertValidUrl(item.coverUrl)
Assert.assertEquals(item.source, MangaSource.READMANGA_RU) Assert.assertEquals(item.source, MangaSource.READMANGA_RU)
} }
@@ -30,11 +30,11 @@ class ReadmangaRuTest : MangaParserTest {
override fun testMangaDetails() { override fun testMangaDetails() {
val manga = getMangaItem() val manga = getMangaItem()
Assert.assertNotNull(manga.largeCoverUrl) Assert.assertNotNull(manga.largeCoverUrl)
TestUtil.assertValidUrl(manga.largeCoverUrl!!) AssertX.assertValidUrl(manga.largeCoverUrl!!)
Assert.assertNotNull(manga.chapters) Assert.assertNotNull(manga.chapters)
val chapter = manga.chapters!!.last() val chapter = manga.chapters!!.last()
Assert.assertEquals(chapter.source, MangaSource.READMANGA_RU) Assert.assertEquals(chapter.source, MangaSource.READMANGA_RU)
TestUtil.assertValidUrl(chapter.url) AssertX.assertValidUrl(chapter.url)
} }
@Test @Test
@@ -43,8 +43,8 @@ class ReadmangaRuTest : MangaParserTest {
val pages = runBlocking { repository.getPages(chapter) } val pages = runBlocking { repository.getPages(chapter) }
Assert.assertFalse(pages.isEmpty()) Assert.assertFalse(pages.isEmpty())
Assert.assertEquals(pages.first().source, MangaSource.READMANGA_RU) Assert.assertEquals(pages.first().source, MangaSource.READMANGA_RU)
TestUtil.assertValidUrl(runBlocking { repository.getPageFullUrl(pages.first()) }) AssertX.assertValidUrl(runBlocking { repository.getPageFullUrl(pages.first()) })
TestUtil.assertValidUrl(runBlocking { repository.getPageFullUrl(pages.last()) }) AssertX.assertValidUrl(runBlocking { repository.getPageFullUrl(pages.last()) })
} }
@Test @Test
@@ -54,7 +54,7 @@ class ReadmangaRuTest : MangaParserTest {
val tag = tags.first() val tag = tags.first()
Assert.assertFalse(tag.title.isBlank()) Assert.assertFalse(tag.title.isBlank())
Assert.assertEquals(tag.source, MangaSource.READMANGA_RU) Assert.assertEquals(tag.source, MangaSource.READMANGA_RU)
TestUtil.assertValidUrl("https://readmanga.me/list/genre/${tag.key}") AssertX.assertValidUrl("https://readmanga.me/list/genre/${tag.key}")
} }
companion object : RepositoryTestEnvironment() { companion object : RepositoryTestEnvironment() {

View File

@@ -8,7 +8,7 @@ import org.junit.runner.RunWith
import org.koitharu.kotatsu.core.model.MangaSource import org.koitharu.kotatsu.core.model.MangaSource
import org.koitharu.kotatsu.parsers.MangaParserTest import org.koitharu.kotatsu.parsers.MangaParserTest
import org.koitharu.kotatsu.parsers.RepositoryTestEnvironment import org.koitharu.kotatsu.parsers.RepositoryTestEnvironment
import org.koitharu.kotatsu.utils.TestUtil import org.koitharu.kotatsu.utils.AssertX
import org.mockito.junit.MockitoJUnitRunner import org.mockito.junit.MockitoJUnitRunner
@RunWith(MockitoJUnitRunner::class) @RunWith(MockitoJUnitRunner::class)
@@ -21,8 +21,8 @@ class SelfMangaTest : MangaParserTest {
val item = list[40] val item = list[40]
Assert.assertTrue(item.title.isNotEmpty()) Assert.assertTrue(item.title.isNotEmpty())
Assert.assertTrue(item.rating in 0f..1f) Assert.assertTrue(item.rating in 0f..1f)
TestUtil.assertValidUrl(item.url) AssertX.assertValidUrl(item.url)
TestUtil.assertValidUrl(item.coverUrl) AssertX.assertValidUrl(item.coverUrl)
Assert.assertEquals(item.source, MangaSource.SELFMANGA) Assert.assertEquals(item.source, MangaSource.SELFMANGA)
} }
@@ -30,11 +30,11 @@ class SelfMangaTest : MangaParserTest {
override fun testMangaDetails() { override fun testMangaDetails() {
val manga = getMangaItem() val manga = getMangaItem()
Assert.assertNotNull(manga.largeCoverUrl) Assert.assertNotNull(manga.largeCoverUrl)
TestUtil.assertValidUrl(manga.largeCoverUrl!!) AssertX.assertValidUrl(manga.largeCoverUrl!!)
Assert.assertNotNull(manga.chapters) Assert.assertNotNull(manga.chapters)
val chapter = manga.chapters!!.last() val chapter = manga.chapters!!.last()
Assert.assertEquals(chapter.source, MangaSource.SELFMANGA) Assert.assertEquals(chapter.source, MangaSource.SELFMANGA)
TestUtil.assertValidUrl(chapter.url) AssertX.assertValidUrl(chapter.url)
} }
@Test @Test
@@ -43,8 +43,8 @@ class SelfMangaTest : MangaParserTest {
val pages = runBlocking { repository.getPages(chapter) } val pages = runBlocking { repository.getPages(chapter) }
Assert.assertFalse(pages.isEmpty()) Assert.assertFalse(pages.isEmpty())
Assert.assertEquals(pages.first().source, MangaSource.SELFMANGA) Assert.assertEquals(pages.first().source, MangaSource.SELFMANGA)
TestUtil.assertValidUrl(runBlocking { repository.getPageFullUrl(pages.first()) }) AssertX.assertValidUrl(runBlocking { repository.getPageFullUrl(pages.first()) })
TestUtil.assertValidUrl(runBlocking { repository.getPageFullUrl(pages.last()) }) AssertX.assertValidUrl(runBlocking { repository.getPageFullUrl(pages.last()) })
} }
@Test @Test
@@ -54,7 +54,7 @@ class SelfMangaTest : MangaParserTest {
val tag = tags.first() val tag = tags.first()
Assert.assertFalse(tag.title.isBlank()) Assert.assertFalse(tag.title.isBlank())
Assert.assertEquals(tag.source, MangaSource.SELFMANGA) Assert.assertEquals(tag.source, MangaSource.SELFMANGA)
TestUtil.assertValidUrl("https://selfmanga.ru/list/genre/${tag.key}") AssertX.assertValidUrl("https://selfmanga.ru/list/genre/${tag.key}")
} }
companion object : RepositoryTestEnvironment() { companion object : RepositoryTestEnvironment() {

View File

@@ -4,7 +4,7 @@ import org.junit.Assert
import java.net.HttpURLConnection import java.net.HttpURLConnection
import java.net.URL import java.net.URL
object TestUtil { object AssertX {
private val VALID_RESPONSE_CODES = arrayOf( private val VALID_RESPONSE_CODES = arrayOf(
HttpURLConnection.HTTP_OK, HttpURLConnection.HTTP_OK,