Implemented user api paths with session

This commit is contained in:
RemixDev 2021-05-05 12:06:24 +02:00
parent 85fcc70948
commit dfd66e5164
17 changed files with 238 additions and 14 deletions

View File

@ -4,4 +4,5 @@ import { Deezer } from 'deezer-js'
import deemix from 'deemix' import deemix from 'deemix'
export let settings: any = deemix.settings.load() 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 = {}

View File

@ -1,6 +1,8 @@
import { RequestHandler } from 'express' import { RequestHandler } from 'express'
import { ApiHandler } from '../../../types' import { ApiHandler } from '../../../types'
import { dz } from '../../../main' // @ts-expect-error
import { Deezer } from 'deezer-js'
import { sessionDZ } from '../../../main'
export interface RawAlbumQuery { export interface RawAlbumQuery {
term: string term: string
@ -23,6 +25,9 @@ export interface AlbumResponse {
const path: ApiHandler['path'] = '/album-search/' const path: ApiHandler['path'] = '/album-search/'
const handler: RequestHandler<{}, {}, {}, RawAlbumQuery> = async (req, res, next) => { 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) { if (!req.query) {
res.status(400).send() res.status(400).send()
return next() return next()

View File

@ -1,6 +1,9 @@
import { RequestHandler } from 'express' import { RequestHandler } from 'express'
import { ApiHandler } from '../../../types' 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 { isObjectEmpy } from '../../../helpers/primitive-checks'
import { BadRequestError, isBadRequestError, consoleError } from '../../../helpers/errors' import { BadRequestError, isBadRequestError, consoleError } from '../../../helpers/errors'
@ -14,6 +17,9 @@ const path: ApiHandler['path'] = '/getChartTracks'
const handler: RequestHandler<{}, {}, {}, RawChartTracksQuery> = async (req, res, next) => { const handler: RequestHandler<{}, {}, {}, RawChartTracksQuery> = async (req, res, next) => {
try { 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) { if (isObjectEmpy(req.query) || !req.query.id) {
throw new BadRequestError() throw new BadRequestError()
} }

View File

@ -1,12 +1,17 @@
import { ApiHandler } from '../../../types' 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' const path: ApiHandler['path'] = '/getCharts'
let chartsCache: any let chartsCache: any
const handler: ApiHandler['handler'] = async (_, res) => { const handler: ApiHandler['handler'] = async (req, res) => {
if (!chartsCache) { 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 chartsData = await dz.api.get_countries_charts()
const countries: any[] = [] const countries: any[] = []
chartsData.forEach((country: any) => { chartsData.forEach((country: any) => {

View File

@ -1,11 +1,16 @@
import { ApiHandler } from '../../../types' 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' const path: ApiHandler['path'] = '/getHome'
let homeCache: any 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) { if (!homeCache) {
homeCache = await dz.api.get_chart(0, { limit: 30 }) homeCache = await dz.api.get_chart(0, { limit: 30 })
} }

View File

@ -1,10 +1,10 @@
import { ApiHandler } from '../../../types' import { ApiHandler } from '../../../types'
import { settings } from '../../../main' import { settings, defaultSettings } from '../../../main'
const path: ApiHandler['path'] = '/getSettings' const path: ApiHandler['path'] = '/getSettings'
const handler: ApiHandler['handler'] = async (_, res) => { const handler: ApiHandler['handler'] = async (_, res) => {
res.send(settings) res.send({settings, defaultSettings})
} }
const apiHandler: ApiHandler = { path, handler } const apiHandler: ApiHandler = { path, handler }

View File

@ -1,9 +1,14 @@
import { ApiHandler } from '../../../types' 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 path: ApiHandler['path'] = '/getTracklist'
const handler: ApiHandler['handler'] = async (req, 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]
let list_id = String(req.query.id) let list_id = String(req.query.id)
let list_type = String(req.query.type) let list_type = String(req.query.type)
switch (list_type) { switch (list_type) {

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -6,6 +6,11 @@ import getTracklist from './getTracklist'
import albumSearch from './albumSearch' import albumSearch from './albumSearch'
import getChartTracks from './getChartTracks' import getChartTracks from './getChartTracks'
import getSettings from './getSettings' 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 [ export default [
albumSearch, albumSearch,
@ -15,5 +20,10 @@ export default [
mainSearch, mainSearch,
search, search,
getTracklist, getTracklist,
getSettings getSettings,
getUserTracks,
getUserAlbums,
getUserArtists,
getUserPlaylists,
getUserFavorites
] ]

View File

@ -1,9 +1,14 @@
import { ApiHandler } from '../../../types' 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 path: ApiHandler['path'] = '/mainSearch'
const handler: ApiHandler['handler'] = async (req, 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]
const term = String(req.query.term) const term = String(req.query.term)
const results = await dz.gw.search(term) const results = await dz.gw.search(term)
const order: string[] = [] const order: string[] = []

View File

@ -1,9 +1,14 @@
import { ApiHandler } from '../../../types' 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 path: ApiHandler['path'] = '/search'
const handler: ApiHandler['handler'] = async (req, 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]
const term = String(req.query.term) const term = String(req.query.term)
const type = String(req.query.type) const type = String(req.query.type)
const start = parseInt(String(req.query.start)) const start = parseInt(String(req.query.start))

View File

@ -1,8 +1,8 @@
import { RequestHandler } from 'express' import { RequestHandler } from 'express'
// @ts-expect-error // @ts-expect-error
import { Deezer } from 'deezer-js' import { Deezer } from 'deezer-js'
import { sessionDZ } from '../../../main'
import { ApiHandler } from '../../../types' import { ApiHandler } from '../../../types'
import { dz } from '../../../main'
export interface RawLoginArlQuery { export interface RawLoginArlQuery {
arl: string arl: string
@ -12,6 +12,9 @@ export interface RawLoginArlQuery {
const path: ApiHandler['path'] = '/login-arl/' const path: ApiHandler['path'] = '/login-arl/'
const handler: RequestHandler<{}, {}, {}, RawLoginArlQuery> = async (req, res, next) => { 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) { if (!req.query) {
res.status(400).send() res.status(400).send()
return next() return next()

View File

@ -1,4 +1,7 @@
import express from 'express' import express from 'express'
// @ts-expect-error
import { Deezer } from 'deezer-js'
import { sessionDZ } from '../main'
const router = express.Router() const router = express.Router()
@ -11,4 +14,18 @@ router.get('/', (_, res) => {
res.render('index', { title: 'deemix' }) 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 export default router