Skip to content
Snippets Groups Projects
Commit 677e4eab authored by Geoffrey Métais's avatar Geoffrey Métais Committed by Geoffrey Métais
Browse files

Use app context for media fetching

parent 60ed93bf
No related branches found
No related tags found
1 merge request!288Moviepedia API implementation
Pipeline #11390 passed with stage
in 25 minutes and 40 seconds
......@@ -382,7 +382,7 @@ class MediaParsingService : LifecycleService(), DevicesDiscoveryCb, LifecycleOwn
lastNotificationTime = 0L
//todo reenable entry point when ready
if (BuildConfig.DEBUG) try {
MoviepediaIndexer.indexMedialib(this@MediaParsingService)
MoviepediaIndexer.indexMedialib(applicationContext)
} catch (e: Exception) {
if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "${e.cause}")
}
......
......@@ -27,7 +27,10 @@ package org.videolan.vlc.moviepedia
import android.content.Context
import android.net.Uri
import android.util.Log
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import org.videolan.medialibrary.interfaces.AbstractMedialibrary
import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
import org.videolan.vlc.BuildConfig
......@@ -44,40 +47,38 @@ import org.videolan.vlc.util.getLocaleLanguages
object MoviepediaIndexer : CoroutineScope by MainScope() {
fun indexMedialib(context: Context) {
launch {
withContext(Dispatchers.IO) {
val medias = context.getFromMl { getPagedVideos(AbstractMedialibrary.SORT_DEFAULT, false, 1000, 0) }
val filesToIndex = HashMap<Long, Uri>()
medias.forEach {
if (it.getMetaLong(AbstractMediaWrapper.META_METADATA_RETRIEVED) != 1L)
filesToIndex[it.id] = it.uri
}
if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "Retrieving infos for ${filesToIndex.size} files")
for (filesToIndex in filesToIndex) {
if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "Retrieving infos for: ${filesToIndex.value.lastPathSegment}")
}
val repo = MoviepediaApiRepository.getInstance()
val results = try {
repo.searchMediaBatch(filesToIndex)
} catch (e: Exception) {
return@withContext
}
medias.forEach { media ->
media?.setLongMeta(AbstractMediaWrapper.META_METADATA_RETRIEVED, 1L)
}
results.forEach { result ->
result.lucky?.let {
val media = medias.find { it.id == result.id.toLong() }
try {
saveMediaMetadata(context, media, it, retrieveCast = false, removePersonOrphans = false)
} catch (e: Exception) {
media?.setLongMeta(251, 0L)
}
launch(Dispatchers.IO) {
val medias = context.getFromMl { getPagedVideos(AbstractMedialibrary.SORT_DEFAULT, false, 1000, 0) }
val filesToIndex = HashMap<Long, Uri>()
medias.forEach {
if (it.getMetaLong(AbstractMediaWrapper.META_METADATA_RETRIEVED) != 1L)
filesToIndex[it.id] = it.uri
}
if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "Retrieving infos for ${filesToIndex.size} files")
for (filesToIndex in filesToIndex) {
if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "Retrieving infos for: ${filesToIndex.value.lastPathSegment}")
}
val repo = MoviepediaApiRepository.getInstance()
val results = try {
repo.searchMediaBatch(filesToIndex)
} catch (e: Exception) {
return@launch
}
medias.forEach { media ->
media?.setLongMeta(AbstractMediaWrapper.META_METADATA_RETRIEVED, 1L)
}
results.forEach { result ->
result.lucky?.let {
val media = medias.find { it.id == result.id.toLong() }
try {
saveMediaMetadata(context, media, it, retrieveCast = false, removePersonOrphans = false)
} catch (e: Exception) {
media?.setLongMeta(251, 0L)
}
}
removePersonOrphans(context)
}
removePersonOrphans(context)
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment