Add unit test for readmanga source

This commit is contained in:
Admin
2020-01-31 19:51:57 +02:00
parent 9eda96c0d8
commit b1217d5f48
9 changed files with 105 additions and 11 deletions

View File

@@ -29,7 +29,7 @@ class KotatsuApp : Application() {
}
}, module {
single {
MangaLoaderContext(applicationContext)
MangaLoaderContext()
}
}, module {
single {

View File

@@ -6,10 +6,9 @@ import org.koin.core.KoinComponent
import org.koin.core.inject
import org.koitharu.kotatsu.utils.ext.await
class MangaLoaderContext(context: Context) : KoinComponent {
class MangaLoaderContext : KoinComponent {
private val okHttp by inject<OkHttpClient>()
private val preferences = context.getSharedPreferences("sources", Context.MODE_PRIVATE)
suspend fun get(url: String, block: (Request.Builder.() -> Unit)? = null): Response {
val request = Request.Builder()
@@ -38,11 +37,4 @@ class MangaLoaderContext(context: Context) : KoinComponent {
}
return okHttp.newCall(request.build()).await()
}
fun getStringOption(name: String, default: String? = null) =
preferences.getString(name, default)
fun getIntOption(name: String, default: Int) = preferences.getInt(name, default)
fun getBooleanOption(name: String, default: Boolean) = preferences.getBoolean(name, default)
}

View File

@@ -7,6 +7,7 @@ import org.koitharu.kotatsu.domain.exceptions.ParseException
import org.koitharu.kotatsu.utils.ext.longHashCode
import org.koitharu.kotatsu.utils.ext.parseHtml
import org.koitharu.kotatsu.utils.ext.safe
import org.koitharu.kotatsu.utils.ext.withDomain
class ReadmangaRepository(loaderContext: MangaLoaderContext) : MangaRepository(loaderContext) {
@@ -23,7 +24,8 @@ class ReadmangaRepository(loaderContext: MangaLoaderContext) : MangaRepository(l
return root.select("div.tile").mapNotNull { node ->
val imgDiv = node.selectFirst("div.img") ?: return@mapNotNull null
val descDiv = node.selectFirst("div.desc") ?: return@mapNotNull null
val href = imgDiv.selectFirst("a").attr("href") ?: return@mapNotNull null
val href = imgDiv.selectFirst("a").attr("href")?.withDomain("readmanga.me")
?: return@mapNotNull null
val title = descDiv.selectFirst("h3")?.selectFirst("a")?.text()
?: return@mapNotNull null
Manga(

View File

@@ -7,4 +7,9 @@ fun String.longHashCode(): Long {
h = 31 * h + this[i].toLong()
}
return h
}
fun String.withDomain(domain: String) = when {
this.startsWith("/") -> "http://$domain"
else -> this
}