Skip to content

Commit

Permalink
Parameter Refactor. Should hopefully erase some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
LuftVerbot committed May 24, 2024
1 parent 2201504 commit ce8fe2d
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 128 deletions.
21 changes: 8 additions & 13 deletions app/src/main/java/dev/brahmkshatriya/echo/extension/Convertors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,42 @@ import kotlinx.serialization.json.jsonPrimitive


fun JsonElement.toMediaItemsContainer(
api: DeezerApi = DeezerApi(),
name: String?
): MediaItemsContainer {
val itemsArray = jsonObject["items"]!!.jsonArray
return MediaItemsContainer.Category(
title = name ?: "Unknown",
list = itemsArray.mapNotNull { item ->
item.jsonObject.toEchoMediaItem(api)
item.jsonObject.toEchoMediaItem()
}
)
}
fun JsonObject.toMediaItemsContainer(
api: DeezerApi = DeezerApi(),
name: String?
): MediaItemsContainer {
return MediaItemsContainer.Category(
title = name ?: "Unknown",
list = listOf(jsonObject.toEchoMediaItem(api) ?: emptyList<EchoMediaItem>().first())
list = listOf(jsonObject.toEchoMediaItem() ?: emptyList<EchoMediaItem>().first())
)
}

fun JsonArray.toMediaItemsContainer(
api: DeezerApi = DeezerApi(),
name: String?
): MediaItemsContainer {
val itemsArray = jsonArray
return MediaItemsContainer.Category(
title = name ?: "Unknown",
list = itemsArray.mapNotNull { item ->
item.jsonObject.toEchoMediaItem(api)
item.jsonObject.toEchoMediaItem()
}
)
}

