Przeglądaj źródła

album art downloader cover art improvement

Milan Jurkulak 3 lat temu
rodzic
commit
62da0da697

+ 7 - 0
tvlib/src/main/kotlin/org/mjdev/tvlib/extensions/ComposeExt.kt

@@ -34,6 +34,7 @@ import coil.decode.GifDecoder
 import coil.decode.ImageDecoderDecoder
 import coil.decode.SvgDecoder
 import coil.decode.VideoFrameDecoder
+import coil.disk.DiskCache
 import coil.request.CachePolicy
 import org.mjdev.tvlib.extensions.ContextExt.isATv
 import org.mjdev.tvlib.helpers.coil.AlbumArtDecoder
@@ -144,6 +145,12 @@ object ComposeExt {
                 .crossfade(false)
                 .diskCachePolicy(CachePolicy.ENABLED)
                 .memoryCachePolicy(CachePolicy.ENABLED)
+                .diskCache {
+                    DiskCache.Builder()
+                        .directory(context.cacheDir.resolve("image_cache"))
+                        .maxSizePercent(0.7)
+                        .build()
+                }
                 .respectCacheHeaders(true)
                 .networkObserverEnabled(true)
                 .components {

+ 20 - 5
tvlib/src/main/kotlin/org/mjdev/tvlib/helpers/coil/AlbumArtDecoder.kt

@@ -29,12 +29,20 @@ import timber.log.Timber
 import java.io.BufferedInputStream
 import java.io.InputStream
 
+@Suppress("PrivatePropertyName")
 class AlbumArtDecoder(
     private val source: ImageSource,
     private val options: Options
 ) : Decoder {
 
-    private val httpClient: OkHttpClient by lazy { OkHttpClient() }
+    private val USER_AGENT =
+        "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0"
+
+    private val httpClient: OkHttpClient by lazy {
+        OkHttpClient.Builder().apply {
+            addNetworkInterceptor(UserAgentInterceptor(USER_AGENT))
+        }.build()
+    }
 
     private fun call(url: String) = httpClient.newCall(
         Request.Builder()
@@ -44,12 +52,17 @@ class AlbumArtDecoder(
 
     override suspend fun decode() = MediaMetadataRetriever().use { retriever ->
         retriever.setDataSource(source)
-        var rawData = retriever.embeddedPicture
+        var rawData = try {
+            retriever.embeddedPicture
+        } catch (e: Exception) {
+            Timber.e(e)
+            null
+        }
         if (rawData == null) {
             var albumName = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM)
             val artistName = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST)
             if (albumName == null && artistName == null) {
-                albumName = source.file().name // todo better logic
+                albumName = source.file().name.substringBeforeLast(".")
             }
             rawData = downloadAlbumArt(albumName, artistName)
         }
@@ -65,8 +78,10 @@ class AlbumArtDecoder(
 
     private fun downloadAlbumArt(albumName: String?, artistName: String?): ByteArray? {
         var releaseGroupID: String? = null
-        var musicSearchUrl = "http://www.musicbrainz.org/ws/2/" +
-                "release-group?query=$albumName AND artist:$artistName"
+        var musicSearchUrl = "http://www.musicbrainz.org/ws/2/release-group?query=$albumName"
+        if (artistName != null) {
+            musicSearchUrl += " AND artist:$artistName"
+        }
         musicSearchUrl = musicSearchUrl.replace(" ".toRegex(), "%20")
         val musicSearchIs = retrieveXMLFromURL(musicSearchUrl)
         if (musicSearchIs != null) {

+ 24 - 0
tvlib/src/main/kotlin/org/mjdev/tvlib/helpers/coil/UserAgentInterceptor.kt

@@ -0,0 +1,24 @@
+/*
+ *  Copyright (c) Milan Jurkulák 2023. 
+ *  Contact:
+ *  e: mimoccc@gmail.com
+ *  e: mj@mjdev.org
+ *  w: https://mjdev.org
+ */
+package org.mjdev.tvlib.helpers.coil
+
+import okhttp3.Interceptor
+import okhttp3.Request
+import okhttp3.Response
+import java.io.IOException
+
+class UserAgentInterceptor(private val userAgent: String) : Interceptor {
+    @Throws(IOException::class)
+    override fun intercept(chain: Interceptor.Chain): Response {
+        val originalRequest: Request = chain.request()
+        val requestWithUserAgent = originalRequest.newBuilder()
+            .header("User-Agent", userAgent)
+            .build()
+        return chain.proceed(requestWithUserAgent)
+    }
+}