feat: fallback to markdown instances list instead of hardcoded list

This commit is contained in:
Bnyro 2025-04-08 18:17:18 +02:00
parent 1da7704ca2
commit 211e8d99f5
No known key found for this signature in database
6 changed files with 25 additions and 81 deletions

View File

@ -9,6 +9,8 @@ import com.github.libretube.api.obj.SubmitSegmentResponse
import com.github.libretube.api.obj.VoteInfo
import com.github.libretube.obj.update.UpdateInfo
import kotlinx.serialization.json.JsonElement
import okhttp3.ResponseBody
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.Headers
@ -22,11 +24,16 @@ private const val SB_API_URL = "https://sponsor.ajay.app"
private const val RYD_API_URL = "https://returnyoutubedislikeapi.com"
private const val GOOGLE_API_KEY = "AIzaSyDyT5W0Jh49F30Pqqtyfdf7pDLFKLJoAnw"
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"
private const val PIPED_INSTANCES_URL = "https://piped-instances.kavin.rocks"
private const val PIPED_INSTANCES_MARKDOWN_URL = "https://raw.githubusercontent.com/TeamPiped/documentation/refs/heads/main/content/docs/public-instances/index.md"
interface ExternalApi {
// only for fetching servers list
@GET
suspend fun getInstances(@Url url: String): List<PipedInstance>
suspend fun getInstances(@Url url: String = PIPED_INSTANCES_URL): List<PipedInstance>
@GET
suspend fun getInstancesMarkdown(@Url url: String = PIPED_INSTANCES_MARKDOWN_URL): Response<ResponseBody>
@GET("config")
suspend fun getInstanceConfig(@Url url: String): PipedConfig

View File

@ -2,9 +2,11 @@ package com.github.libretube.api
import android.content.Context
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance.PIPED_API_URL
import com.github.libretube.api.obj.PipedInstance
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.HttpUrl.Companion.toHttpUrl
class InstanceRepository(private val context: Context) {
@ -13,19 +15,23 @@ class InstanceRepository(private val context: Context) {
*/
suspend fun getInstances(): Result<List<PipedInstance>> = withContext(Dispatchers.IO) {
runCatching {
RetrofitInstance.externalApi.getInstances(PIPED_INSTANCES_URL)
RetrofitInstance.externalApi.getInstances()
}
}
fun getInstancesFallback(): List<PipedInstance> {
val instanceNames = context.resources.getStringArray(R.array.instances)
return context.resources.getStringArray(R.array.instancesValue)
.mapIndexed { index, instanceValue ->
PipedInstance(instanceNames[index], instanceValue)
}
}
suspend fun getInstancesFallback(): List<PipedInstance> = withContext(Dispatchers.IO) {
return@withContext try {
RetrofitInstance.externalApi.getInstancesMarkdown().body()!!.string().lines().reversed()
.takeWhile { !it.startsWith("---") }
.filter { it.isNotBlank() }
.map { line ->
val infoParts = line.split("|")
companion object {
private const val PIPED_INSTANCES_URL = "https://piped-instances.kavin.rocks"
PipedInstance(name = infoParts[0], apiUrl = infoParts[1], locations = infoParts[2], cdn = infoParts[3] == "Yes")
}
} catch (e: Exception) {
// worst case scenario: only return official instance
return@withContext listOf(PipedInstance(name = PIPED_API_URL.toHttpUrl().host, apiUrl = PIPED_API_URL))
}
}
}

View File

@ -112,7 +112,7 @@ class MainActivity : BaseActivity() {
val isAppConfigured = PreferenceHelper.getBoolean(PreferenceKeys.LOCAL_FEED_EXTRACTION, false) ||
PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, "").isNotEmpty()
if (isAppConfigured) {
if (!isAppConfigured) {
val welcomeIntent = Intent(this, WelcomeActivity::class.java)
startActivity(welcomeIntent)
finish()

View File

@ -1,39 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="instances">
<item>kavin.rocks (Official)</item>
<item>syncpundit.io</item>
<item>lunar.icu</item>
<item>r4fo.com</item>
<item>kavin.rocks libre (Official)</item>
<item>projectsegfau.lt</item>
<item>projectsegfau.lt us</item>
<item>smnz.de</item>
<item>adminforge.de</item>
<item>astartes.nl</item>
<item>drgns.space</item>
<item>ducks.party</item>
<item>ngn.tf</item>
<item>coldforge.xyz</item>
</string-array>
<string-array name="instancesValue">
<item>https://pipedapi.kavin.rocks</item>
<item>https://pipedapi.syncpundit.io</item>
<item>https://piped-api.lunar.icu</item>
<item>https://pipedapi.r4fo.com</item>
<item>https://pipedapi-libre.kavin.rocks</item>
<item>https://api.piped.projectsegfau.lt</item>
<item>https://pipedapi.us.projectsegfau.lt</item>
<item>https://pipedapi.smnz.de</item>
<item>https://pipedapi.adminforge.de</item>
<item>https://pipedapi.astartes.nl</item>
<item>https://pipedapi.drgns.space</item>
<item>https://pipedapi.ducks.party</item>
<item>https://pipedapi.ngn.tf</item>
<item>https://pipedapi.coldforge.xyz</item>
</string-array>
<string-array name="languages">
<item>@string/systemLanguage</item>
<item>العربية</item>

View File

@ -27,8 +27,6 @@
<ListPreference
android:icon="@drawable/ic_server"
app:defaultValue="https://pipedapi.kavin.rocks"
app:entries="@array/instances"
app:entryValues="@array/instancesValue"
app:key="selectInstance"
android:dependency="full_local_mode"
app:title="@string/instances" />
@ -70,8 +68,6 @@
android:dependency="auth_instance_toggle"
android:icon="@drawable/ic_server"
app:defaultValue="https://pipedapi.kavin.rocks"
app:entries="@array/instances"
app:entryValues="@array/instancesValue"
app:key="selectAuthInstance"
app:title="@string/auth_instances" />

View File

@ -1,31 +0,0 @@
#!/bin/bash
# fetch instances from public api
INSTANCES=$(curl -s 'https://piped-instances.kavin.rocks')
# generate instances list for settings
echo -e "\nContent for res/values/array.xml\n"
echo '<string-array name="instances">'
echo $INSTANCES | jq '.[].name' | while read name; do
echo " <item>$name</item>" | tr -d '"'
done
echo -e '</string-array>\n\n<string-array name="instancesValue">'
echo $INSTANCES | jq '.[].api_url' | while read url; do
echo " <item>$url</item>" | tr -d '"'
done
echo -e '</string-array>'
# generate android url schemes
echo -e "\n\nContent for AndroidManifest.xml to be replaced\n"
gen_frontends() {
echo $INSTANCES | jq '.[].api_url' | while read url; do
_url=$(echo "$url" | tr -d '"')
_frontend_url=$(curl -Ls -o /dev/null -w %{url_effective} "$_url")
_host=$(echo ${_frontend_url/https:\/\//} | tr -d '/')
echo " <data android:host=\"$_host\" />"
done
}
echo "$(gen_frontends)" | sort | uniq