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'
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 { 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()

View file

@ -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()
}

View file

@ -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) => {

View file

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

View file

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

View file

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

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

View file

@ -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[] = []

View file

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

View file

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

View file

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