From cc7fc98720e70910dcf14fcc45c11acc4c2c2c33 Mon Sep 17 00:00:00 2001 From: shabinder Date: Fri, 14 May 2021 04:06:07 +0530 Subject: [PATCH] Code Cleanup --- .../AndroidPlatformActions.kt | 4 +- .../com/shabinder/common/di/AudioTagging.kt | 61 ++----------------- .../kotlin/com/shabinder/common/di/DI.kt | 1 + .../common/di/providers/GaanaProvider.kt | 9 +-- .../common/di/providers/SpotifyProvider.kt | 12 ++-- .../common/di/providers/YoutubeProvider.kt | 20 +++--- 6 files changed, 29 insertions(+), 78 deletions(-) diff --git a/common/data-models/src/androidMain/kotlin/com.shabinder.common.models/AndroidPlatformActions.kt b/common/data-models/src/androidMain/kotlin/com.shabinder.common.models/AndroidPlatformActions.kt index 46732ac7..ffd466e5 100644 --- a/common/data-models/src/androidMain/kotlin/com.shabinder.common.models/AndroidPlatformActions.kt +++ b/common/data-models/src/androidMain/kotlin/com.shabinder.common.models/AndroidPlatformActions.kt @@ -8,7 +8,7 @@ actual interface PlatformActions { const val SharedPreferencesKey = "configurations" } - val imageCacheDir: java.io.File + val imageCacheDir: String val sharedPreferences: SharedPreferences? @@ -18,7 +18,7 @@ actual interface PlatformActions { } actual val StubPlatformActions = object: PlatformActions { - override val imageCacheDir = java.io.File("/") + override val imageCacheDir = "" override val sharedPreferences: SharedPreferences? = null diff --git a/common/dependency-injection/src/androidMain/kotlin/com/shabinder/common/di/AudioTagging.kt b/common/dependency-injection/src/androidMain/kotlin/com/shabinder/common/di/AudioTagging.kt index 5bb3fbe3..4df5a3ce 100644 --- a/common/dependency-injection/src/androidMain/kotlin/com/shabinder/common/di/AudioTagging.kt +++ b/common/dependency-injection/src/androidMain/kotlin/com/shabinder/common/di/AudioTagging.kt @@ -26,59 +26,6 @@ import com.shabinder.common.models.DownloadResult import kotlinx.coroutines.flow.collect import java.io.FileInputStream - -/* -suspend fun MP3File.setAudioTags(track: TrackDetails) { - - val id3v1Tag = this.iD3v1Tag ?: ID3v1Tag() - id3v1Tag.apply { - setField(FieldKey.ALBUM,track.albumName) - setField(FieldKey.ARTIST,track.artists.getOrNull(0) ?: "") - setField(FieldKey.ARTIST,track.artists.getOrNull(0) ?: "") - setField(FieldKey.TITLE,track.title) - setField(FieldKey.YEAR,track.year) - setField(FieldKey.COMMENT,track.comment) - } - - val id3v2Tag = this.iD3v2TagAsv24 ?: ID3v24Tag() - id3v2Tag.apply { - setField(FieldKey.ALBUM,track.albumName) - setField(FieldKey.ARTISTS,track.artists.joinToString(",")) - setField(FieldKey.ARTIST,track.artists.getOrNull(0) ?: "") - setField(FieldKey.ARTIST,track.artists.getOrNull(0) ?: "") - setField(FieldKey.TITLE,track.title) - setField(FieldKey.YEAR,track.year) - setField(FieldKey.COMMENT,track.comment) - setField(FieldKey.LYRICS,"Gonna Implement Soon") - setField(FieldKey.URL_OFFICIAL_RELEASE_SITE,track.trackUrl) - - try { - val artwork = ArtworkFactory.createArtworkFromFile(File(track.albumArtPath)) - createField(artwork) - setField(artwork) - } catch (e: java.io.FileNotFoundException) { - try { - // Image Still Not Downloaded! - // Lets Download Now and Write it into Album Art - downloadByteArray(track.albumArtURL)?.let { - setField(createArtworkField(it,"image/jpeg")) - } - } catch (e: Exception) { - e.printStackTrace() - // log("Error", "Couldn't Write Mp3 Album Art, error: ${e.stackTrace}") - } - } catch (e:Exception) { e.printStackTrace() } - } - - // Write Tags to file - this.iD3v1Tag = id3v1Tag - this.iD3v2Tag = id3v2Tag - - commit() -} -*/ - - fun Mp3File.removeAllTags(): Mp3File { removeId3v1Tag() removeId3v2Tag() @@ -99,8 +46,9 @@ fun Mp3File.setId3v1Tags(track: TrackDetails): Mp3File { } @Suppress("BlockingMethodInNonBlockingContext") -suspend fun Mp3File.setId3v2TagsAndSaveFile(track: TrackDetails,tempMp3Path:String) { +suspend fun Mp3File.setId3v2TagsAndSaveFile(track: TrackDetails) { val id3v2Tag = ID3v24Tag().apply { + artist = track.artists.joinToString(", ") title = track.title album = track.albumName @@ -117,7 +65,7 @@ suspend fun Mp3File.setId3v2TagsAndSaveFile(track: TrackDetails,tempMp3Path:Stri fis.close() id3v2Tag.setAlbumImage(bytesArray, "image/jpeg") this.id3v2Tag = id3v2Tag - saveFile(tempMp3Path) + saveFile(track.outputFilePath) } catch (e: java.io.FileNotFoundException) { Log.e("Error", "Couldn't Write Cached Mp3 Album Art, Downloading And Trying Again, error: ${e.message}") try { @@ -129,7 +77,7 @@ suspend fun Mp3File.setId3v2TagsAndSaveFile(track: TrackDetails,tempMp3Path:Stri is DownloadResult.Success -> { id3v2Tag.setAlbumImage(it.byteArray, "image/jpeg") this.id3v2Tag = id3v2Tag - saveFile(tempMp3Path) + saveFile(track.outputFilePath) } is DownloadResult.Progress -> {} // Nothing for Now , no progress bar to show } @@ -142,7 +90,6 @@ suspend fun Mp3File.setId3v2TagsAndSaveFile(track: TrackDetails,tempMp3Path:Stri } fun Mp3File.saveFile(filePath: String) { - Log.d("Mp3 File Save",filePath) save(filePath.substringBeforeLast('.') + ".new.mp3") val m4aFile = File(filePath) m4aFile.delete() diff --git a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/DI.kt b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/DI.kt index 75974e63..ea18a196 100644 --- a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/DI.kt +++ b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/DI.kt @@ -24,6 +24,7 @@ import com.shabinder.common.di.providers.GaanaProvider import com.shabinder.common.di.providers.SpotifyProvider import com.shabinder.common.di.providers.YoutubeMp3 import com.shabinder.common.di.providers.YoutubeMusic +import com.shabinder.common.di.providers.YoutubeProvider import io.ktor.client.* import io.ktor.client.features.json.* import io.ktor.client.features.json.serializer.* diff --git a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/GaanaProvider.kt b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/GaanaProvider.kt index fbebe579..69959a54 100644 --- a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/GaanaProvider.kt +++ b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/GaanaProvider.kt @@ -18,8 +18,8 @@ package com.shabinder.common.di.providers import co.touchlab.kermit.Kermit import com.shabinder.common.di.Dir +import com.shabinder.common.di.finalOutputDir import com.shabinder.common.di.gaana.GaanaRequests -import com.shabinder.common.di.getNameURL import com.shabinder.common.models.DownloadStatus import com.shabinder.common.models.PlatformQueryResult import com.shabinder.common.models.TrackDetails @@ -136,7 +136,7 @@ class GaanaProvider( title = it.track_title, artists = it.artist.map { artist -> artist?.name.toString() }, durationSec = it.duration, - albumArtPath = dir.imageCachePath + getNameURL(it.artworkLink), + albumArtPath = dir.imageCacheDir() + (it.artworkLink.substringBeforeLast('/').substringAfterLast('/')) + ".jpeg", albumName = it.album_title, year = it.release_date, comment = "Genres:${it.genre?.map { genre -> genre?.name }?.reduceOrNull { acc, s -> acc + s }}", @@ -144,15 +144,16 @@ class GaanaProvider( downloaded = it.downloaded ?: DownloadStatus.NotDownloaded, source = Source.Gaana, albumArtURL = it.artworkLink.replace("http:","https:"), - outputFilePath = dir.finalOutputPath(it.track_title, type, subFolder /*,".m4a"*/) + outputFilePath = dir.finalOutputDir(it.track_title, type, subFolder, dir.defaultDir()/*,".m4a"*/) ) } private fun GaanaTrack.updateStatusIfPresent(folderType: String, subFolder: String) { if (dir.isPresent( - dir.finalOutputFile( + dir.finalOutputDir( track_title, folderType, subFolder, + dir.defaultDir() ) ) ) { // Download Already Present!! diff --git a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/SpotifyProvider.kt b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/SpotifyProvider.kt index ad029703..1d97bae7 100644 --- a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/SpotifyProvider.kt +++ b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/SpotifyProvider.kt @@ -20,7 +20,7 @@ import co.touchlab.kermit.Kermit import com.shabinder.common.di.Dir import com.shabinder.common.di.TokenStore import com.shabinder.common.di.createHttpClient -import com.shabinder.common.di.getNameURL +import com.shabinder.common.di.finalOutputDir import com.shabinder.common.di.spotify.SpotifyRequests import com.shabinder.common.di.spotify.authenticateSpotify import com.shabinder.common.models.NativeAtomicReference @@ -216,28 +216,28 @@ class SpotifyProvider( } private fun List.toTrackDetailsList(type: String, subFolder: String) = this.map { - val albumArtLink = it.album?.images?.firstOrNull()?.url.toString() TrackDetails( title = it.name.toString(), artists = it.artists?.map { artist -> artist?.name.toString() } ?: listOf(), durationSec = (it.duration_ms / 1000).toInt(), - albumArtPath = dir.imageCachePath + getNameURL(albumArtLink), + albumArtPath = dir.imageCacheDir() + (it.album?.images?.firstOrNull()?.url.toString()).substringAfterLast('/') + ".jpeg", albumName = it.album?.name, year = it.album?.release_date, comment = "Genres:${it.album?.genres?.joinToString()}", trackUrl = it.href, downloaded = it.downloaded, source = Source.Spotify, - albumArtURL = albumArtLink, - outputFilePath = dir.finalOutputPath(it.name.toString(), type, subFolder/*,".m4a"*/) + albumArtURL = it.album?.images?.firstOrNull()?.url.toString(), + outputFilePath = dir.finalOutputDir(it.name.toString(), type, subFolder, dir.defaultDir()/*,".m4a"*/) ) } private fun Track.updateStatusIfPresent(folderType: String, subFolder: String) { if (dir.isPresent( - dir.finalOutputFile( + dir.finalOutputDir( name.toString(), folderType, subFolder, + dir.defaultDir() ) ) ) { // Download Already Present!! diff --git a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/YoutubeProvider.kt b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/YoutubeProvider.kt index 9d8af706..a02cd22d 100644 --- a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/YoutubeProvider.kt +++ b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/YoutubeProvider.kt @@ -18,7 +18,7 @@ package com.shabinder.common.di.providers import co.touchlab.kermit.Kermit import com.shabinder.common.di.Dir -import com.shabinder.common.di.getNameURL +import com.shabinder.common.di.finalOutputDir import com.shabinder.common.di.utils.removeIllegalChars import com.shabinder.common.models.DownloadStatus import com.shabinder.common.models.PlatformQueryResult @@ -108,14 +108,15 @@ class YoutubeProvider( title = it.title ?: "N/A", artists = listOf(it.author ?: "N/A"), durationSec = it.lengthSeconds, - albumArtPath = dir.imageCachePath + getNameURL(coverUrl), + albumArtPath = dir.imageCacheDir() + it.videoId + ".jpeg", source = Source.YouTube, - albumArtURL = coverUrl, + albumArtURL = "https://i.ytimg.com/vi/${it.videoId}/hqdefault.jpg", downloaded = if (dir.isPresent( - dir.finalOutputFile( + dir.finalOutputDir( itemName = it.title ?: "N/A", type = folderType, subFolder = subFolder, + dir.defaultDir() ) ) ) @@ -123,7 +124,7 @@ class YoutubeProvider( else { DownloadStatus.NotDownloaded }, - outputFilePath = dir.finalOutputPath(it.title ?: "N/A", folderType, subFolder/*,".m4a"*/), + outputFilePath = dir.finalOutputDir(it.title ?: "N/A", folderType, subFolder, dir.defaultDir()/*,".m4a"*/), videoID = it.videoId ) } @@ -161,14 +162,15 @@ class YoutubeProvider( title = name, artists = listOf(detail.author ?: "N/A"), durationSec = detail.lengthSeconds, - albumArtPath = dir.imageCachePath + getNameURL(coverUrl), + albumArtPath = dir.imageCacheDir() + "$searchId.jpeg", source = Source.YouTube, - albumArtURL = coverUrl, + albumArtURL = "https://i.ytimg.com/vi/$searchId/hqdefault.jpg", downloaded = if (dir.isPresent( - dir.finalOutputFile( + dir.finalOutputDir( itemName = name, type = folderType, subFolder = subFolder, + defaultDir = dir.defaultDir() ) ) ) @@ -176,7 +178,7 @@ class YoutubeProvider( else { DownloadStatus.NotDownloaded }, - outputFilePath = dir.finalOutputPath(name, folderType, subFolder /*,".m4a"*/), + outputFilePath = dir.finalOutputDir(name, folderType, subFolder, dir.defaultDir()/*,".m4a"*/), videoID = searchId ) )