From cf1fc3ce3e6a8e563bcfc8f843992375b3b93cd1 Mon Sep 17 00:00:00 2001 From: LagradOst <11805592+LagradOst@users.noreply.github.com> Date: Sat, 2 Apr 2022 19:58:56 +0200 Subject: [PATCH] added sync to more sites --- .../cloudstream3/ui/result/ResultFragment.kt | 17 ++++---- .../cloudstream3/ui/result/SyncViewModel.kt | 24 +++++++++-- .../lagradost/cloudstream3/utils/SyncUtil.kt | 43 ++++++++++++++++++- 3 files changed, 69 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragment.kt index 244af24a..4cef7d33 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragment.kt @@ -518,13 +518,13 @@ class ResultFragment : Fragment(), PanelsChildGestureRegionObserver.GestureRegio setFormatText(result_meta_rating, R.string.rating_format, rating?.div(1000f)) } - private fun setMalSync(id: String?): Boolean { - syncModel.setMalId(id ?: return false) + private fun setMalSync(id: Int?): Boolean { + syncModel.setMalId(id?.toString() ?: return false) return true } - private fun setAniListSync(id: String?): Boolean { - syncModel.setAniListId(id ?: return false) + private fun setAniListSync(id: Int?): Boolean { + syncModel.setAniListId(id?.toString() ?: return false) return true } @@ -637,6 +637,7 @@ class ResultFragment : Fragment(), PanelsChildGestureRegionObserver.GestureRegio startAction = arguments?.getInt("startAction") ?: START_ACTION_NORMAL startValue = arguments?.getInt("startValue") ?: START_VALUE_NORMAL + syncModel.addFromUrl(url) val api = getApiFromName(apiName) if (media_route_button != null) { @@ -1037,7 +1038,6 @@ class ResultFragment : Fragment(), PanelsChildGestureRegionObserver.GestureRegio } } - val adapter: RecyclerView.Adapter = EpisodeAdapter( ArrayList(), @@ -1523,12 +1523,11 @@ class ResultFragment : Fragment(), PanelsChildGestureRegionObserver.GestureRegio if (SettingsFragment.accountEnabled) if (d is AnimeLoadResponse) { if ( - setMalSync(d.malId?.toString()) + setMalSync(d.malId) || - setAniListSync(d.anilistId?.toString()) + setAniListSync(d.anilistId) ) { - syncModel.updateMetadata() - syncModel.updateUserData() + syncModel.updateMetaAndUser() } } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/result/SyncViewModel.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/result/SyncViewModel.kt index c2e13cf1..885c8a6e 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/result/SyncViewModel.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/result/SyncViewModel.kt @@ -9,6 +9,7 @@ import com.lagradost.cloudstream3.syncproviders.OAuth2API.Companion.SyncApis import com.lagradost.cloudstream3.syncproviders.OAuth2API.Companion.aniListApi import com.lagradost.cloudstream3.syncproviders.OAuth2API.Companion.malApi import com.lagradost.cloudstream3.syncproviders.SyncAPI +import com.lagradost.cloudstream3.utils.SyncUtil import kotlinx.coroutines.launch @@ -28,12 +29,22 @@ class SyncViewModel : ViewModel() { // prefix, id private val syncIds = hashMapOf() - fun setMalId(id: String) { - syncIds[malApi.idPrefix] = id + fun setMalId(id: String?) { + syncIds[malApi.idPrefix] = id ?: return } - fun setAniListId(id: String) { - syncIds[aniListApi.idPrefix] = id + fun setAniListId(id: String?) { + syncIds[aniListApi.idPrefix] = id ?: return + } + + fun addFromUrl(url : String?) = viewModelScope.launch { + SyncUtil.getIdsFromUrl(url)?.let { (malId, aniListId) -> + setMalId(malId) + setAniListId(aniListId) + if(malId != null || aniListId != null) { + updateMetaAndUser() + } + } } fun setEpisodesDelta(delta: Int) { @@ -124,4 +135,9 @@ class SyncViewModel : ViewModel() { _metaResponse.postValue(lastError) setEpisodesDelta(0) } + + fun updateMetaAndUser() { + updateMetadata() + updateUserData() + } } \ No newline at end of file diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/SyncUtil.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/SyncUtil.kt index 2e5a0340..a2278853 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/SyncUtil.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/SyncUtil.kt @@ -8,9 +8,49 @@ import com.lagradost.cloudstream3.mvvm.logError import java.util.concurrent.TimeUnit object SyncUtil { + private val regexs = listOf( + Regex("""(9anime)\.(?:to|center|id)/watch/(?:.*?)\.([^/?]*)"""), + Regex("""(gogoanime|gogoanimes)\..*?/category/([^/?]*)"""), + Regex("""(twist\.moe)/a/([^/?]*)"""), + ) + + private const val GOGOANIME = "Gogoanime" + private const val NINE_ANIME = "9anime" + private const val TWIST_MOE = "Twistmoe" + + private val matchList = + mapOf( + "9anime" to NINE_ANIME, + "gogoanime" to GOGOANIME, + "gogoanimes" to GOGOANIME, + "twist.moe" to TWIST_MOE + ) + + suspend fun getIdsFromUrl(url: String?): Pair? { + if (url == null) return null + + for (regex in regexs) { + regex.find(url)?.let { match -> + if (match.groupValues.size == 3) { + val site = match.groupValues[1] + val slug = match.groupValues[2] + matchList[site]?.let { realSite -> + getIdsFromSlug(slug, realSite)?.let { + return it + } + } + } + } + } + return null + } + /** first. Mal, second. Anilist, * valid sites are: Gogoanime, Twistmoe and 9anime*/ - suspend fun getIdsFromSlug(slug: String, site : String = "Gogoanime"): Pair? { + private suspend fun getIdsFromSlug( + slug: String, + site: String = "GogoanimeGogoanime" + ): Pair? { try { //Gogoanime, Twistmoe and 9anime val url = @@ -81,5 +121,4 @@ object SyncUtil { @JsonProperty("updatedAt") val updatedAt: String?, @JsonProperty("deletedAt") val deletedAt: String? ) - } \ No newline at end of file