/
SpotifyProvider.kt
237 lines (215 loc) · 9.44 KB
/
SpotifyProvider.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Copyright (c) 2021 Shabinder Singh
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.shabinder.common.providers.spotify
import co.touchlab.kermit.Kermit
import com.shabinder.common.core_components.file_manager.FileManager
import com.shabinder.common.core_components.file_manager.finalOutputDir
import com.shabinder.common.core_components.file_manager.getImageCachePath
import com.shabinder.common.core_components.utils.createHttpClient
import com.shabinder.common.models.*
import com.shabinder.common.models.event.coroutines.SuspendableEvent
import com.shabinder.common.models.spotify.PlaylistTrack
import com.shabinder.common.models.spotify.Source
import com.shabinder.common.models.spotify.Track
import com.shabinder.common.providers.spotify.requests.SpotifyRequests
import com.shabinder.common.providers.spotify.requests.authenticateSpotify
import com.shabinder.common.providers.spotify.token_store.TokenStore
import com.shabinder.common.utils.globalJson
import io.ktor.client.*
import io.ktor.client.features.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
import io.ktor.client.request.*
class SpotifyProvider(
private val tokenStore: TokenStore,
private val logger: Kermit,
private val fileManager: FileManager,
) : SpotifyRequests {
override suspend fun authenticateSpotifyClient(override: Boolean) {
val token = if (override) authenticateSpotify().component1() else tokenStore.getToken()
if (token == null) {
logger.d { "Spotify Auth Failed: Please Check your Network Connection" }
} else {
logger.d { "Spotify Provider Created with $token" }
HttpClient {
defaultRequest {
header("Authorization", "Bearer ${token.access_token}")
}
install(JsonFeature) {
serializer = KotlinxSerializer(globalJson)
}
}.also { httpClientRef.value = it }
}
}
override val httpClientRef = NativeAtomicReference(createHttpClient(true))
suspend fun query(fullLink: String): SuspendableEvent<PlatformQueryResult, Throwable> = SuspendableEvent {
var spotifyLink =
"https://" + fullLink.substringAfterLast("https://").substringBefore(" ").trim()
if (!spotifyLink.contains("open.spotify")) {
// Very Rare instance
spotifyLink = resolveLink(spotifyLink)
}
val link = spotifyLink.substringAfterLast('/', "Error").substringBefore('?')
val type = spotifyLink.substringBeforeLast('/', "Error").substringAfterLast('/')
if (type == "Error" || link == "Error") {
throw SpotiFlyerException.LinkInvalid(fullLink)
}
if (type == "episode" || type == "show") {
throw SpotiFlyerException.FeatureNotImplementedYet(
"Support for Spotify's ${type.toUpperCase()} isn't implemented yet"
)
}
try {
spotifySearch(
type,
link
)
} catch (e: Exception) {
e.printStackTrace()
// Try Reinitialising Client // Handle 401 Token Expiry ,etc Exceptions
authenticateSpotifyClient(true)
spotifySearch(
type,
link
)
}
}
private suspend fun spotifySearch(
type: String,
link: String
): PlatformQueryResult {
return PlatformQueryResult(
folderType = "",
subFolder = "",
title = "",
coverUrl = "",
trackList = listOf(),
Source.Spotify
).apply {
when (type) {
"track" -> {
getTrack(link).also {
folderType = "Tracks"
subFolder = ""
trackList = listOf(it).toTrackDetailsList(folderType, subFolder)
title = it.name.toString()
coverUrl = it.album?.images?.elementAtOrNull(0)?.url.toString()
}
}
"album" -> {
val albumObject = getAlbum(link)
folderType = "Albums"
subFolder = albumObject.name.toString()
albumObject.tracks?.items?.forEach { it.album = albumObject }
albumObject.tracks?.items?.toTrackDetailsList(folderType, subFolder).let {
if (it.isNullOrEmpty()) {
// TODO Handle Error
} else {
trackList = it
title = albumObject.name.toString()
coverUrl = albumObject.images?.elementAtOrNull(0)?.url.toString()
}
}
}
"playlist" -> {
val playlistObject = getPlaylist(link)
folderType = "Playlists"
subFolder = playlistObject.name.toString()
val tempTrackList = mutableListOf<Track>().apply {
// Add Fetched Tracks
playlistObject.tracks?.items?.mapNotNull(PlaylistTrack::track)?.let {
addAll(it)
}
}
// Check For More Tracks If available
var moreTracksAvailable = !playlistObject.tracks?.next.isNullOrBlank()
while (moreTracksAvailable) {
// Fetch Remaining Tracks
val moreTracks =
getPlaylistTracks(link, offset = tempTrackList.size)
moreTracks.items?.mapNotNull(PlaylistTrack::track)?.let { remTracks ->
tempTrackList.addAll(remTracks)
}
moreTracksAvailable = !moreTracks.next.isNullOrBlank()
}
// log("Total Tracks Fetched", tempTrackList.size.toString())
trackList = tempTrackList.toTrackDetailsList(folderType, subFolder)
title = playlistObject.name.toString()
coverUrl = playlistObject.images?.firstOrNull()?.url.toString()
}
"episode" -> { // TODO
throw SpotiFlyerException.FeatureNotImplementedYet()
}
"show" -> { // TODO
throw SpotiFlyerException.FeatureNotImplementedYet()
}
else -> {
throw SpotiFlyerException.LinkInvalid("Provide: Spotify, Type:$type -> Link:$link")
}
}
}
}
/*
* New Link Schema: https://link.tospotify.com/kqTBblrjQbb,
* Fetching Standard Link: https://open.spotify.com/playlist/37i9dQZF1DX9RwfGbeGQwP?si=iWz7B1tETiunDntnDo3lSQ&_branch_match_id=862039436205270630
* */
private suspend fun resolveLink(
url: String
): String {
val response = getResponse(url)
val regex = """https://open\.spotify\.com.+\w""".toRegex()
return regex.find(response)?.value.toString()
}
private fun List<Track>.toTrackDetailsList(type: String, subFolder: String) = this.map {
TrackDetails(
title = it.name.toString(),
trackNumber = it.track_number,
genre = it.album?.genres?.filterNotNull() ?: emptyList(),
artists = it.artists?.map { artist -> artist?.name.toString() } ?: listOf(),
albumArtists = it.album?.artists?.mapNotNull { artist -> artist?.name } ?: emptyList(),
durationSec = (it.duration_ms / 1000).toInt(),
albumArtPath = fileManager.getImageCachePath(it.album?.images?.firstOrNull()?.url ?: ""),
albumName = it.album?.name,
year = it.album?.release_date,
comment = "Genres:${it.album?.genres?.joinToString()}",
trackUrl = it.href,
downloaded = it.updateStatusIfPresent(type, subFolder),
source = Source.Spotify,
albumArtURL = it.album?.images?.firstOrNull()?.url.toString(),
outputFilePath = fileManager.finalOutputDir(
it.name.toString(),
type,
subFolder,
fileManager.defaultDir()/*,".m4a"*/
)
)
}
private fun Track.updateStatusIfPresent(folderType: String, subFolder: String): DownloadStatus {
return if (fileManager.isPresent(
fileManager.finalOutputDir(
name.toString(),
folderType,
subFolder,
fileManager.defaultDir()
)
)
) { // Download Already Present!!
DownloadStatus.Downloaded.also {
downloaded = it
}
} else downloaded
}
}