[Android] OpenSubtitles: hotswap the domain url depending on the user info

Nicolas Pomepuy git at videolan.org
Thu Nov 21 13:19:11 UTC 2024


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Nov  5 07:23:35 2024 +0100| [7f548c2bc4080e56caa1d5421a45f3087f97efa4] | committer: Duncan McNamara

OpenSubtitles: hotswap the domain url depending on the user info

> https://code.videolan.org/videolan/vlc-android/commit/7f548c2bc4080e56caa1d5421a45f3087f97efa4
---

 .../resources/opensubtitles/OpenSubtitleService.kt | 54 ++++++++++++++++------
 .../resources/opensubtitles/OpenSubtitlesUtils.kt  |  1 +
 .../dialogs/SubtitleDownloaderDialogFragment.kt    |  6 ++-
 3 files changed, 45 insertions(+), 16 deletions(-)

diff --git a/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitleService.kt b/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitleService.kt
index 331838c601..bd56f6639c 100644
--- a/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitleService.kt
+++ b/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitleService.kt
@@ -6,6 +6,7 @@ import com.moczul.ok2curl.logger.Logger
 import com.squareup.moshi.Moshi
 import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter
 import main.java.org.videolan.resources.opensubtitles.OpenSubtitlesUtils
+import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
 import okhttp3.Interceptor
 import okhttp3.OkHttpClient
 import okhttp3.Request
@@ -24,28 +25,33 @@ private const val BASE_URL = "https://api.opensubtitles.com/api/v1/"
 const val USER_AGENT = "VLSub v0.9"
 
 private fun buildClient() = Retrofit.Builder()
-        .baseUrl(BASE_URL)
-        .client(OkHttpClient.Builder()
-                .addInterceptor(HttpLoggingInterceptor().apply {
-                    level = HttpLoggingInterceptor.Level.BODY
-                })
-                .addInterceptor(UserAgentInterceptor(USER_AGENT))
-                .addInterceptor(ConnectivityInterceptor(AppContextProvider.appContext))
+    .baseUrl(BASE_URL)
+    .client(
+        OkHttpClient.Builder()
+            .addInterceptor(HttpLoggingInterceptor().apply {
+                level = HttpLoggingInterceptor.Level.BODY
+            })
+            .addInterceptor(DomainInterceptor())
+            .addInterceptor(UserAgentInterceptor(USER_AGENT))
+            .addInterceptor(ConnectivityInterceptor(AppContextProvider.appContext))
             .addInterceptor(CurlInterceptor(object : Logger {
                 override fun log(message: String) {
                     Log.v("Ok2Curl", message)
                 }
             }))
-                .readTimeout(10, TimeUnit.SECONDS)
-                .connectTimeout(5, TimeUnit.SECONDS)
-                .build())
-        .addConverterFactory(MoshiConverterFactory.create(
+            .readTimeout(10, TimeUnit.SECONDS)
+            .connectTimeout(5, TimeUnit.SECONDS)
+            .build()
+    )
+    .addConverterFactory(
+        MoshiConverterFactory.create(
             Moshi.Builder()
                 .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
                 .build()
-        ))
-        .build()
-        .create(IOpenSubtitleService::class.java)
+        )
+    )
+    .build()
+    .create(IOpenSubtitleService::class.java)
 
 private class UserAgentInterceptor(val userAgent: String): Interceptor {
 
@@ -60,9 +66,29 @@ private class UserAgentInterceptor(val userAgent: String): Interceptor {
     }
 }
 
+class DomainInterceptor : Interceptor {
+
+    @Throws(Exception::class)
+    override fun intercept(chain: Interceptor.Chain): Response {
+        val request = chain.request()
+        val newBuilder = request.newBuilder()
+        OpenSubtitleClient.userDomain?.let {
+            newBuilder.url(
+                    request.url.toString()
+                        .replace("api.opensubtitles.com", it)
+                        .toHttpUrlOrNull() ?: request.url
+                )
+        }
+        return chain.proceed(
+            newBuilder.build()
+        )
+    }
+}
+
 interface OpenSubtitleClient {
     companion object {
         val instance: IOpenSubtitleService by lazy { buildClient() }
         var authorizationToken:String = ""
+        var userDomain:String? = null
     }
 }
diff --git a/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitlesUtils.kt b/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitlesUtils.kt
index 48e1cbd1d1..ea6f55b200 100644
--- a/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitlesUtils.kt
+++ b/application/resources/src/main/java/org/videolan/resources/opensubtitles/OpenSubtitlesUtils.kt
@@ -64,6 +64,7 @@ object OpenSubtitlesUtils {
         val jsonAdapter = getUserAdapter()
         settings.putSingle(KEY_OPEN_SUBTITLES_USER, jsonAdapter.toJson(user))
         OpenSubtitleClient.authorizationToken = user.account?.token ?: ""
+        OpenSubtitleClient.userDomain = user.account?.baseUrl
     }
 
     private fun getUserAdapter(): JsonAdapter<OpenSubtitlesUser> {
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SubtitleDownloaderDialogFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SubtitleDownloaderDialogFragment.kt
index a2f1707019..80d21342df 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SubtitleDownloaderDialogFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SubtitleDownloaderDialogFragment.kt
@@ -124,8 +124,10 @@ class SubtitleDownloaderDialogFragment : VLCBottomSheetDialogFragment() {
     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
         super.onCreateView(inflater, container, savedInstanceState)
         settings = Settings.getInstance(requireContext())
-        val token = OpenSubtitlesUtils.getUser(settings).account?.token
+        val user = OpenSubtitlesUtils.getUser(settings)
+        val token = user.account?.token
         if (!token.isNullOrEmpty()) OpenSubtitleClient.authorizationToken = token
+        OpenSubtitleClient.userDomain = user.account?.baseUrl
         binding = SubtitleDownloaderDialogBinding.inflate(inflater, container, false)
         binding.viewmodel = viewModel
        if (!token.isNullOrEmpty()) viewModel.checkUserInfos(settings)
@@ -190,7 +192,7 @@ class SubtitleDownloaderDialogFragment : VLCBottomSheetDialogFragment() {
         })
         //todo
         viewModel.observableSearchHearingImpaired.set(false)
-        viewModel.observableUser.set(OpenSubtitlesUtils.getUser(settings))
+        viewModel.observableUser.set(user)
         viewModel.observableLimit.set(OpenSubtitlesUtils.getLimit(settings))
 
         binding.retryButton.setOnClickListener {



More information about the Android mailing list