From e54eaa6a00cb9a9ab7e5bf2cb2d736d7b71acfd4 Mon Sep 17 00:00:00 2001 From: RemixDev Date: Fri, 30 Apr 2021 14:27:37 +0200 Subject: [PATCH] Moved functions out of main, added getSettings --- server/package.json | 4 +- server/src/main.ts | 125 +------------------- server/src/routes/api/get/getChartTracks.ts | 2 +- server/src/routes/api/get/getCharts.ts | 21 +++- server/src/routes/api/get/getHome.ts | 10 +- server/src/routes/api/get/getSettings.ts | 12 ++ server/src/routes/api/get/getTracklist.ts | 38 +++++- server/src/routes/api/get/index.ts | 12 +- server/src/routes/api/get/mainSearch.ts | 44 ++++++- server/src/routes/api/get/search.ts | 37 ++++-- server/yarn.lock | 32 +++-- 11 files changed, 179 insertions(+), 158 deletions(-) diff --git a/server/package.json b/server/package.json index 95ea304..a6f154e 100644 --- a/server/package.json +++ b/server/package.json @@ -15,8 +15,8 @@ "dependencies": { "cookie-parser": "1.4.5", "debug": "2.6.9", - "deemix": "0.0.1", - "deezer-js": "0.0.8", + "deemix": "0.0.2", + "deezer-js": "0.0.10", "dotenv": "8.2.0", "express": "4.17.1", "morgan": "1.10.0", diff --git a/server/src/main.ts b/server/src/main.ts index 28e49ba..ab40b0b 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -1,124 +1,7 @@ // @ts-expect-error import { Deezer } from 'deezer-js' +// @ts-expect-error +import deemix from 'deemix' -export const dz = new Deezer() -let homeCache: any, chartsCache: any - -export async function getHome() { - if (!homeCache) { - homeCache = await dz.api.get_chart(0, { limit: 30 }) - } - return homeCache -} - -export async function getCharts() { - if (!chartsCache) { - const chartsData = await dz.api.get_countries_charts() - const countries: any[] = [] - chartsData.forEach((country: any) => { - countries.push({ - title: country.title.replace('Top ', ''), - id: country.id, - picture_small: country.picture_small, - picture_medium: country.picture_medium, - picture_big: country.picture_big - }) - }) - chartsCache = { data: countries } - } - return chartsCache -} - -export async function getTracklist(list_id: string, list_type: string) { - switch (list_type) { - case 'artist': { - const artistAPI = await dz.api.get_artist(list_id) - artistAPI.releases = await dz.gw.get_artist_discography_tabs(list_id, 100) - return artistAPI - } - default: { - const releaseAPI = await dz.api[`get_${list_type}`](list_id) - let releaseTracksAPI = await dz.api[`get_${list_type}_tracks`](list_id) - releaseTracksAPI = releaseTracksAPI.data - - const 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 - } - } -} - -export async function searchAll(term: string) { - const results = await dz.gw.search(term) - const order: string[] = [] - results.ORDER.forEach((element: string) => { - if (['TOP_RESULT', 'TRACK', 'ALBUM', 'ARTIST', 'PLAYLIST'].includes(element)) order.push(element) - }) - if (results.TOP_RESULT && results.TOP_RESULT.length) { - const originalTopResult = results.TOP_RESULT[0] - const 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 }) - } -} +export let settings: any = deemix.settings.load() +export const dz = new Deezer(settings.tagsLanguage) diff --git a/server/src/routes/api/get/getChartTracks.ts b/server/src/routes/api/get/getChartTracks.ts index d40bcf8..c9d49dc 100644 --- a/server/src/routes/api/get/getChartTracks.ts +++ b/server/src/routes/api/get/getChartTracks.ts @@ -10,7 +10,7 @@ export interface RawChartTracksQuery { limit?: number } -const path: ApiHandler['path'] = '/get-chart-tracks' +const path: ApiHandler['path'] = '/getChartTracks' const handler: RequestHandler<{}, {}, {}, RawChartTracksQuery> = async (req, res, next) => { try { diff --git a/server/src/routes/api/get/getCharts.ts b/server/src/routes/api/get/getCharts.ts index e33dafd..7222906 100644 --- a/server/src/routes/api/get/getCharts.ts +++ b/server/src/routes/api/get/getCharts.ts @@ -1,11 +1,26 @@ import { ApiHandler } from '../../../types' -import { getCharts } from '../../../main' +import { dz } from '../../../main' const path: ApiHandler['path'] = '/getCharts' +let chartsCache: any + const handler: ApiHandler['handler'] = async (_, res) => { - const chartsData = await getCharts() - res.send(chartsData) + if (!chartsCache) { + const chartsData = await dz.api.get_countries_charts() + const countries: any[] = [] + chartsData.forEach((country: any) => { + countries.push({ + title: country.title.replace('Top ', ''), + id: country.id, + picture_small: country.picture_small, + picture_medium: country.picture_medium, + picture_big: country.picture_big + }) + }) + chartsCache = { data: countries } + } + res.send(chartsCache) } const apiHandler: ApiHandler = { path, handler } diff --git a/server/src/routes/api/get/getHome.ts b/server/src/routes/api/get/getHome.ts index d0b1da1..58974a7 100644 --- a/server/src/routes/api/get/getHome.ts +++ b/server/src/routes/api/get/getHome.ts @@ -1,11 +1,15 @@ import { ApiHandler } from '../../../types' -import { getHome } from '../../../main' +import { dz } from '../../../main' const path: ApiHandler['path'] = '/getHome' +let homeCache: any + const handler: ApiHandler['handler'] = async (_, res) => { - const homeData = await getHome() - res.send(homeData) + if (!homeCache) { + homeCache = await dz.api.get_chart(0, { limit: 30 }) + } + res.send(homeCache) } const apiHandler: ApiHandler = { path, handler } diff --git a/server/src/routes/api/get/getSettings.ts b/server/src/routes/api/get/getSettings.ts index e69de29..7c32cfe 100644 --- a/server/src/routes/api/get/getSettings.ts +++ b/server/src/routes/api/get/getSettings.ts @@ -0,0 +1,12 @@ +import { ApiHandler } from '../../../types' +import { settings } from '../../../main' + +const path: ApiHandler['path'] = '/getSettings' + +const handler: ApiHandler['handler'] = async (_, res) => { + res.send(settings) +} + +const apiHandler: ApiHandler = { path, handler } + +export default apiHandler diff --git a/server/src/routes/api/get/getTracklist.ts b/server/src/routes/api/get/getTracklist.ts index a1a0b2f..f59c757 100644 --- a/server/src/routes/api/get/getTracklist.ts +++ b/server/src/routes/api/get/getTracklist.ts @@ -1,11 +1,43 @@ import { ApiHandler } from '../../../types' -import { getTracklist } from '../../../main' +import { dz } 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) + let list_id = String(req.query.id) + let list_type = String(req.query.type) + switch (list_type) { + case 'artist': { + const artistAPI = await dz.api.get_artist(list_id) + artistAPI.releases = await dz.gw.get_artist_discography_tabs(list_id, {limit: 100}) + res.send(artistAPI) + break + } + default: { + const releaseAPI = await dz.api[`get_${list_type}`](list_id) + let releaseTracksAPI = await dz.api[`get_${list_type}_tracks`](list_id) + releaseTracksAPI = releaseTracksAPI.data + + const 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 + res.send(releaseAPI) + break + } + } } const apiHandler: ApiHandler = { path, handler } diff --git a/server/src/routes/api/get/index.ts b/server/src/routes/api/get/index.ts index b6aaf89..7fb6a0c 100644 --- a/server/src/routes/api/get/index.ts +++ b/server/src/routes/api/get/index.ts @@ -5,5 +5,15 @@ import search from './search' import getTracklist from './getTracklist' import albumSearch from './albumSearch' import getChartTracks from './getChartTracks' +import getSettings from './getSettings' -export default [albumSearch, getHome, getCharts, getChartTracks, mainSearch, search, getTracklist] +export default [ + albumSearch, + getHome, + getCharts, + getChartTracks, + mainSearch, + search, + getTracklist, + getSettings +] diff --git a/server/src/routes/api/get/mainSearch.ts b/server/src/routes/api/get/mainSearch.ts index 9c9b69a..a402e66 100644 --- a/server/src/routes/api/get/mainSearch.ts +++ b/server/src/routes/api/get/mainSearch.ts @@ -1,11 +1,49 @@ import { ApiHandler } from '../../../types' -import { searchAll } from '../../../main' +import { dz } 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 term = String(req.query.term) + const results = await dz.gw.search(term) + const order: string[] = [] + results.ORDER.forEach((element: string) => { + if (['TOP_RESULT', 'TRACK', 'ALBUM', 'ARTIST', 'PLAYLIST'].includes(element)) order.push(element) + }) + if (results.TOP_RESULT && results.TOP_RESULT.length) { + const originalTopResult = results.TOP_RESULT[0] + const 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 + res.send(results) } const apiHandler: ApiHandler = { path, handler } diff --git a/server/src/routes/api/get/search.ts b/server/src/routes/api/get/search.ts index 08a7bfb..4ac2767 100644 --- a/server/src/routes/api/get/search.ts +++ b/server/src/routes/api/get/search.ts @@ -1,16 +1,37 @@ import { ApiHandler } from '../../../types' -import { search } from '../../../main' +import { dz } 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 term = String(req.query.term) + const type = String(req.query.type) + const start = parseInt(String(req.query.start)) + const nb = parseInt(String(req.query.nb)) + + switch (type) { + case 'track': + res.send( await dz.api.search_track(term, { limit: nb, index: start }) ) + break + case 'album': + res.send( await dz.api.search_album(term, { limit: nb, index: start }) ) + break + case 'artist': + res.send( await dz.api.search_artist(term, { limit: nb, index: start }) ) + break + case 'playlist': + res.send( await dz.api.search_playlist(term, { limit: nb, index: start }) ) + break + case 'radio': + res.send( await dz.api.search_radio(term, { limit: nb, index: start }) ) + break + case 'user': + res.send( await dz.api.search_user(term, { limit: nb, index: start }) ) + break + default: + res.send( await dz.api.search(term, { limit: nb, index: start }) ) + break + } } const apiHandler: ApiHandler = { path, handler } diff --git a/server/yarn.lock b/server/yarn.lock index c1a46dc..c4a3d51 100644 --- a/server/yarn.lock +++ b/server/yarn.lock @@ -1179,6 +1179,11 @@ astral-regex@^2.0.0: resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-2.0.0.tgz#483143c567aeed4785759c0865786dc77d7d2e31" integrity sha512-Z7tMw1ytTXt5jqMcOP+OQteU1VuNK9Y02uuJtKQ1Sv69jXQKKg5cibLwGJow8yzZP+eAc18EmLGPal0bp36rvQ== +async@^3.2.0: + version "3.2.0" + resolved "https://registry.yarnpkg.com/async/-/async-3.2.0.tgz#b3a2685c5ebb641d3de02d161002c60fc9f85720" + integrity sha512-TR2mEZFVOj2pLStYxLht7TyfuRzaydfpxr3k9RpHIzMgw7A64dzsdqCxH1WJyQdoe8T10nDXd9wnEigmiuHIZw== + asynckit@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" @@ -1866,14 +1871,15 @@ decompress-response@^6.0.0: dependencies: mimic-response "^3.1.0" -deemix@0.0.1: - version "0.0.1" - resolved "https://registry.yarnpkg.com/deemix/-/deemix-0.0.1.tgz#5f0fd4802133894dcb39c99a180fa0190d7f54a0" - integrity sha512-Izr64y+b+VMPgByZe1v0kirjaFddX0P6uS4MykgiJXyA7bl6Gc9aMM73eDvJFMbanUmeDgoWOBMUXZ9WYxuh/Q== +deemix@0.0.2: + version "0.0.2" + resolved "https://registry.yarnpkg.com/deemix/-/deemix-0.0.2.tgz#3b375834327d4d6bd1d1db072db976ced86746ee" + integrity sha512-NhTAM2LcoNxit6GiQYlUUO0YaGUMWtN8brGt9HO5mkvbn2cKojF1k9BulmUt/38NoiVvfkqTEtSQD8RtD4qPIA== dependencies: + async "^3.2.0" browser-id3-writer "^4.4.0" crypto "^1.0.1" - deezer-js "^0.0.3" + deezer-js "^0.0.8" got "^11.8.2" metaflac-js2 "^1.0.7" @@ -1892,18 +1898,18 @@ deepmerge@^4.0.0, deepmerge@^4.2.2: resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-4.2.2.tgz#44d2ea3679b8f4d4ffba33f03d865fc1e7bf4955" integrity sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg== -deezer-js@0.0.8: - version "0.0.8" - resolved "https://registry.yarnpkg.com/deezer-js/-/deezer-js-0.0.8.tgz#cadc58eb43bc43dd9a6ec5fdefe5a9f562b8e2bf" - integrity sha512-hOc9C8iRDqw3SS5kF86ss0zWOoPF0C0eg1G+qi8f2gvzNofBzpkuBQ5lCfWYnkKv77w1SQWb4gDtGDYkmkGZuw== +deezer-js@0.0.10: + version "0.0.10" + resolved "https://registry.yarnpkg.com/deezer-js/-/deezer-js-0.0.10.tgz#40aae50082765b7bf93c2c61788306e24cfd39e9" + integrity sha512-V/ZQb3MsouBVHQ7BXQqiWR/5pZh1F9EtZuXGOLa7ZGbRzbN9dVPeYB9dHdr3k8Kf5YUGyp/rXrZtUD25NcZ27g== dependencies: got "^11.8.2" tough-cookie "^4.0.0" -deezer-js@^0.0.3: - version "0.0.3" - resolved "https://registry.yarnpkg.com/deezer-js/-/deezer-js-0.0.3.tgz#6e91c285ca1c2df497c8b42c26a7130f4234f518" - integrity sha512-Nr3ZKZb4NN0onOcFEpUYvSx01Ka7CgJEGgdHcqD9Mlk+KRtMcrWIcmKwheLtHEyZq/W+KTZW3sNG6wPTeWdj+w== +deezer-js@^0.0.8: + version "0.0.8" + resolved "https://registry.yarnpkg.com/deezer-js/-/deezer-js-0.0.8.tgz#cadc58eb43bc43dd9a6ec5fdefe5a9f562b8e2bf" + integrity sha512-hOc9C8iRDqw3SS5kF86ss0zWOoPF0C0eg1G+qi8f2gvzNofBzpkuBQ5lCfWYnkKv77w1SQWb4gDtGDYkmkGZuw== dependencies: got "^11.8.2" tough-cookie "^4.0.0"