@@ -2,9 +2,11 @@ package com.github.libretube.api
22
33import android.content.Context
44import com.github.libretube.R
5+ import com.github.libretube.api.RetrofitInstance.PIPED_API_URL
56import com.github.libretube.api.obj.PipedInstance
67import kotlinx.coroutines.Dispatchers
78import kotlinx.coroutines.withContext
9+ import okhttp3.HttpUrl.Companion.toHttpUrl
810
911class InstanceRepository (private val context : Context ) {
1012
@@ -13,19 +15,23 @@ class InstanceRepository(private val context: Context) {
1315 */
1416 suspend fun getInstances (): Result <List <PipedInstance >> = withContext(Dispatchers .IO ) {
1517 runCatching {
16- RetrofitInstance .externalApi.getInstances(PIPED_INSTANCES_URL )
18+ RetrofitInstance .externalApi.getInstances()
1719 }
1820 }
1921
20- fun getInstancesFallback (): List <PipedInstance > {
21- val instanceNames = context.resources.getStringArray( R .array.instances)
22- return context.resources.getStringArray( R .array.instancesValue )
23- .mapIndexed { index, instanceValue ->
24- PipedInstance (instanceNames[index], instanceValue)
25- }
26- }
22+ suspend fun getInstancesFallback (): List <PipedInstance > = withContext( Dispatchers . IO ) {
23+ return @withContext try {
24+ RetrofitInstance .externalApi.getInstancesMarkdown().body() !! .string().lines().reversed( )
25+ . takeWhile { ! it.startsWith( " --- " ) }
26+ .filter { it.isNotBlank() }
27+ .map { line ->
28+ val infoParts = line.split( " | " )
2729
28- companion object {
29- private const val PIPED_INSTANCES_URL = " https://piped-instances.kavin.rocks"
30+ PipedInstance (name = infoParts[0 ], apiUrl = infoParts[1 ], locations = infoParts[2 ], cdn = infoParts[3 ] == " Yes" )
31+ }
32+ } catch (e: Exception ) {
33+ // worst case scenario: only return official instance
34+ return @withContext listOf (PipedInstance (name = PIPED_API_URL .toHttpUrl().host, apiUrl = PIPED_API_URL ))
35+ }
3036 }
3137}
0 commit comments