Skip to content

Commit

Permalink
Add Search and Limit Home Tabs correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
LuftVerbot committed May 19, 2024
1 parent 5b07bfe commit 0a9036f
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 119 deletions.
72 changes: 47 additions & 25 deletions app/src/main/java/dev/brahmkshatriya/echo/extension/Convertors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import dev.brahmkshatriya.echo.common.models.ImageHolder.Companion.toImageHolder
import dev.brahmkshatriya.echo.common.models.MediaItemsContainer
import dev.brahmkshatriya.echo.common.models.Playlist
import dev.brahmkshatriya.echo.common.models.Track
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.int
import kotlinx.serialization.json.jsonArray
Expand All @@ -22,15 +24,28 @@ fun JsonObject.toMediaItemsContainer(
return MediaItemsContainer.Category(
title = name ?: "Unknown",
list = itemsArray.mapNotNull { item ->
item.jsonObject.toEchoMediaPlaylistItem(api)
item.jsonObject.toEchoMediaItem(api)
}
)
}

fun JsonObject.toEchoMediaPlaylistItem(
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)
}
)
}

fun JsonElement.toEchoMediaItem(
api: DeezerApi
): EchoMediaItem? {
val data = jsonObject["data"]!!.jsonObject
val data = jsonObject["data"]?.jsonObject ?: jsonObject
val type = data["__TYPE__"]!!.jsonPrimitive.content
return when {
type.contains("playlist") -> EchoMediaItem.Lists.PlaylistItem(toPlaylist(api))
Expand All @@ -42,8 +57,8 @@ fun JsonObject.toEchoMediaPlaylistItem(



fun JsonObject.toAlbum(): Album {
val data = jsonObject["data"]?.jsonObject ?: jsonObject["DATA"]!!.jsonObject
fun JsonElement.toAlbum(): Album {
val data = jsonObject["data"]?.jsonObject ?: jsonObject["DATA"]?.jsonObject ?: jsonObject
return Album(
id = data["ALB_ID"]?.jsonPrimitive?.content ?: "",
title = data["ALB_TITLE"]?.jsonPrimitive?.content ?: "",
Expand All @@ -53,7 +68,7 @@ fun JsonObject.toAlbum(): Album {
)
}

fun JsonObject.toTrack(): Track {
fun JsonElement.toTrack(): Track {
val data = jsonObject["data"]?.jsonObject ?: jsonObject
return Track(
id = data["SNG_ID"]!!.jsonPrimitive.content,
Expand All @@ -66,37 +81,44 @@ fun JsonObject.toTrack(): Track {
)
}

fun JsonObject.toPlaylist(api: DeezerApi): Playlist {
val data = jsonObject["data"]?.jsonObject ?: jsonObject["DATA"]!!.jsonObject
fun JsonElement.toPlaylist(api: DeezerApi): Playlist {
val data = jsonObject["data"]?.jsonObject ?: jsonObject["DATA"]?.jsonObject ?: jsonObject
val type = jsonObject["PICTURE_TYPE"]?.jsonPrimitive?.content
return Playlist(
id = data["PLAYLIST_ID"]?.jsonPrimitive?.content ?: "",
title = data["TITLE"]?.jsonPrimitive?.content ?: "",
cover = getCover(jsonObject),
cover = getCover(jsonObject, type),
description = data["DESCRIPTION"]?.jsonPrimitive?.content ?: "",
subtitle = jsonObject["subtitle"]?.jsonPrimitive?.content ?: "",
isEditable = data["PARENT_USER_ID"]!!.jsonPrimitive.content == api.userId,
tracks = data["NB_SONG"]?.jsonPrimitive?.int ?: 0,
)
}

fun getCover(jsonObject: JsonObject): ImageHolder {
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"
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"
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"
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"
return url.toImageHolder()
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"
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"
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"
return url.toImageHolder()
}
}
}
59 changes: 43 additions & 16 deletions app/src/main/java/dev/brahmkshatriya/echo/extension/DeezerApi.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.brahmkshatriya.echo.extension

import dev.brahmkshatriya.echo.common.models.Album
import dev.brahmkshatriya.echo.common.models.Artist
import dev.brahmkshatriya.echo.common.models.ImageHolder.Companion.toImageHolder
import dev.brahmkshatriya.echo.common.models.Playlist
import dev.brahmkshatriya.echo.common.models.Track
Expand Down Expand Up @@ -28,11 +29,11 @@ class Settings {
}

class DeezerApi(
private val arl: String = "",
private val sid: String = "",
private val token: String = "",
val userId: String = "",
private val licenseToken: String = "",
private var arl: String = "",
private var sid: String = "",
private var token: String = "",
var userId: String = "",
private var licenseToken: String = "",
private var userName: String? = null,
private var favoritesPlaylistId: String = ""
) {
Expand Down Expand Up @@ -109,15 +110,6 @@ class DeezerApi(
val responseBody = response.body?.string()
val body = responseBody.toString()

// Grab SID
/*if (method == "deezer.getUserData") {
response.headers("Set-Cookie").forEach { cookie ->
if (cookie.startsWith("sid=")) {
sid = cookie.split("=")[1]
}
}
}*/

body
}

Expand Down Expand Up @@ -174,7 +166,6 @@ class DeezerApi(
headersBuilder.add("Connection", "Keep-alive")
headersBuilder.add("Content-Type", "application/json; charset=utf-8")
headersBuilder.add("Cookie", "arl=$arl&sid=$sid")
//headersBuilder.add("Host", "media.deezer.com")
headersBuilder.add("Host", "media.deezer.com")
headersBuilder.add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36")
val headers = headersBuilder.build()
Expand Down Expand Up @@ -235,6 +226,30 @@ class DeezerApi(
jObject
}

suspend fun search(query: String): JsonObject {
val jsonData = callApi(
method = "deezer.pageSearch",
params = mapOf(
"nb" to 128,
"query" to query,
"start" to 0
)
)
val jObject = json.decodeFromString<JsonObject>(jsonData)
return jObject
}

suspend fun searchSuggestions(query: String): JsonObject {
val jsonData = callApi(
method = "search_getSuggestedQueries",
params = mapOf(
"QUERY" to query
)
)
val jObject = json.decodeFromString<JsonObject>(jsonData)
return jObject
}

suspend fun track(track: Track): JsonObject {
val jsonData = callApi(
method = "song.getListData",
Expand All @@ -246,6 +261,18 @@ class DeezerApi(
return jObject
}

suspend fun artist(artist: Artist): JsonObject {
val jsonData = callApi(
method = "song.getListData",
params = mapOf(
"art_id" to artist.id,
"lang" to settings.deezerLanguage
)
)
val jObject = json.decodeFromString<JsonObject>(jsonData)
return jObject
}

suspend fun album(album: Album): JsonObject {
val jsonData = callApi(
method = "deezer.pageAlbum",
Expand Down Expand Up @@ -304,7 +331,7 @@ class DeezerApi(
val jsonData = callApi(
method = "page.get",
gatewayInput = """
{"PAGE":"home","VERSION":"2.5","SUPPORT":{"ads":[],"deeplink-list":["deeplink"],"event-card":["live-event"],"grid-preview-one":["album","artist","artistLineUp","channel","livestream","flow","playlist","radio","show","smarttracklist","track","user","video-link","external-link"],"grid-preview-two":["album","artist","artistLineUp","channel","livestream","flow","playlist","radio","show","smarttracklist","track","user","video-link","external-link"],"grid":["album","artist","artistLineUp","channel","livestream","flow","playlist","radio","show","smarttracklist","track","user","video-link","external-link"],"horizontal-grid":["album","artist","artistLineUp","channel","livestream","flow","playlist","radio","show","smarttracklist","track","user","video-link","external-link"],"horizontal-list":["track","song"],"item-highlight":["radio"],"large-card":["album","external-link","playlist","show","video-link"],"list":["episode"],"message":["call_onboarding"],"mini-banner":["external-link"],"slideshow":["album","artist","channel","external-link","flow","livestream","playlist","show","smarttracklist","user","video-link"],"small-horizontal-grid":["flow"],"long-card-horizontal-grid":["album","artist","artistLineUp","channel","livestream","flow","playlist","radio","show","smarttracklist","track","user","video-link","external-link"],"filterable-grid":["flow"]},"LANG":"en","OPTIONS":["deeplink_newsandentertainment","deeplink_subscribeoffer"]}
{"PAGE":"home","VERSION":"2.5","SUPPORT":{"ads":[],"deeplink-list":["deeplink"],"event-card":["live-event"],"grid-preview-one":["album","artist","artistLineUp","channel","livestream","flow","playlist","radio","show","smarttracklist","track","user","video-link","external-link"],"grid-preview-two":["album","artist","artistLineUp","channel","livestream","flow","playlist","radio","show","smarttracklist","track","user","video-link","external-link"],"grid":["album","artist","artistLineUp","channel","livestream","flow","playlist","radio","show","smarttracklist","track","user","video-link","external-link"],"horizontal-grid":["album","artist","artistLineUp","channel","livestream","flow","playlist","radio","show","smarttracklist","track","user","video-link","external-link"],"horizontal-list":["track","song"],"item-highlight":["radio"],"large-card":["album","external-link","playlist","show","video-link"],"list":["episode"],"mini-banner":["external-link"],"slideshow":["album","artist","channel","external-link","flow","livestream","playlist","show","smarttracklist","user","video-link"],"small-horizontal-grid":["flow"],"long-card-horizontal-grid":["album","artist","artistLineUp","channel","livestream","flow","playlist","radio","show","smarttracklist","track","user","video-link","external-link"],"filterable-grid":["flow"]},"LANG":"en","OPTIONS":["deeplink_newsandentertainment","deeplink_subscribeoffer"]}
""".trimIndent()
)
val jObject = json.decodeFromString<JsonObject>(jsonData)
Expand Down
Loading

0 comments on commit 0a9036f

Please sign in to comment.