mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 07:50:31 +05:30
feat: fallback to markdown instances list instead of hardcoded list
This commit is contained in:
parent
1da7704ca2
commit
211e8d99f5
@ -9,6 +9,8 @@ import com.github.libretube.api.obj.SubmitSegmentResponse
|
|||||||
import com.github.libretube.api.obj.VoteInfo
|
import com.github.libretube.api.obj.VoteInfo
|
||||||
import com.github.libretube.obj.update.UpdateInfo
|
import com.github.libretube.obj.update.UpdateInfo
|
||||||
import kotlinx.serialization.json.JsonElement
|
import kotlinx.serialization.json.JsonElement
|
||||||
|
import okhttp3.ResponseBody
|
||||||
|
import retrofit2.Response
|
||||||
import retrofit2.http.Body
|
import retrofit2.http.Body
|
||||||
import retrofit2.http.GET
|
import retrofit2.http.GET
|
||||||
import retrofit2.http.Headers
|
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 RYD_API_URL = "https://returnyoutubedislikeapi.com"
|
||||||
private const val GOOGLE_API_KEY = "AIzaSyDyT5W0Jh49F30Pqqtyfdf7pDLFKLJoAnw"
|
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"
|
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 {
|
interface ExternalApi {
|
||||||
// only for fetching servers list
|
// only for fetching servers list
|
||||||
@GET
|
@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")
|
@GET("config")
|
||||||
suspend fun getInstanceConfig(@Url url: String): PipedConfig
|
suspend fun getInstanceConfig(@Url url: String): PipedConfig
|
||||||
|
@ -2,9 +2,11 @@ package com.github.libretube.api
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
|
import com.github.libretube.api.RetrofitInstance.PIPED_API_URL
|
||||||
import com.github.libretube.api.obj.PipedInstance
|
import com.github.libretube.api.obj.PipedInstance
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||||
|
|
||||||
class InstanceRepository(private val context: Context) {
|
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) {
|
suspend fun getInstances(): Result<List<PipedInstance>> = withContext(Dispatchers.IO) {
|
||||||
runCatching {
|
runCatching {
|
||||||
RetrofitInstance.externalApi.getInstances(PIPED_INSTANCES_URL)
|
RetrofitInstance.externalApi.getInstances()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getInstancesFallback(): List<PipedInstance> {
|
suspend fun getInstancesFallback(): List<PipedInstance> = withContext(Dispatchers.IO) {
|
||||||
val instanceNames = context.resources.getStringArray(R.array.instances)
|
return@withContext try {
|
||||||
return context.resources.getStringArray(R.array.instancesValue)
|
RetrofitInstance.externalApi.getInstancesMarkdown().body()!!.string().lines().reversed()
|
||||||
.mapIndexed { index, instanceValue ->
|
.takeWhile { !it.startsWith("---") }
|
||||||
PipedInstance(instanceNames[index], instanceValue)
|
.filter { it.isNotBlank() }
|
||||||
}
|
.map { line ->
|
||||||
}
|
val infoParts = line.split("|")
|
||||||
|
|
||||||
companion object {
|
PipedInstance(name = infoParts[0], apiUrl = infoParts[1], locations = infoParts[2], cdn = infoParts[3] == "Yes")
|
||||||
private const val PIPED_INSTANCES_URL = "https://piped-instances.kavin.rocks"
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
// worst case scenario: only return official instance
|
||||||
|
return@withContext listOf(PipedInstance(name = PIPED_API_URL.toHttpUrl().host, apiUrl = PIPED_API_URL))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -112,7 +112,7 @@ class MainActivity : BaseActivity() {
|
|||||||
|
|
||||||
val isAppConfigured = PreferenceHelper.getBoolean(PreferenceKeys.LOCAL_FEED_EXTRACTION, false) ||
|
val isAppConfigured = PreferenceHelper.getBoolean(PreferenceKeys.LOCAL_FEED_EXTRACTION, false) ||
|
||||||
PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, "").isNotEmpty()
|
PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, "").isNotEmpty()
|
||||||
if (isAppConfigured) {
|
if (!isAppConfigured) {
|
||||||
val welcomeIntent = Intent(this, WelcomeActivity::class.java)
|
val welcomeIntent = Intent(this, WelcomeActivity::class.java)
|
||||||
startActivity(welcomeIntent)
|
startActivity(welcomeIntent)
|
||||||
finish()
|
finish()
|
||||||
|
@ -1,39 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<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">
|
<string-array name="languages">
|
||||||
<item>@string/systemLanguage</item>
|
<item>@string/systemLanguage</item>
|
||||||
<item>العربية</item>
|
<item>العربية</item>
|
||||||
|
@ -27,8 +27,6 @@
|
|||||||
<ListPreference
|
<ListPreference
|
||||||
android:icon="@drawable/ic_server"
|
android:icon="@drawable/ic_server"
|
||||||
app:defaultValue="https://pipedapi.kavin.rocks"
|
app:defaultValue="https://pipedapi.kavin.rocks"
|
||||||
app:entries="@array/instances"
|
|
||||||
app:entryValues="@array/instancesValue"
|
|
||||||
app:key="selectInstance"
|
app:key="selectInstance"
|
||||||
android:dependency="full_local_mode"
|
android:dependency="full_local_mode"
|
||||||
app:title="@string/instances" />
|
app:title="@string/instances" />
|
||||||
@ -70,8 +68,6 @@
|
|||||||
android:dependency="auth_instance_toggle"
|
android:dependency="auth_instance_toggle"
|
||||||
android:icon="@drawable/ic_server"
|
android:icon="@drawable/ic_server"
|
||||||
app:defaultValue="https://pipedapi.kavin.rocks"
|
app:defaultValue="https://pipedapi.kavin.rocks"
|
||||||
app:entries="@array/instances"
|
|
||||||
app:entryValues="@array/instancesValue"
|
|
||||||
app:key="selectAuthInstance"
|
app:key="selectAuthInstance"
|
||||||
app:title="@string/auth_instances" />
|
app:title="@string/auth_instances" />
|
||||||
|
|
||||||
|
@ -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
|
|
Loading…
x
Reference in New Issue
Block a user