fetch servers from github

This commit is contained in:
rimthekid 2022-02-10 15:32:22 +04:00
parent dfab644ae2
commit 641dd5f290
4 changed files with 78 additions and 7 deletions

View File

@ -54,4 +54,5 @@ dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-jackson:2.9.0'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.1'
implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'
}

View File

@ -52,5 +52,7 @@ interface PipedApi {
@POST("unsubscribe")
suspend fun unsubscribe(@Header("Authorization") token: String, @Body subscribe: Subscribe): String
@GET("Instances.md")
suspend fun getInstances(): String
}

View File

@ -2,18 +2,25 @@ package com.github.libretube
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
import okhttp3.HttpUrl
import androidx.preference.PreferenceManager
import com.github.libretube.adapters.TrendingAdapter
import retrofit2.HttpException
import retrofit2.Retrofit
import retrofit2.converter.jackson.JacksonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.io.IOException
class Settings : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.settings, rootKey)
val instance = findPreference<ListPreference>("instance")
fetchInstance()
instance?.setOnPreferenceChangeListener { preference, newValue ->
RetrofitInstance.url = newValue.toString()
RetrofitInstance.lazyMgr.reset()
@ -27,4 +34,59 @@ class Settings : PreferenceFragmentCompat() {
}
}
private fun fetchInstance() {
val api: PipedApi by lazy{
Retrofit.Builder()
.baseUrl("https://raw.githubusercontent.com/wiki/TeamPiped/Piped-Frontend/")
.addConverterFactory(ScalarsConverterFactory.create())
.build()
.create(PipedApi::class.java)
}
lifecycleScope.launchWhenCreated {
val response = try {
api.getInstances()
} catch (e: IOException) {
println(e)
Log.e("settings", "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
Log.e("settings", "HttpException, unexpected response ${e.toString()}")
return@launchWhenCreated
} catch (e: Exception){
Log.e("settings",e.toString())
return@launchWhenCreated
}
//println("dafaq $response")
val listEntries: MutableList<String> = ArrayList()
val listEntryValues: MutableList<String> = ArrayList()
var skipped = 0
val lines = response.split("\n")
for(line in lines) {
val split = line.split("|")
if (split.size == 5) {
if (skipped < 2) {
skipped++
}else{
println("dafaq $line")
listEntries.add(split[0])
listEntryValues.add(split[1])
}
}
}
val entries = listEntries.toTypedArray<CharSequence>()
val entryValues = listEntryValues.toTypedArray<CharSequence>()
runOnUiThread {
val instance = findPreference<ListPreference>("instance")
instance?.entries = entries
instance?.entryValues = entryValues
}
}
}
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
}

View File

@ -1,12 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="instances">
<item>piped.tokhmi.xyz</item>
<item>piped.kavin.rocks</item>
<item>kavin.rocks (Official)</item>
<item>silkky.cloud</item>
<item>tokhmi.xyz</item>
<item>moomoo.me</item>
<item>mint.lgbt</item>
</string-array>
<string-array name="instancesValue">
<item>https://pipedapi.tokhmi.xyz/</item>
<item>https://pipedapi.kavin.rocks/</item>
<item>https://api.piped.silkky.cloud</item>
<item>https://pipedapi.tokhmi.xyz/</item>
<item>https://pipedapi.moomoo.me</item>
<item>https://pa.mint.lgbt</item>
</string-array>
<string-array name="regions">
<item>Afghanistan</item>