From dfd66e5164c49a780c3511f19f64bb3b0bd51290 Mon Sep 17 00:00:00 2001 From: RemixDev Date: Wed, 5 May 2021 12:06:24 +0200 Subject: [PATCH] Implemented user api paths with session --- server/src/main.ts | 3 +- server/src/routes/api/get/albumSearch.ts | 7 +++- server/src/routes/api/get/getChartTracks.ts | 8 +++- server/src/routes/api/get/getCharts.ts | 9 +++- server/src/routes/api/get/getHome.ts | 9 +++- server/src/routes/api/get/getSettings.ts | 4 +- server/src/routes/api/get/getTracklist.ts | 7 +++- server/src/routes/api/get/getUserAlbums.ts | 29 +++++++++++++ server/src/routes/api/get/getUserArtists.ts | 29 +++++++++++++ server/src/routes/api/get/getUserFavorites.ts | 41 +++++++++++++++++++ server/src/routes/api/get/getUserPlaylists.ts | 29 +++++++++++++ server/src/routes/api/get/getUserTracks.ts | 29 +++++++++++++ server/src/routes/api/get/index.ts | 12 +++++- server/src/routes/api/get/mainSearch.ts | 7 +++- server/src/routes/api/get/search.ts | 7 +++- server/src/routes/api/post/login-arl.ts | 5 ++- server/src/routes/index.ts | 17 ++++++++ 17 files changed, 238 insertions(+), 14 deletions(-) diff --git a/server/src/main.ts b/server/src/main.ts index ab40b0b..6cc942f 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -4,4 +4,5 @@ import { Deezer } from 'deezer-js' import deemix from 'deemix' export let settings: any = deemix.settings.load() -export const dz = new Deezer(settings.tagsLanguage) +export const defaultSettings: any = deemix.settings.DEFAULTS +export let sessionDZ: any = {} diff --git a/server/src/routes/api/get/albumSearch.ts b/server/src/routes/api/get/albumSearch.ts index 7da830c..007cd75 100644 --- a/server/src/routes/api/get/albumSearch.ts +++ b/server/src/routes/api/get/albumSearch.ts @@ -1,6 +1,8 @@ import { RequestHandler } from 'express' import { ApiHandler } from '../../../types' -import { dz } from '../../../main' +// @ts-expect-error +import { Deezer } from 'deezer-js' +import { sessionDZ } from '../../../main' export interface RawAlbumQuery { term: string @@ -23,6 +25,9 @@ export interface AlbumResponse { const path: ApiHandler['path'] = '/album-search/' const handler: RequestHandler<{}, {}, {}, RawAlbumQuery> = async (req, res, next) => { + if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() + let dz = sessionDZ[req.session.id] + if (!req.query) { res.status(400).send() return next() diff --git a/server/src/routes/api/get/getChartTracks.ts b/server/src/routes/api/get/getChartTracks.ts index c9d49dc..cd2c3c3 100644 --- a/server/src/routes/api/get/getChartTracks.ts +++ b/server/src/routes/api/get/getChartTracks.ts @@ -1,6 +1,9 @@ import { RequestHandler } from 'express' import { ApiHandler } from '../../../types' -import { dz } from '../../../main' +// @ts-expect-error +import { Deezer } from 'deezer-js' +import { sessionDZ } from '../../../main' + import { isObjectEmpy } from '../../../helpers/primitive-checks' import { BadRequestError, isBadRequestError, consoleError } from '../../../helpers/errors' @@ -14,6 +17,9 @@ const path: ApiHandler['path'] = '/getChartTracks' const handler: RequestHandler<{}, {}, {}, RawChartTracksQuery> = async (req, res, next) => { try { + if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() + let dz = sessionDZ[req.session.id] + if (isObjectEmpy(req.query) || !req.query.id) { throw new BadRequestError() } diff --git a/server/src/routes/api/get/getCharts.ts b/server/src/routes/api/get/getCharts.ts index 7222906..9079437 100644 --- a/server/src/routes/api/get/getCharts.ts +++ b/server/src/routes/api/get/getCharts.ts @@ -1,12 +1,17 @@ import { ApiHandler } from '../../../types' -import { dz } from '../../../main' +// @ts-expect-error +import { Deezer } from 'deezer-js' +import { sessionDZ } from '../../../main' const path: ApiHandler['path'] = '/getCharts' let chartsCache: any -const handler: ApiHandler['handler'] = async (_, res) => { +const handler: ApiHandler['handler'] = async (req, res) => { if (!chartsCache) { + if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() + let dz = sessionDZ[req.session.id] + const chartsData = await dz.api.get_countries_charts() const countries: any[] = [] chartsData.forEach((country: any) => { diff --git a/server/src/routes/api/get/getHome.ts b/server/src/routes/api/get/getHome.ts index 58974a7..d275d79 100644 --- a/server/src/routes/api/get/getHome.ts +++ b/server/src/routes/api/get/getHome.ts @@ -1,11 +1,16 @@ import { ApiHandler } from '../../../types' -import { dz } from '../../../main' +// @ts-expect-error +import { Deezer } from 'deezer-js' +import { sessionDZ } from '../../../main' const path: ApiHandler['path'] = '/getHome' let homeCache: any -const handler: ApiHandler['handler'] = async (_, res) => { +const handler: ApiHandler['handler'] = async (req, res) => { + if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() + let dz = sessionDZ[req.session.id] + if (!homeCache) { homeCache = await dz.api.get_chart(0, { limit: 30 }) } diff --git a/server/src/routes/api/get/getSettings.ts b/server/src/routes/api/get/getSettings.ts index 7c32cfe..6ee4b1d 100644 --- a/server/src/routes/api/get/getSettings.ts +++ b/server/src/routes/api/get/getSettings.ts @@ -1,10 +1,10 @@ import { ApiHandler } from '../../../types' -import { settings } from '../../../main' +import { settings, defaultSettings } from '../../../main' const path: ApiHandler['path'] = '/getSettings' const handler: ApiHandler['handler'] = async (_, res) => { - res.send(settings) + res.send({settings, defaultSettings}) } const apiHandler: ApiHandler = { path, handler } diff --git a/server/src/routes/api/get/getTracklist.ts b/server/src/routes/api/get/getTracklist.ts index f59c757..e5e3100 100644 --- a/server/src/routes/api/get/getTracklist.ts +++ b/server/src/routes/api/get/getTracklist.ts @@ -1,9 +1,14 @@ import { ApiHandler } from '../../../types' -import { dz } from '../../../main' +// @ts-expect-error +import { Deezer } from 'deezer-js' +import { sessionDZ } from '../../../main' const path: ApiHandler['path'] = '/getTracklist' const handler: ApiHandler['handler'] = async (req, res) => { + if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() + let dz = sessionDZ[req.session.id] + let list_id = String(req.query.id) let list_type = String(req.query.type) switch (list_type) { diff --git a/server/src/routes/api/get/getUserAlbums.ts b/server/src/routes/api/get/getUserAlbums.ts index e69de29..9bc43ed 100644 --- a/server/src/routes/api/get/getUserAlbums.ts +++ b/server/src/routes/api/get/getUserAlbums.ts @@ -0,0 +1,29 @@ +import { ApiHandler } from '../../../types' +// @ts-expect-error +import { Deezer } from 'deezer-js' +import { sessionDZ } from '../../../main' + +const path: ApiHandler['path'] = '/getUserAlbums' + +const handler: ApiHandler['handler'] = async (req, res) => { + if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() + let dz = sessionDZ[req.session.id] + let data + + if (dz.logged_in){ + let userID = dz.current_user.id + try { + data = await dz.api.get_user_albums(userID, {limit: -1}) + data = data.data + } catch { + data = await dz.gw.get_user_albums(userID, {limit: -1}) + } + } else { + data = {error: "notLoggedIn"} + } + res.send(data) +} + +const apiHandler: ApiHandler = { path, handler } + +export default apiHandler diff --git a/server/src/routes/api/get/getUserArtists.ts b/server/src/routes/api/get/getUserArtists.ts index e69de29..6b36105 100644 --- a/server/src/routes/api/get/getUserArtists.ts +++ b/server/src/routes/api/get/getUserArtists.ts @@ -0,0 +1,29 @@ +import { ApiHandler } from '../../../types' +// @ts-expect-error +import { Deezer } from 'deezer-js' +import { sessionDZ } from '../../../main' + +const path: ApiHandler['path'] = '/getUserArtists' + +const handler: ApiHandler['handler'] = async (req, res) => { + if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() + let dz = sessionDZ[req.session.id] + let data + + if (dz.logged_in){ + let userID = dz.current_user.id + try { + data = await dz.api.get_user_artists(userID, {limit: -1}) + data = data.data + } catch { + data = await dz.gw.get_user_artists(userID, {limit: -1}) + } + } else { + data = {error: "notLoggedIn"} + } + res.send(data) +} + +const apiHandler: ApiHandler = { path, handler } + +export default apiHandler diff --git a/server/src/routes/api/get/getUserFavorites.ts b/server/src/routes/api/get/getUserFavorites.ts index e69de29..fb4ff73 100644 --- a/server/src/routes/api/get/getUserFavorites.ts +++ b/server/src/routes/api/get/getUserFavorites.ts @@ -0,0 +1,41 @@ +import { ApiHandler } from '../../../types' +// @ts-expect-error +import { Deezer } from 'deezer-js' +import { sessionDZ } from '../../../main' + +const path: ApiHandler['path'] = '/getUserFavorites' + +const handler: ApiHandler['handler'] = async (req, res) => { + if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() + let dz = sessionDZ[req.session.id] + + let result: any = {} + + if (dz.logged_in){ + let userID = dz.current_user.id + + try { + let data + data = await dz.api.get_user_playlists(userID, {limit: -1}) + result['playlists'] = data.data + data = await dz.api.get_user_playlists(userID, {limit: -1}) + result['playlists'] = data.data + data = await dz.api.get_user_playlists(userID, {limit: -1}) + result['playlists'] = data.data + data = await dz.api.get_user_playlists(userID, {limit: -1}) + result['playlists'] = data.data + } catch { + result['playlists'] = await dz.gw.get_user_playlists(userID, {limit: -1}) + result['albums'] = await dz.gw.get_user_albums(userID, {limit: -1}) + result['artists'] = await dz.gw.get_user_artists(userID, {limit: -1}) + result['tracks'] = await dz.gw.get_user_tracks(userID, {limit: -1}) + } + } else { + result = {error: "notLoggedIn"} + } + res.send(result) +} + +const apiHandler: ApiHandler = { path, handler } + +export default apiHandler diff --git a/server/src/routes/api/get/getUserPlaylists.ts b/server/src/routes/api/get/getUserPlaylists.ts index e69de29..f3c0360 100644 --- a/server/src/routes/api/get/getUserPlaylists.ts +++ b/server/src/routes/api/get/getUserPlaylists.ts @@ -0,0 +1,29 @@ +import { ApiHandler } from '../../../types' +// @ts-expect-error +import { Deezer } from 'deezer-js' +import { sessionDZ } from '../../../main' + +const path: ApiHandler['path'] = '/getUserPlaylists' + +const handler: ApiHandler['handler'] = async (req, res) => { + if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() + let dz = sessionDZ[req.session.id] + let data + + if (dz.logged_in){ + let userID = dz.current_user.id + try { + data = await dz.api.get_user_playlists(userID, {limit: -1}) + data = data.data + } catch { + data = await dz.gw.get_user_playlists(userID, {limit: -1}) + } + } else { + data = {error: "notLoggedIn"} + } + res.send(data) +} + +const apiHandler: ApiHandler = { path, handler } + +export default apiHandler diff --git a/server/src/routes/api/get/getUserTracks.ts b/server/src/routes/api/get/getUserTracks.ts index e69de29..4eea7f1 100644 --- a/server/src/routes/api/get/getUserTracks.ts +++ b/server/src/routes/api/get/getUserTracks.ts @@ -0,0 +1,29 @@ +import { ApiHandler } from '../../../types' +// @ts-expect-error +import { Deezer } from 'deezer-js' +import { sessionDZ } from '../../../main' + +const path: ApiHandler['path'] = '/getUserTracks' + +const handler: ApiHandler['handler'] = async (req, res) => { + if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() + let dz = sessionDZ[req.session.id] + let data + + if (dz.logged_in){ + let userID = dz.current_user.id + try { + data = await dz.api.get_user_tracks(userID, {limit: -1}) + data = data.data + } catch { + data = await dz.gw.get_user_tracks(userID, {limit: -1}) + } + } else { + data = {error: "notLoggedIn"} + } + res.send(data) +} + +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 7fb6a0c..b11c692 100644 --- a/server/src/routes/api/get/index.ts +++ b/server/src/routes/api/get/index.ts @@ -6,6 +6,11 @@ import getTracklist from './getTracklist' import albumSearch from './albumSearch' import getChartTracks from './getChartTracks' import getSettings from './getSettings' +import getUserTracks from './getUserTracks' +import getUserAlbums from './getUserAlbums' +import getUserArtists from './getUserArtists' +import getUserPlaylists from './getUserPlaylists' +import getUserFavorites from './getUserFavorites' export default [ albumSearch, @@ -15,5 +20,10 @@ export default [ mainSearch, search, getTracklist, - getSettings + getSettings, + getUserTracks, + getUserAlbums, + getUserArtists, + getUserPlaylists, + getUserFavorites ] diff --git a/server/src/routes/api/get/mainSearch.ts b/server/src/routes/api/get/mainSearch.ts index a402e66..32ba3dd 100644 --- a/server/src/routes/api/get/mainSearch.ts +++ b/server/src/routes/api/get/mainSearch.ts @@ -1,9 +1,14 @@ import { ApiHandler } from '../../../types' -import { dz } from '../../../main' +// @ts-expect-error +import { Deezer } from 'deezer-js' +import { sessionDZ } from '../../../main' const path: ApiHandler['path'] = '/mainSearch' const handler: ApiHandler['handler'] = async (req, res) => { + if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() + let dz = sessionDZ[req.session.id] + const term = String(req.query.term) const results = await dz.gw.search(term) const order: string[] = [] diff --git a/server/src/routes/api/get/search.ts b/server/src/routes/api/get/search.ts index 4ac2767..3934515 100644 --- a/server/src/routes/api/get/search.ts +++ b/server/src/routes/api/get/search.ts @@ -1,9 +1,14 @@ import { ApiHandler } from '../../../types' -import { dz } from '../../../main' +// @ts-expect-error +import { Deezer } from 'deezer-js' +import { sessionDZ } from '../../../main' const path: ApiHandler['path'] = '/search' const handler: ApiHandler['handler'] = async (req, res) => { + if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() + let dz = sessionDZ[req.session.id] + const term = String(req.query.term) const type = String(req.query.type) const start = parseInt(String(req.query.start)) diff --git a/server/src/routes/api/post/login-arl.ts b/server/src/routes/api/post/login-arl.ts index f72e7c3..c2c5996 100644 --- a/server/src/routes/api/post/login-arl.ts +++ b/server/src/routes/api/post/login-arl.ts @@ -1,8 +1,8 @@ import { RequestHandler } from 'express' // @ts-expect-error import { Deezer } from 'deezer-js' +import { sessionDZ } from '../../../main' import { ApiHandler } from '../../../types' -import { dz } from '../../../main' export interface RawLoginArlQuery { arl: string @@ -12,6 +12,9 @@ export interface RawLoginArlQuery { const path: ApiHandler['path'] = '/login-arl/' const handler: RequestHandler<{}, {}, {}, RawLoginArlQuery> = async (req, res, next) => { + if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() + let dz = sessionDZ[req.session.id] + if (!req.query) { res.status(400).send() return next() diff --git a/server/src/routes/index.ts b/server/src/routes/index.ts index 7adbdd1..e21bfa2 100644 --- a/server/src/routes/index.ts +++ b/server/src/routes/index.ts @@ -1,4 +1,7 @@ import express from 'express' +// @ts-expect-error +import { Deezer } from 'deezer-js' +import { sessionDZ } from '../main' const router = express.Router() @@ -11,4 +14,18 @@ router.get('/', (_, res) => { res.render('index', { title: 'deemix' }) }) +router.get('/connect', (req, res) => { + if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() + res.send({ + update: { + currentCommit: "testing", + latestCommit: "testing", + updateAvailable: false, + deemixVersion: "3.0_beta" + }, + autologin: true, + deezerNotAvailable: false + }) +}) + export default router