mini api fix

This commit is contained in:
reduplicated 2022-10-11 15:24:16 +02:00
parent af4d57e842
commit 661f8c3c4e
2 changed files with 42 additions and 11 deletions

View file

@ -22,7 +22,6 @@ import okhttp3.Interceptor
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
import kotlin.math.absoluteValue import kotlin.math.absoluteValue
import kotlin.collections.MutableList
const val USER_AGENT = const val USER_AGENT =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
@ -332,13 +331,24 @@ data class SettingsJson(
data class MainPageData( data class MainPageData(
val name: String, val name: String,
val data: String, val data: String,
val horizontalImages: Boolean = false
) )
data class MainPageRequest( data class MainPageRequest(
val name: String, val name: String,
val data: String, val data: String,
val horizontalImages: Boolean,
//TODO genre selection or smth
) )
fun mainPage(url: String, name: String, horizontalImages: Boolean = false): MainPageData {
return MainPageData(name = name, data = url, horizontalImages = horizontalImages)
}
fun mainPageOf(vararg elements: MainPageData): List<MainPageData> {
return elements.toList()
}
/** return list of MainPageData with url to name, make for more readable code */ /** return list of MainPageData with url to name, make for more readable code */
fun mainPageOf(vararg elements: Pair<String, String>): List<MainPageData> { fun mainPageOf(vararg elements: Pair<String, String>): List<MainPageData> {
return elements.map { (url, name) -> MainPageData(name = name, data = url) } return elements.map { (url, name) -> MainPageData(name = name, data = url) }
@ -347,7 +357,7 @@ fun mainPageOf(vararg elements: Pair<String, String>): List<MainPageData> {
fun newHomePageResponse( fun newHomePageResponse(
name: String, name: String,
list: List<SearchResponse>, list: List<SearchResponse>,
hasNext: Boolean? = null hasNext: Boolean? = null,
): HomePageResponse { ): HomePageResponse {
return HomePageResponse( return HomePageResponse(
listOf(HomePageList(name, list)), listOf(HomePageList(name, list)),
@ -355,6 +365,17 @@ fun newHomePageResponse(
) )
} }
fun newHomePageResponse(
data: MainPageRequest,
list: List<SearchResponse>,
hasNext: Boolean? = null,
): HomePageResponse {
return HomePageResponse(
listOf(HomePageList(data.name, list, data.horizontalImages)),
hasNext = hasNext ?: list.isNotEmpty()
)
}
fun newHomePageResponse(list: HomePageList, hasNext: Boolean? = null): HomePageResponse { fun newHomePageResponse(list: HomePageList, hasNext: Boolean? = null): HomePageResponse {
return HomePageResponse(listOf(list), hasNext = hasNext ?: list.list.isNotEmpty()) return HomePageResponse(listOf(list), hasNext = hasNext ?: list.list.isNotEmpty())
} }
@ -392,11 +413,14 @@ abstract class MainAPI {
/** if this is turned on then it will request the homepage one after the other, /** if this is turned on then it will request the homepage one after the other,
used to delay if they block many request at the same time*/ used to delay if they block many request at the same time*/
open var sequentialMainPage: Boolean = false open var sequentialMainPage: Boolean = false
/** in milliseconds, this can be used to add more delay between homepage requests /** in milliseconds, this can be used to add more delay between homepage requests
* on first load if sequentialMainPage is turned on */ * on first load if sequentialMainPage is turned on */
open var sequentialMainPageDelay: Long = 0L open var sequentialMainPageDelay: Long = 0L
/** in milliseconds, this can be used to add more delay between homepage requests when scrolling */ /** in milliseconds, this can be used to add more delay between homepage requests when scrolling */
open var sequentialMainPageScrollDelay: Long = 0L open var sequentialMainPageScrollDelay: Long = 0L
/** used to keep track when last homepage request was in unixtime ms */ /** used to keep track when last homepage request was in unixtime ms */
var lastHomepageRequest: Long = 0L var lastHomepageRequest: Long = 0L
@ -431,7 +455,8 @@ abstract class MainAPI {
open val vpnStatus = VPNStatus.None open val vpnStatus = VPNStatus.None
open val providerType = ProviderType.DirectProvider open val providerType = ProviderType.DirectProvider
open val mainPage = listOf(MainPageData("", "")) //emptyList<MainPageData>() //
open val mainPage = listOf(MainPageData("", "", false))
@WorkerThread @WorkerThread
open suspend fun getMainPage( open suspend fun getMainPage(

View file

@ -75,7 +75,7 @@ class APIRepository(val api: MainAPI) {
api.lastHomepageRequest = unixTimeMS api.lastHomepageRequest = unixTimeMS
nameIndex?.let { api.mainPage.getOrNull(it) }?.let { data -> nameIndex?.let { api.mainPage.getOrNull(it) }?.let { data ->
listOf(api.getMainPage(page, MainPageRequest(data.name, data.data))) listOf(api.getMainPage(page, MainPageRequest(data.name, data.data, data.horizontalImages)))
} ?: run { } ?: run {
if (api.sequentialMainPage) { if (api.sequentialMainPage) {
var first = true var first = true
@ -84,11 +84,17 @@ class APIRepository(val api: MainAPI) {
delay(api.sequentialMainPageDelay) delay(api.sequentialMainPageDelay)
first = false first = false
api.getMainPage(page, MainPageRequest(data.name, data.data)) api.getMainPage(
page,
MainPageRequest(data.name, data.data, data.horizontalImages)
)
} }
} else { } else {
api.mainPage.apmap { data -> api.mainPage.apmap { data ->
api.getMainPage(page, MainPageRequest(data.name, data.data)) api.getMainPage(
page,
MainPageRequest(data.name, data.data, data.horizontalImages)
)
} }
} }
} }