Extra endpoints for lidarr

This commit is contained in:
kermit 2021-07-02 18:57:52 +01:00
parent 698ef1f9df
commit a903ba6444
4 changed files with 103 additions and 15 deletions

2
server/dist/app.js vendored

File diff suppressed because one or more lines are too long

View file

@ -9,7 +9,6 @@ export interface RawAlbumQuery {
term: string
start?: string
nb?: string
ack: number
}
export interface AlbumSearchParams extends Omit<RawAlbumQuery, 'start' | 'nb'> {
@ -20,7 +19,6 @@ export interface AlbumSearchParams extends Omit<RawAlbumQuery, 'start' | 'nb'> {
export interface AlbumResponse {
data: any[]
total: number
ack: RawAlbumQuery['ack']
}
const path: ApiHandler['path'] = '/album-search/'
@ -33,18 +31,19 @@ const handler: RequestHandler<{}, {}, {}, RawAlbumQuery> = async (req, res) => {
return res.status(400).send()
}
const { term, start, nb, ack } = parseQuery(req.query)
const { term, start, nb } = parseQuery(req.query)
if (!term || term.trim() === '') {
return res.status(400).send()
}
const albums = await dz.api.search_album(term, { start, nb })
const results = await dz.gw.search_music(term, 'ALBUM', { index: start, limit: nb })
const albums = await Promise.all(results.data.map((c: any) => getAlbumDetails(dz, c.ALB_ID)))
const output: AlbumResponse = {
data: albums,
total: albums.data.length,
ack
total: albums.length
}
return res.send(output)
@ -52,8 +51,6 @@ const handler: RequestHandler<{}, {}, {}, RawAlbumQuery> = async (req, res) => {
const apiHandler = { path, handler }
export default apiHandler
function parseQuery(query: RawAlbumQuery): AlbumSearchParams {
let startingPoint = 0
@ -70,11 +67,26 @@ function parseQuery(query: RawAlbumQuery): AlbumSearchParams {
return {
term: query.term,
start: startingPoint,
nb: newNb,
ack: query.ack
nb: newNb
}
}
// function getAlbums(term: string, start: number, nb: number): any[] {
// return []
// }
async function getAlbumDetails(dz: any, albumId: string): Promise<any> {
const result = await dz.gw.get_album_page(albumId)
const output = result.DATA
let duration = 0
result.SONGS.data.forEach((s: any) => {
if ('DURATION' in s) {
duration += parseInt(s.DURATION)
}
})
output.DURATION = duration
output.NUMBER_TRACK = result.SONGS.total
output.LINK = `https://deezer.com/album/${output.ALB_ID}`
return output
}
export { apiHandler, getAlbumDetails }

View file

@ -4,8 +4,9 @@ import getHome from './getHome'
import getCharts from './getCharts'
import mainSearch from './mainSearch'
import search from './search'
import newReleases from './newReleases'
import getTracklist from './getTracklist'
import albumSearch from './albumSearch'
import { apiHandler as albumSearch } from './albumSearch'
import getChartTracks from './getChartTracks'
import getSettings from './getSettings'
import getUserTracks from './getUserTracks'
@ -25,6 +26,7 @@ export default [
getChartTracks,
mainSearch,
search,
newReleases,
getTracklist,
getSettings,
getUserTracks,

View file

@ -0,0 +1,74 @@
// @ts-expect-error
import { Deezer } from 'deezer-js'
import { ApiHandler } from '../../../types'
import { sessionDZ } from '../../../main'
import { getAlbumDetails } from './albumSearch'
const path: ApiHandler['path'] = '/newReleases'
const handler: ApiHandler['handler'] = async (req, res) => {
if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer()
const dz = sessionDZ[req.session.id]
const results = await dz.gw.get_page('channels/explore')
const music_section = results.sections.find((e: any) =>
e.section_id.includes('module_id=83718b7b-5503-4062-b8b9-3530e2e2cefa')
)
const channels = music_section.items.map((e: any) => e.target)
const newReleasesByChannel = <any[][]>await Promise.all(channels.map((c: string) => channelNewReleases(dz, c)))
const seen = new Set()
const distinct: any[] = []
newReleasesByChannel.forEach(l => {
l.forEach(r => {
if (!seen.has(r.ALB_ID)) {
seen.add(r.ALB_ID)
distinct.push(r)
}
})
})
distinct.sort((a, b) =>
a.DIGITAL_RELEASE_DATE < b.DIGITAL_RELEASE_DATE ? 1 : b.DIGITAL_RELEASE_DATE < a.DIGITAL_RELEASE_DATE ? -1 : 0
)
const now = Date.now()
const delta = 8 * 24 * 60 * 60 * 1000
const recent = distinct.filter((x: any) => now - Date.parse(x.DIGITAL_RELEASE_DATE) < delta)
const albums = await Promise.all(recent.map((c: any) => getAlbumDetails(dz, c.ALB_ID)))
const output = {
data: albums,
total: albums.length
}
return res.send(output)
}
const apiHandler: ApiHandler = { path, handler }
export default apiHandler
async function channelNewReleases(dz: any, channelName: string): Promise<any[]> {
const channelData = await dz.gw.get_page(channelName)
const re = /^New.*releases$/
const newReleases = channelData.sections.find((e: any) => re.test(e.title))
if (!newReleases) {
return []
} else if ('target' in newReleases) {
const showAll = await dz.gw.get_page(newReleases.target)
return showAll.sections[0].items.map((e: any) => e.data)
} else if ('items' in newReleases) {
return newReleases.items.map((e: any) => e.data)
} else {
return []
}
}