fun JsonElement.toEchoMediaItem(
api: DeezerApi
): EchoMediaItem? {
fun JsonElement.toEchoMediaItem(): EchoMediaItem? {
val data = jsonObject["data"]?.jsonObject ?: jsonObject
val type = data["__TYPE__"]!!.jsonPrimitive.content
return when {
type.contains("playlist") -> EchoMediaItem.Lists.PlaylistItem(toPlaylist(api))
type.contains("playlist") -> EchoMediaItem.Lists.PlaylistItem(toPlaylist())
type.contains("album") -> EchoMediaItem.Lists.AlbumItem(toAlbum())
type.contains("song") -> EchoMediaItem.TrackItem(toTrack())
type.contains("artist") -> EchoMediaItem.Profile.ArtistItem(toArtist())
Expand Down Expand Up @@ -101,12 +96,12 @@ fun JsonElement.toTrack(): Track {
cover = getCover(md5, "cover"),
extras = mapOf(
"TRACK_TOKEN" to (data["TRACK_TOKEN"]?.jsonPrimitive?.content ?: ""),
"FILESIZE_MP3_MISC" to (data["FILESIZE_MP3_MISC"]?.jsonPrimitive?.content ?: "")
"FILESIZE_MP3_MISC" to (data["FILESIZE_MP3_MISC"]?.jsonPrimitive?.content ?: "0")
)
)
}

fun JsonElement.toPlaylist(api: DeezerApi): Playlist {
fun JsonElement.toPlaylist(): Playlist {
val data = jsonObject["data"]?.jsonObject ?: jsonObject["DATA"]?.jsonObject ?: jsonObject
val type = data["PICTURE_TYPE"]?.jsonPrimitive?.content ?: ""
val md5 = data["PLAYLIST_PICTURE"]?.jsonPrimitive?.content ?: ""
Expand All @@ -116,7 +111,7 @@ fun JsonElement.toPlaylist(api: DeezerApi): Playlist {
cover = getCover(md5, type),
description = data["DESCRIPTION"]?.jsonPrimitive?.content ?: "",
subtitle = jsonObject["subtitle"]?.jsonPrimitive?.content ?: "",
isEditable = data["PARENT_USER_ID"]!!.jsonPrimitive.content == api.userId,
isEditable = data["PARENT_USER_ID"]!!.jsonPrimitive.content == DeezerCredentials.userId,
tracks = data["NB_SONG"]?.jsonPrimitive?.int ?: 0,
)
}
Expand Down
96 changes: 51 additions & 45 deletions app/src/main/java/dev/brahmkshatriya/echo/extension/DeezerApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,36 @@ import java.util.zip.GZIPInputStream

// Settings placeholder
class Settings {
val deezerLanguage: String? = "en"
val deezerCountry: String? = "US"
val deezerLanguage: String = "en"
val deezerCountry: String = "US"
}

class DeezerApi(
private var arl: String = "",
private var sid: String = "",
private var token: String = "",
var userId: String = "",
private var licenseToken: String = "",
private var userName: String = "",
private var favoritesPlaylistId: String = ""
) {
class DeezerApi {

private val settings = Settings()

// Access shared email and pass
private val arl: String
get() = DeezerCredentials.arl

private val sid: String
get() = DeezerCredentials.sid

private val token: String
get() = DeezerCredentials.token

private val userId: String
get() = DeezerCredentials.userId

private val licenseToken: String
get() = DeezerCredentials.licenseToken

private val email: String
get() = DeezerCredentials.email

private val pass: String
get() = DeezerCredentials.pass

private val client: OkHttpClient = OkHttpClient.Builder()
.addInterceptor { chain ->
val originalResponse = chain.proceed(chain.request())
Expand All @@ -66,10 +80,10 @@ class DeezerApi(
headersBuilder.add("Accept", "*/*")
headersBuilder.add("Accept-Charset", "utf-8,ISO-8859-1;q=0.7,*;q=0.3")
headersBuilder.add("Accept-Encoding", "gzip")
headersBuilder.add("Accept-Language", settings.deezerLanguage ?: "en")
headersBuilder.add("Accept-Language", settings.deezerLanguage)
headersBuilder.add("Cache-Control", "max-age=0")
headersBuilder.add("Connection", "keep-alive")
headersBuilder.add("Content-Language", "${settings.deezerLanguage ?: "en"}-${settings.deezerCountry ?: "US"}")
headersBuilder.add("Content-Language", "${settings.deezerLanguage}-${settings.deezerCountry}")
headersBuilder.add("Content-Type", "application/json; charset=utf-8")
if (method != "user.getArl") {
headersBuilder.add("Cookie", "arl=$arl; sid=$sid")
Expand Down Expand Up @@ -121,27 +135,24 @@ class DeezerApi(
val responseBody = response.body?.string()
val body = responseBody.toString()

body
}
if (method == "deezer.getUserData") {
response.headers.forEach {
if (it.second.startsWith("sid=")) {
DeezerCredentials.sid = it.second.substringAfter("sid=").substringBefore(";")
}
}
}

private suspend fun rawAuthorize(): Boolean {
val jsonData = callApi("deezer.getUserData")
val jObject = json.decodeFromString<JsonObject>(jsonData)
val userResults = jObject["results"]
val user = userResults!!.jsonObject["USER"]
val userId = user!!.jsonObject["USER_ID"]!!.jsonPrimitive.content
if (userId.contentEquals("0")) {
return false
} else {
//token = userResults.jsonObject["checkForm"]!!.jsonPrimitive.content
//userId = user.jsonObject["USER_ID"]!!.jsonPrimitive.int.toString()
userName = user.jsonObject["BLOG_NAME"]!!.jsonPrimitive.content
favoritesPlaylistId = user.jsonObject["LOVEDTRACKS_ID"]!!.jsonPrimitive.content
return true
if(body.contains("\"VALID_TOKEN_REQUIRED\":\"Invalid CSRF token\"")) {
val userList = DeezerExtension().onLogin(email, pass)
DeezerExtension().onSetLoginUser(userList.first())
return@withContext callApi(method, params, gatewayInput)
}

body
}

suspend fun makeUser(): List<User> {
suspend fun makeUser(email: String = "", pass: String = ""): List<User> {
val userList = mutableListOf<User>()
val jsonData = callApi("deezer.getUserData")
val jObject = json.decodeFromString<JsonObject>(jsonData)
Expand All @@ -161,16 +172,18 @@ class DeezerApi(
"user_id" to userId,
"sid" to sid,
"token" to token,
"license_token" to licenseToken
"license_token" to licenseToken,
"email" to email,
"pass" to pass
)
)
userList.add(user)
return userList
}

suspend fun getArlByEmail(mail: String, password: String): Map<String, String?> {
suspend fun getArlByEmail(mail: String, password: String) {
//Get SID
getSid()
//getSid()

val clientId = "447462"
val clientSecret = "a83bf7f38ad2f137e444727cfc3775cf"
Expand All @@ -186,18 +199,12 @@ class DeezerApi(
//Get access token
val responseJson = getToken(params)
val apiResponse = json.decodeFromString<JsonObject>(responseJson)
token = apiResponse.jsonObject["access_token"]!!.jsonPrimitive.content
DeezerCredentials.token = apiResponse.jsonObject["access_token"]!!.jsonPrimitive.content

// Get ARL
val arlResponse = callApi("user.getArl")
val arlObject = json.decodeFromString<JsonObject>(arlResponse)
arl = arlObject["results"]!!.jsonPrimitive.content

return mapOf(
"arl" to arl,
"token" to token,
"sid" to sid
)
DeezerCredentials.arl = arlObject["results"]!!.jsonPrimitive.content
}

private fun md5(input: String): String {
Expand Down Expand Up @@ -229,7 +236,7 @@ class DeezerApi(
}
}

fun getSid(): String {
fun getSid() {
//Get SID
val url = "https://www.deezer.com/"
val request = Request.Builder()
Expand All @@ -240,16 +247,15 @@ class DeezerApi(
val response = client.newCall(request).execute()
response.headers.forEach {
if (it.second.startsWith("sid=")) {
sid = it.second.substringAfter("sid=").substringBefore(";")
DeezerCredentials.sid = it.second.substringAfter("sid=").substringBefore(";")
}
}
return sid
}

suspend fun getMP3MediaUrl(track: Track): JsonObject = withContext(Dispatchers.IO) {
val headersBuilder = Headers.Builder()
headersBuilder.add("Accept-Encoding", "gzip")
headersBuilder.add("Accept-Language", settings.deezerLanguage ?: "en")
headersBuilder.add("Accept-Language", settings.deezerLanguage)
headersBuilder.add("Cache-Control", "max-age=0")
headersBuilder.add("Connection", "Keep-alive")
headersBuilder.add("Content-Type", "application/json; charset=utf-8")
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/dev/brahmkshatriya/echo/extension/DeezerBase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.brahmkshatriya.echo.extension

object DeezerCredentials {
var arl: String = ""
var sid: String = ""
var token: String = ""
var userId: String = ""
var licenseToken: String = ""
var email: String = ""
var pass: String = ""
}
Loading

0 comments on commit ce8fe2d

Please sign in to comment.