Skip to content

Commit

Permalink
decrease server downtime
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Apr 22, 2023
1 parent 0e59a79 commit 093c3c4
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ object NetworkModule {
jsonBaseKtor.create<JsonBase>()
}
bindSingleton {
HomeRepository(instance())
HomeRepository(instance(), instance())
}
bindSingleton {
GenreRepository(instance())
GenreRepository(instance(), instance())
}
bindProvider {
SeriesRepository(instance(), instance())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ actual object BsScraper {
}
}

actual fun client(client: HttpClient) = apply {
JvmBsScraper.client(client)
}

actual suspend fun getHome(url: String?): Home? {
return try {
if (url != null) {
Expand Down Expand Up @@ -45,11 +49,11 @@ actual object BsScraper {
}
}

actual suspend fun getSeries(url: String?, client: HttpClient): Series? {
actual suspend fun getSeries(url: String?): Series? {
return try {
if (url != null) {
JvmBsScraper.getDocument(JvmBsScraper.fixSeriesHref(url))?.let {
JvmBsScraper.getSeries(it, client)
JvmBsScraper.getSeries(it)
}
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import io.ktor.client.*

expect object BsScraper {

actual fun client(client: HttpClient): BsScraper

suspend fun getHome(url: String?): Home?

suspend fun getAll(url: String?): List<Genre>?

actual suspend fun getSeries(url: String?, client: HttpClient): Series?
actual suspend fun getSeries(url: String?): Series?
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.withContext
import dev.datlag.burningseries.network.common.Dispatchers
import io.ktor.client.*

class GenreRepository(
private val api: BurningSeries
private val api: BurningSeries,
private val client: HttpClient
) {

val allState: MutableStateFlow<List<Genre>> = MutableStateFlow(emptyList())

private val all: Flow<Resource<List<Genre>>> = dbBoundResource(
makeNetworkRequest = {
BsScraper.getAll("andere-serien")?.let { genres ->
BsScraper.client(client).getAll("andere-serien")?.let { genres ->
ApiSuccessResponse(genres, emptySet())
} ?: api.all()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import dev.datlag.burningseries.network.common.Dispatchers
import io.ktor.client.*

class HomeRepository(
private val api: BurningSeries
private val api: BurningSeries,
private val client: HttpClient
) {

val homeState: MutableStateFlow<Home> = MutableStateFlow(Home())

private val home: Flow<Resource<Home>> = dbBoundResource(
makeNetworkRequest = {
BsScraper.getHome(String())?.let { home ->
BsScraper.client(client).getHome(String())?.let { home ->
ApiSuccessResponse(home, emptySet())
} ?: api.home()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SeriesRepository(
if (it != null) {
return@transformLatest emitAll(dbBoundResource(
makeNetworkRequest = {
BsScraper.getSeries(it, client)?.let { series ->
BsScraper.client(client).getSeries(it)?.let { series ->
ApiSuccessResponse(series, emptySet())
} ?: api.series(it)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import dev.datlag.burningseries.model.Series
import io.ktor.client.*

actual object BsScraper {

actual fun client(client: HttpClient): BsScraper = apply { }

actual suspend fun getHome(url: String?): Home? {
return null
}
Expand All @@ -14,10 +17,7 @@ actual object BsScraper {
return null
}

actual suspend fun getSeries(
url: String?,
client: HttpClient
): Series? {
actual suspend fun getSeries(url: String?): Series? {
return null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ actual object BsScraper {
}
}

actual fun client(client: HttpClient) = apply {
JvmBsScraper.client(client)
}

actual suspend fun getHome(url: String?): Home? {
return try {
if (url != null) {
Expand Down Expand Up @@ -45,11 +49,11 @@ actual object BsScraper {
}
}

actual suspend fun getSeries(url: String?, client: HttpClient): Series? {
actual suspend fun getSeries(url: String?): Series? {
return try {
if (url != null) {
JvmBsScraper.getDocument(JvmBsScraper.fixSeriesHref(url))?.let {
JvmBsScraper.getSeries(it, client)
JvmBsScraper.getSeries(it)
}
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import dev.datlag.burningseries.scraper.common.*
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
Expand All @@ -27,10 +28,22 @@ object JvmBsScraper {
Cover(String()) to false
}

private var client: HttpClient? = null

fun client(client: HttpClient) = apply {
if (this.client == null) {
this.client = client
}
}

suspend fun getDocument(url: String): Document? = withTimeout(15.seconds) {
return@withTimeout suspendCatching {
Jsoup.connect(Constants.getBurningSeriesLink(url)).followRedirects(true).get()
}.getOrNull()
}.getOrNull() ?: client?.let {
suspendCatching {
Jsoup.parse(it.get(Constants.getBurningSeriesLink(url)).bodyAsText())
}.getOrNull()
}
}

suspend fun getLatestEpisodes(doc: Document): List<Home.Episode> = coroutineScope {
Expand Down Expand Up @@ -119,7 +132,7 @@ object JvmBsScraper {
}.awaitAll().filterNotNull()
}

suspend fun getSeries(doc: Document, client: HttpClient): Series? {
suspend fun getSeries(doc: Document): Series? {
val title = doc.selectFirst(".serie h2")?.wholeText() ?: String()
val description = doc.selectFirst(".serie #sp_left > p")?.text() ?: String()

Expand Down

0 comments on commit 093c3c4

Please sign in to comment.