Skip to content

Commit

Permalink
Add ARL Login, Improve Cover Quality & Limit to 320kbps
Browse files Browse the repository at this point in the history
  • Loading branch information
LuftVerbot committed May 24, 2024
1 parent dc968bf commit 6486f29
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,26 @@ fun JsonElement.toPlaylist(api: DeezerApi): Playlist {
fun getCover(jsonObject: JsonObject, type: String? = null): ImageHolder {
if(type != null) {
val md5 = jsonObject["PLAYLIST_PICTURE"]!!.jsonPrimitive.content
val url = "https://e-cdns-images.dzcdn.net/images/$type/$md5/264x264-000000-80-0-0.jpg"
val url = "https://e-cdns-images.dzcdn.net/images/$type/$md5/1200x1200-000000-80-0-0.jpg"
return url.toImageHolder()
} else {
if (jsonObject["pictures"]?.jsonArray != null) {
val pictureArray = jsonObject["pictures"]!!.jsonArray
val picObject = pictureArray.first().jsonObject
val md5 = picObject["md5"]!!.jsonPrimitive.content
val type = picObject["type"]!!.jsonPrimitive.content
val url = "https://e-cdns-images.dzcdn.net/images/$type/$md5/264x264-000000-80-0-0.jpg"
val ownType = picObject["type"]!!.jsonPrimitive.content
val url = "https://e-cdns-images.dzcdn.net/images/$ownType/$md5/1200x1200-000000-80-0-0.jpg"
return url.toImageHolder()
} else if (jsonObject["DATA"]?.jsonObject != null) {
val dataObject = jsonObject["DATA"]!!.jsonObject
val md5 = dataObject["PLAYLIST_PICTURE"]?.jsonPrimitive?.content
?: dataObject["ALB_PICTURE"]?.jsonPrimitive?.content ?: ""
val type = dataObject["PICTURE_TYPE"]?.jsonPrimitive?.content ?: "cover"
val url = "https://e-cdns-images.dzcdn.net/images/$type/$md5/264x264-000000-80-0-0.jpg"
val ownType = dataObject["PICTURE_TYPE"]?.jsonPrimitive?.content ?: "cover"
val url = "https://e-cdns-images.dzcdn.net/images/$ownType/$md5/1200x1200-000000-80-0-0.jpg"
return url.toImageHolder()
} else {
val md5 = jsonObject["ALB_PICTURE"]?.jsonPrimitive?.content ?: ""
val url = "https://e-cdns-images.dzcdn.net/images/cover/$md5/264x264-000000-80-0-0.jpg"
val url = "https://e-cdns-images.dzcdn.net/images/cover/$md5/1200x1200-000000-80-0-0.jpg"
return url.toImageHolder()
}
}
Expand Down
35 changes: 21 additions & 14 deletions app/src/main/java/dev/brahmkshatriya/echo/extension/DeezerApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class DeezerApi(
}
}

suspend fun makeUser(): List<User>{
suspend fun makeUser(): List<User> {
val userList = mutableListOf<User>()
val jsonData = callApi("deezer.getUserData")
val jObject = json.decodeFromString<JsonObject>(jsonData)
Expand Down Expand Up @@ -170,18 +170,7 @@ class DeezerApi(

suspend fun getArlByEmail(mail: String, password: String): Map<String, String?> {
//Get SID
val url = "https://www.deezer.com/"
val request = Request.Builder()
.url(url)
.get()
.build()

val response = client.newCall(request).execute()
response.headers.forEach {
if(it.second.startsWith("sid=")) {
sid = it.second.substringAfter("sid=").substringBefore(";")
}
}
getSid()

val clientId = "447462"
val clientSecret = "a83bf7f38ad2f137e444727cfc3775cf"
Expand Down Expand Up @@ -240,6 +229,23 @@ class DeezerApi(
}
}

fun getSid(): String {
//Get SID
val url = "https://www.deezer.com/"
val request = Request.Builder()
.url(url)
.get()
.build()

val response = client.newCall(request).execute()
response.headers.forEach {
if (it.second.startsWith("sid=")) {
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")
Expand Down Expand Up @@ -307,7 +313,8 @@ class DeezerApi(

// Create request body
val requestBody = JSONObject(mapOf(
"formats" to arrayOf("FLAC", "MP3_320", "MP3_128", "MP3_64", "MP3_MISC"),
// Limit to 320 until download is added
"formats" to arrayOf(/*"FLAC", */"MP3_320", "MP3_128", "MP3_64", "MP3_MISC"),
"ids" to arrayOf(track.id.toLong())
)).toString().toRequestBody("application/json; charset=utf-8".toMediaType())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import org.apache.http.conn.ConnectTimeoutException
import java.util.Locale

class DeezerExtension : ExtensionClient, HomeFeedClient, TrackClient, SearchClient, AlbumClient,
PlaylistClient, LoginClient.WebView, LoginClient.UsernamePassword, LibraryClient {
PlaylistClient, LoginClient.WebView, LoginClient.UsernamePassword, LoginClient.CustomTextInput, LibraryClient {

private val json = Json {
isLenient = true
Expand Down Expand Up @@ -417,6 +417,24 @@ class DeezerExtension : ExtensionClient, HomeFeedClient, TrackClient, SearchClie
}
}

override val loginInputFields: List<LoginClient.InputField>
get() = listOf(
LoginClient.InputField(
key = "arl",
label = "ARL",
isRequired = false,
isPassword = true

)
)

override suspend fun onLogin(data: Map<String, String?>): List<User> {
val arl = data["arl"] ?: ""
val sid = DeezerApi().getSid()
val userList = DeezerApi(arl, sid).makeUser()
return userList
}

override suspend fun onLogin(username: String, password: String): List<User> {
val map = DeezerApi().getArlByEmail(username, password)
val arl = map["arl"] ?: ""
Expand Down

0 comments on commit 6486f29

Please sign in to comment.