Add Spotify search type filter for album/track/artist with album-based Lidarr flow
This commit is contained in:
parent
b3a343c3a0
commit
abf9d61d72
4 changed files with 167 additions and 40 deletions
74
server.js
74
server.js
|
|
@ -247,28 +247,82 @@ app.get('/api/health', (_req, res) => {
|
|||
|
||||
app.get('/api/spotify/search', async (req, res) => {
|
||||
const q = String(req.query.q || '').trim();
|
||||
const type = String(req.query.type || 'album').trim();
|
||||
const allowedTypes = new Set(['album', 'track', 'artist']);
|
||||
|
||||
if (!q) {
|
||||
return res.status(400).json({ error: 'Suchbegriff fehlt.' });
|
||||
}
|
||||
if (!allowedTypes.has(type)) {
|
||||
return res.status(400).json({ error: 'Ungueltiger Suchtyp. Erlaubt: album, track, artist.' });
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await spotifyRequest('/search', {
|
||||
q,
|
||||
type: 'album',
|
||||
type,
|
||||
limit: 20,
|
||||
market: 'DE'
|
||||
});
|
||||
|
||||
const albums = (data.albums?.items || []).map((album) => ({
|
||||
id: album.id,
|
||||
name: album.name,
|
||||
artist: album.artists?.map((a) => a.name).join(', ') || 'Unbekannt',
|
||||
image: album.images?.[1]?.url || album.images?.[0]?.url || null,
|
||||
releaseDate: album.release_date,
|
||||
totalTracks: album.total_tracks
|
||||
}));
|
||||
let items = [];
|
||||
if (type === 'album') {
|
||||
items = (data.albums?.items || []).map((album) => ({
|
||||
id: album.id,
|
||||
name: album.name,
|
||||
artist: album.artists?.map((a) => a.name).join(', ') || 'Unbekannt',
|
||||
image: album.images?.[1]?.url || album.images?.[0]?.url || null,
|
||||
releaseDate: album.release_date,
|
||||
totalTracks: album.total_tracks
|
||||
}));
|
||||
}
|
||||
if (type === 'track') {
|
||||
items = (data.tracks?.items || []).map((track) => ({
|
||||
id: track.id,
|
||||
trackName: track.name,
|
||||
artist: track.artists?.map((a) => a.name).join(', ') || 'Unbekannt',
|
||||
albumId: track.album?.id,
|
||||
albumName: track.album?.name || 'Unbekannt',
|
||||
image: track.album?.images?.[1]?.url || track.album?.images?.[0]?.url || null
|
||||
}));
|
||||
}
|
||||
if (type === 'artist') {
|
||||
items = (data.artists?.items || []).map((artist) => ({
|
||||
id: artist.id,
|
||||
name: artist.name,
|
||||
followers: artist.followers?.total || 0,
|
||||
image: artist.images?.[1]?.url || artist.images?.[0]?.url || null
|
||||
}));
|
||||
}
|
||||
|
||||
res.json({ albums });
|
||||
res.json({ items });
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.response?.data?.error?.message || err.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/spotify/artist/:id/albums', async (req, res) => {
|
||||
try {
|
||||
const data = await spotifyRequest(`/artists/${req.params.id}/albums`, {
|
||||
include_groups: 'album,single',
|
||||
limit: 50,
|
||||
market: 'DE'
|
||||
});
|
||||
|
||||
const unique = new Map();
|
||||
for (const album of data.items || []) {
|
||||
if (unique.has(album.id)) continue;
|
||||
unique.set(album.id, {
|
||||
id: album.id,
|
||||
name: album.name,
|
||||
artist: album.artists?.map((a) => a.name).join(', ') || 'Unbekannt',
|
||||
image: album.images?.[1]?.url || album.images?.[0]?.url || null,
|
||||
releaseDate: album.release_date,
|
||||
totalTracks: album.total_tracks
|
||||
});
|
||||
}
|
||||
|
||||
res.json({ albums: Array.from(unique.values()) });
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.response?.data?.error?.message || err.message });
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue