Skip to content

Commit e128196

Browse files
authored
Merge pull request libre-tube#7290 from Bnyro/master
feat: fallback to markdown instances list instead of hardcoded list
2 parents e6c0771 + 211e8d9 commit e128196

6 files changed

Lines changed: 25 additions & 81 deletions

File tree

app/src/main/java/com/github/libretube/api/ExternalApi.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import com.github.libretube.api.obj.SubmitSegmentResponse
99
import com.github.libretube.api.obj.VoteInfo
1010
import com.github.libretube.obj.update.UpdateInfo
1111
import kotlinx.serialization.json.JsonElement
12+
import okhttp3.ResponseBody
13+
import retrofit2.Response
1214
import retrofit2.http.Body
1315
import retrofit2.http.GET
1416
import retrofit2.http.Headers
@@ -22,11 +24,16 @@ private const val SB_API_URL = "https://sponsor.ajay.app"
2224
private const val RYD_API_URL = "https://returnyoutubedislikeapi.com"
2325
private const val GOOGLE_API_KEY = "AIzaSyDyT5W0Jh49F30Pqqtyfdf7pDLFKLJoAnw"
2426
const val USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.3"
27+
private const val PIPED_INSTANCES_URL = "https://piped-instances.kavin.rocks"
28+
private const val PIPED_INSTANCES_MARKDOWN_URL = "https://raw.githubusercontent.com/TeamPiped/documentation/refs/heads/main/content/docs/public-instances/index.md"
2529

2630
interface ExternalApi {
2731
// only for fetching servers list
2832
@GET
29-
suspend fun getInstances(@Url url: String): List<PipedInstance>
33+
suspend fun getInstances(@Url url: String = PIPED_INSTANCES_URL): List<PipedInstance>
34+
35+
@GET
36+
suspend fun getInstancesMarkdown(@Url url: String = PIPED_INSTANCES_MARKDOWN_URL): Response<ResponseBody>
3037

3138
@GET("config")
3239
suspend fun getInstanceConfig(@Url url: String): PipedConfig

app/src/main/java/com/github/libretube/api/InstanceRepository.kt

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package com.github.libretube.api
22

33
import android.content.Context
44
import com.github.libretube.R
5+
import com.github.libretube.api.RetrofitInstance.PIPED_API_URL
56
import com.github.libretube.api.obj.PipedInstance
67
import kotlinx.coroutines.Dispatchers
78
import kotlinx.coroutines.withContext
9+
import okhttp3.HttpUrl.Companion.toHttpUrl
810

911
class 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
}

app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class MainActivity : BaseActivity() {
112112

113113
val isAppConfigured = PreferenceHelper.getBoolean(PreferenceKeys.LOCAL_FEED_EXTRACTION, false) ||
114114
PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, "").isNotEmpty()
115-
if (isAppConfigured) {
115+
if (!isAppConfigured) {
116116
val welcomeIntent = Intent(this, WelcomeActivity::class.java)
117117
startActivity(welcomeIntent)
118118
finish()

app/src/main/res/values/array.xml

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3-
<string-array name="instances">
4-
<item>kavin.rocks (Official)</item>
5-
<item>syncpundit.io</item>
6-
<item>lunar.icu</item>
7-
<item>r4fo.com</item>
8-
<item>kavin.rocks libre (Official)</item>
9-
<item>projectsegfau.lt</item>
10-
<item>projectsegfau.lt us</item>
11-
<item>smnz.de</item>
12-
<item>adminforge.de</item>
13-
<item>astartes.nl</item>
14-
<item>drgns.space</item>
15-
<item>ducks.party</item>
16-
<item>ngn.tf</item>
17-
<item>coldforge.xyz</item>
18-
</string-array>
19-
20-
<string-array name="instancesValue">
21-
<item>https://pipedapi.kavin.rocks</item>
22-
<item>https://pipedapi.syncpundit.io</item>
23-
<item>https://piped-api.lunar.icu</item>
24-
<item>https://pipedapi.r4fo.com</item>
25-
<item>https://pipedapi-libre.kavin.rocks</item>
26-
<item>https://api.piped.projectsegfau.lt</item>
27-
<item>https://pipedapi.us.projectsegfau.lt</item>
28-
<item>https://pipedapi.smnz.de</item>
29-
<item>https://pipedapi.adminforge.de</item>
30-
<item>https://pipedapi.astartes.nl</item>
31-
<item>https://pipedapi.drgns.space</item>
32-
<item>https://pipedapi.ducks.party</item>
33-
<item>https://pipedapi.ngn.tf</item>
34-
<item>https://pipedapi.coldforge.xyz</item>
35-
</string-array>
36-
373
<string-array name="languages">
384
<item>@string/systemLanguage</item>
395
<item>العربية</item>

app/src/main/res/xml/instance_settings.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
<ListPreference
2828
android:icon="@drawable/ic_server"
2929
app:defaultValue="https://pipedapi.kavin.rocks"
30-
app:entries="@array/instances"
31-
app:entryValues="@array/instancesValue"
3230
app:key="selectInstance"
3331
android:dependency="full_local_mode"
3432
app:title="@string/instances" />
@@ -70,8 +68,6 @@
7068
android:dependency="auth_instance_toggle"
7169
android:icon="@drawable/ic_server"
7270
app:defaultValue="https://pipedapi.kavin.rocks"
73-
app:entries="@array/instances"
74-
app:entryValues="@array/instancesValue"
7571
app:key="selectAuthInstance"
7672
app:title="@string/auth_instances" />
7773

update-instances.sh

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)