From 9800edf68dd839ba1478e17635cdcae45317a6bf Mon Sep 17 00:00:00 2001 From: RemixDev Date: Thu, 22 Apr 2021 10:32:41 +0200 Subject: [PATCH] Added search and tracklist pages --- server/package.json | 2 +- server/src/main.ts | 90 +++++++++++++++++++++++ server/src/routes/api/get/getTracklist.ts | 13 ++++ server/src/routes/api/get/index.ts | 5 +- server/src/routes/api/get/mainSearch.ts | 13 ++++ server/src/routes/api/get/search.ts | 13 ++++ server/yarn.lock | 8 +- 7 files changed, 138 insertions(+), 6 deletions(-) diff --git a/server/package.json b/server/package.json index 156b887..82713dc 100644 --- a/server/package.json +++ b/server/package.json @@ -14,7 +14,7 @@ "cookie-parser": "1.4.5", "debug": "2.6.9", "deemix": "^0.0.1", - "deezer-js": "^0.0.6", + "deezer-js": "^0.0.7", "express": "4.17.1", "morgan": "1.10.0", "ws": "^7.4.5" diff --git a/server/src/main.ts b/server/src/main.ts index 33d939a..ba720d8 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -29,3 +29,93 @@ export async function getCharts(){ } return chartsCache } + +export async function getTracklist(list_id: string, list_type: string){ + switch (list_type) { + case 'artist': + let artistAPI = await dz.api.get_artist(list_id) + artistAPI.releases = await dz.gw.get_artist_discography_tabs(list_id, 100) + return artistAPI + default: + let releaseAPI = await dz.api[`get_${list_type}`](list_id) + let releaseTracksAPI = await dz.api[`get_${list_type}_tracks`](list_id) + releaseTracksAPI = releaseTracksAPI['data'] + + let tracks: any[] = [] + const showdiscs = (list_type == 'album' && releaseTracksAPI.length && releaseTracksAPI[releaseTracksAPI.length -1].disk_number != 1) + let current_disk = 0 + + releaseTracksAPI.forEach((track: any) => { + if (showdiscs && parseInt(track.disk_number) !== current_disk){ + current_disk = parseInt(track.disk_number) + tracks.push({type: 'disc_separator', number: current_disk}) + } + track.selected = false + tracks.push(track) + }) + releaseAPI.tracks = tracks + return releaseAPI + break + } +} + +export async function searchAll(term: string){ + let results = await dz.gw.search(term) + let order: string[] = [] + results.ORDER.forEach((element: string) => { + if (['TOP_RESULT', 'TRACK', 'ALBUM', 'ARTIST', 'PLAYLIST'].indexOf(element) != -1) order.push(element) + }) + if (results.TOP_RESULT && results.TOP_RESULT.length){ + let originalTopResult = results.TOP_RESULT[0] + let topResult: any = { + type: originalTopResult.__TYPE__ + } + switch (topResult.type) { + case 'artist': + topResult.id = originalTopResult.ART_ID + topResult.picture = `https://e-cdns-images.dzcdn.net/images/artist/${originalTopResult.ART_PICTURE}` + topResult.title = originalTopResult.ART_NAME + topResult.nb_fan = originalTopResult.NB_FAN + break + case 'album': + topResult.id = originalTopResult.ALB_ID + topResult.picture = `https://e-cdns-images.dzcdn.net/images/cover/${originalTopResult.ALB_PICTURE}` + topResult.title = originalTopResult.ALB_TITLE + topResult.artist = originalTopResult.ART_NAME + topResult.nb_song = originalTopResult.NUMBER_TRACK + break + case 'playlist': + topResult.id = originalTopResult.PLAYLIST_ID + topResult.picture = `https://e-cdns-images.dzcdn.net/images/${originalTopResult.PICTURE_TYPE}/${originalTopResult.PLAYLIST_PICTURE}` + topResult.title = originalTopResult.TITLE + topResult.artist = originalTopResult.PARENT_USERNAME + topResult.nb_song = originalTopResult.NB_SONG + break + default: + topResult.id = "0" + topResult.picture = 'https://e-cdns-images.dzcdn.net/images/cover' + break; + } + } + results.ORDER = order + return results +} + +export async function search(term: string, type: string, start: number, nb: number){ + switch (type) { + case 'track': + return await dz.api.search_track(term, {limit: nb, index: start}) + case 'album': + return await dz.api.search_album(term, {limit: nb, index: start}) + case 'artist': + return await dz.api.search_artist(term, {limit: nb, index: start}) + case 'playlist': + return await dz.api.search_playlist(term, {limit: nb, index: start}) + case 'radio': + return await dz.api.search_radio(term, {limit: nb, index: start}) + case 'user': + return await dz.api.search_user(term, {limit: nb, index: start}) + default: + return await dz.api.search(term, {limit: nb, index: start}) + } +} diff --git a/server/src/routes/api/get/getTracklist.ts b/server/src/routes/api/get/getTracklist.ts index e69de29..a1a0b2f 100644 --- a/server/src/routes/api/get/getTracklist.ts +++ b/server/src/routes/api/get/getTracklist.ts @@ -0,0 +1,13 @@ +import { ApiHandler } from '../../../types' +import { getTracklist } from '../../../main' + +const path: ApiHandler['path'] = '/getTracklist' + +const handler: ApiHandler['handler'] = async (req, res) => { + const tracklistData = await getTracklist(String(req.query.id), String(req.query.type)) + res.send(tracklistData) +} + +const apiHandler: ApiHandler = { path, handler } + +export default apiHandler diff --git a/server/src/routes/api/get/index.ts b/server/src/routes/api/get/index.ts index 8f98e88..44c3b8a 100644 --- a/server/src/routes/api/get/index.ts +++ b/server/src/routes/api/get/index.ts @@ -1,5 +1,8 @@ import sample from './sample' import getHome from './getHome' import getCharts from './getCharts' +import mainSearch from './mainSearch' +import search from './search' +import getTracklist from './getTracklist' -export default [sample, getHome, getCharts] +export default [sample, getHome, getCharts, mainSearch, search, getTracklist] diff --git a/server/src/routes/api/get/mainSearch.ts b/server/src/routes/api/get/mainSearch.ts index e69de29..9c9b69a 100644 --- a/server/src/routes/api/get/mainSearch.ts +++ b/server/src/routes/api/get/mainSearch.ts @@ -0,0 +1,13 @@ +import { ApiHandler } from '../../../types' +import { searchAll } from '../../../main' + +const path: ApiHandler['path'] = '/mainSearch' + +const handler: ApiHandler['handler'] = async (req, res) => { + const searchData = await searchAll(String(req.query.term)) + res.send(searchData) +} + +const apiHandler: ApiHandler = { path, handler } + +export default apiHandler diff --git a/server/src/routes/api/get/search.ts b/server/src/routes/api/get/search.ts index e69de29..803d552 100644 --- a/server/src/routes/api/get/search.ts +++ b/server/src/routes/api/get/search.ts @@ -0,0 +1,13 @@ +import { ApiHandler } from '../../../types' +import { search } from '../../../main' + +const path: ApiHandler['path'] = '/search' + +const handler: ApiHandler['handler'] = async (req, res) => { + const searchData = await search(String(req.query.term), String(req.query.type), parseInt(String(req.query.start)), parseInt(String(req.query.nb))) + res.send(searchData) +} + +const apiHandler: ApiHandler = { path, handler } + +export default apiHandler diff --git a/server/yarn.lock b/server/yarn.lock index f22f966..6c30baf 100644 --- a/server/yarn.lock +++ b/server/yarn.lock @@ -1118,10 +1118,10 @@ deezer-js@^0.0.3: got "^11.8.2" tough-cookie "^4.0.0" -deezer-js@^0.0.6: - version "0.0.6" - resolved "https://registry.npmjs.org/deezer-js/-/deezer-js-0.0.6.tgz" - integrity sha512-EX8dLLzM8hc1a1fWEi9agwu9MW9HjZxoWTEfzPLGpiJkh/Yix4Z1HM6X3KwBh4Uc90TIlzUdePRY3fmbpcZfig== +deezer-js@^0.0.7: + version "0.0.7" + resolved "https://registry.npmjs.org/deezer-js/-/deezer-js-0.0.7.tgz" + integrity sha512-DCnIGDvgikWSqJd1GJ5jsJ6ieY9VHLaGLMeizTCoAbDNQqzxMB1wDO+u2KI76dBKZIhVJOAuXTe1gZzok7VjrQ== dependencies: got "^11.8.2" tough-cookie "^4.0.0"