Code Cleanup

This commit is contained in:
shabinder 2021-05-14 04:06:07 +05:30
parent 0c67474be5
commit cc7fc98720
6 changed files with 29 additions and 78 deletions

View File

@ -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

View File

@ -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()

View File

@ -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.*

View File

@ -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!!

View File

@ -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<Track>.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!!

View File

@ -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
)
)