mirror of
https://github.com/videolan/vlc-android
synced 2024-11-23 09:56:36 +08:00
OpenSubtitles: get user info and better login management
This commit is contained in:
parent
c49b081984
commit
3b589533e9
@ -24,6 +24,9 @@ interface IOpenSubtitleService {
|
||||
@POST("login")
|
||||
fun login( @Body loginBody: LoginBody): Call<OpenSubtitleAccount>
|
||||
|
||||
@GET("infos/user")
|
||||
fun userInfo(): Call<UserInfo>
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -170,6 +170,29 @@ data class OpenSubtitleAccount(
|
||||
val user: User?
|
||||
)
|
||||
|
||||
data class UserInfo(
|
||||
@field:Json(name = "data")
|
||||
val `data`: UserInfoData
|
||||
)
|
||||
|
||||
data class UserInfoData(
|
||||
@field:Json(name = "allowed_downloads")
|
||||
val allowedDownloads: Int,
|
||||
@field:Json(name = "downloads_count")
|
||||
val downloadsCount: Int,
|
||||
@field:Json(name = "ext_installed")
|
||||
val extInstalled: Boolean,
|
||||
@field:Json(name = "level")
|
||||
val level: String,
|
||||
@field:Json(name = "remaining_downloads")
|
||||
val remainingDownloads: Int,
|
||||
@field:Json(name = "user_id")
|
||||
val userId: Int,
|
||||
@field:Json(name = "vip")
|
||||
val vip: Boolean
|
||||
)
|
||||
|
||||
|
||||
data class User(
|
||||
@field:Json(name = "allowed_downloads")
|
||||
val allowedDownloads: Int?,
|
||||
|
@ -50,6 +50,10 @@ class OpenSubtitleRepository(private val openSubtitleService: IOpenSubtitleServi
|
||||
return openSubtitleService.login(LoginBody(username, password)).execute()
|
||||
}
|
||||
|
||||
fun userInfo(): Response<UserInfo> {
|
||||
return openSubtitleService.userInfo().execute()
|
||||
}
|
||||
|
||||
companion object {
|
||||
// To ensure the instance can be overridden in tests.
|
||||
var instance = lazy { OpenSubtitleRepository(OpenSubtitleClient.instance) }
|
||||
|
@ -5,6 +5,7 @@ import com.moczul.ok2curl.CurlInterceptor
|
||||
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.Interceptor
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
@ -50,17 +51,18 @@ private class UserAgentInterceptor(val userAgent: String): Interceptor {
|
||||
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val request: Request = chain.request()
|
||||
val userAgentRequest: Request = request.newBuilder()
|
||||
val requestBuilder = request.newBuilder()
|
||||
.header("User-Agent", userAgent)
|
||||
.header("Api-Key", BuildConfig.VLC_OPEN_SUBTITLES_API_KEY)
|
||||
.header("Accept", "application/json")
|
||||
.build()
|
||||
return chain.proceed(userAgentRequest)
|
||||
if (OpenSubtitleClient.authorizationToken.isNotEmpty())requestBuilder.header("Authorization", OpenSubtitleClient.authorizationToken)
|
||||
return chain.proceed(requestBuilder.build())
|
||||
}
|
||||
}
|
||||
|
||||
interface OpenSubtitleClient {
|
||||
companion object {
|
||||
val instance: IOpenSubtitleService by lazy { buildClient() }
|
||||
var authorizationToken:String = ""
|
||||
}
|
||||
}
|
||||
|
@ -27,8 +27,8 @@ package main.java.org.videolan.resources.opensubtitles
|
||||
import java.util.Date
|
||||
|
||||
data class OpenSubtitlesLimit (
|
||||
val requests: Int = 0,
|
||||
val max: Int = 5,
|
||||
var requests: Int = 0,
|
||||
var max: Int = 5,
|
||||
val resetTime: Date? = null
|
||||
) {
|
||||
private fun getRemaining(): Int {
|
||||
|
@ -28,6 +28,8 @@ import android.content.SharedPreferences
|
||||
import com.squareup.moshi.JsonAdapter
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter
|
||||
import org.videolan.resources.opensubtitles.IOpenSubtitleService
|
||||
import org.videolan.resources.opensubtitles.OpenSubtitleClient
|
||||
import org.videolan.tools.KEY_OPEN_SUBTITLES_LIMIT
|
||||
import org.videolan.tools.KEY_OPEN_SUBTITLES_USER
|
||||
import org.videolan.tools.putSingle
|
||||
@ -61,6 +63,7 @@ object OpenSubtitlesUtils {
|
||||
fun saveUser(settings: SharedPreferences, user: OpenSubtitlesUser) {
|
||||
val jsonAdapter = getUserAdapter()
|
||||
settings.putSingle(KEY_OPEN_SUBTITLES_USER, jsonAdapter.toJson(user))
|
||||
OpenSubtitleClient.authorizationToken = user.account?.token ?: ""
|
||||
}
|
||||
|
||||
private fun getUserAdapter(): JsonAdapter<OpenSubtitlesUser> {
|
||||
|
@ -25,8 +25,8 @@ import kotlinx.coroutines.channels.actor
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.withContext
|
||||
import main.java.org.videolan.resources.opensubtitles.OpenSubtitlesLimit
|
||||
import main.java.org.videolan.resources.opensubtitles.OpenSubtitlesUser
|
||||
import main.java.org.videolan.resources.opensubtitles.OpenSubtitlesUtils
|
||||
import org.videolan.resources.opensubtitles.OpenSubtitleClient
|
||||
import org.videolan.resources.opensubtitles.OpenSubtitleRepository
|
||||
import org.videolan.resources.util.parcelable
|
||||
import org.videolan.tools.Settings
|
||||
@ -124,8 +124,11 @@ 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
|
||||
if (!token.isNullOrEmpty()) OpenSubtitleClient.authorizationToken = token
|
||||
binding = SubtitleDownloaderDialogBinding.inflate(inflater, container, false)
|
||||
binding.viewmodel = viewModel
|
||||
if (!token.isNullOrEmpty()) viewModel.checkUserInfos(settings)
|
||||
|
||||
binding.subLogin.setOnClickListener {
|
||||
state = SubDownloadDialogState.Login
|
||||
@ -133,9 +136,7 @@ class SubtitleDownloaderDialogFragment : VLCBottomSheetDialogFragment() {
|
||||
|
||||
binding.loginButton.setOnClickListener {
|
||||
if (viewModel.observableUser.get()?.logged == true) {
|
||||
val user = OpenSubtitlesUser()
|
||||
OpenSubtitlesUtils.saveUser(settings, user)
|
||||
viewModel.observableUser.set(user)
|
||||
viewModel.logout(settings)
|
||||
}else {
|
||||
viewModel.login(
|
||||
settings,
|
||||
|
@ -284,6 +284,34 @@ class SubtitlesModel(private val context: Context, private val mediaUri: Uri, pr
|
||||
}
|
||||
}
|
||||
|
||||
fun checkUserInfos(settings: SharedPreferences) {
|
||||
viewModelScope.launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
val callInfo = OpenSubtitleRepository.getInstance().userInfo()
|
||||
if (callInfo.isSuccessful) {
|
||||
val userInfo = callInfo.body()
|
||||
if (userInfo != null) {
|
||||
val limit = OpenSubtitlesUtils.getLimit(settings)
|
||||
limit.max = userInfo.data.allowedDownloads
|
||||
limit.requests = userInfo.data.downloadsCount
|
||||
OpenSubtitlesUtils.saveLimit(settings, limit)
|
||||
observableLimit.set(limit)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun logout(settings: SharedPreferences) {
|
||||
val user = OpenSubtitlesUser()
|
||||
OpenSubtitlesUtils.saveUser(settings, user)
|
||||
observableUser.set(user)
|
||||
val limit = OpenSubtitlesLimit()
|
||||
OpenSubtitlesUtils.saveLimit(settings, limit)
|
||||
observableLimit.set(limit)
|
||||
}
|
||||
|
||||
private fun migrateFromOld(it: String?): String? {
|
||||
return oldLanguagesMigration[it]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user