171 lines
5.1 KiB
JavaScript
171 lines
5.1 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const axios = require('axios');
|
|
const db = require('../db');
|
|
const camelotToMusicKey = require('../../shared/camelot-to-musickey');
|
|
|
|
const reverseMap = {
|
|
'c': '1B', 'a minor': '1A', 'am': '1A',
|
|
'g': '2B', 'e minor': '2A', 'em': '2A',
|
|
'd': '3B', 'b minor': '3A', 'bm': '3A',
|
|
'a': '4B', 'f# minor': '4A', 'f#m': '4A',
|
|
'e': '5B', 'c# minor': '5A', 'c#m': '5A',
|
|
'b': '6B', 'g# minor': '6A', 'g#m': '6A',
|
|
'f#': '7B', 'd# minor': '7A', 'd#m': '7A',
|
|
'db': '8B', 'c#': '8B', 'bb minor': '8A', 'a# minor': '8A', 'bbm': '8A',
|
|
'ab': '9B', 'g#': '9B', 'f minor': '9A', 'fm': '9A',
|
|
'eb': '10B', 'd#': '10B', 'c minor': '10A', 'cm': '10A',
|
|
'bb': '11B', 'a#': '11B', 'g minor': '11A', 'gm': '11A',
|
|
'f': '12B', 'd minor': '12A', 'dm': '12A',
|
|
'c major': '1B', 'g major': '2B', 'd major': '3B', 'a major': '4B',
|
|
'e major': '5B', 'b major': '6B', 'f# major': '7B', 'db major': '8B',
|
|
'ab major': '9B', 'eb major': '10B', 'bb major': '11B', 'f major': '12B'
|
|
};
|
|
|
|
function standardizeKey(keyString) {
|
|
if (!keyString) return null;
|
|
const str = keyString.toLowerCase().trim();
|
|
return reverseMap[str] || null;
|
|
}
|
|
|
|
router.get('/', async (req, res) => {
|
|
const track = req.query.track;
|
|
const artist = req.query.artist;
|
|
|
|
if (!track || !artist) {
|
|
return res.status(400).json({ error: 'Missing track or artist' });
|
|
}
|
|
|
|
try {
|
|
// 1. Check PostgreSQL first!
|
|
const local = await db.findTrack(artist, track);
|
|
if (local && local.camelot_key && local.bpm) {
|
|
return res.json({
|
|
status: 'success',
|
|
data: {
|
|
key: local.camelot_key,
|
|
bpm: local.bpm,
|
|
music_key: local.music_key || camelotToMusicKey[local.camelot_key] || '',
|
|
tags: local.tags || ['dance'],
|
|
is_corrected: !!local.is_corrected,
|
|
cover_url: local.cover_url || null,
|
|
source: 'postgresql'
|
|
}
|
|
});
|
|
}
|
|
|
|
let bpm = null;
|
|
let camelotKey = null;
|
|
let tags = [];
|
|
let coverUrl = null;
|
|
|
|
// 2. Query GetSongBPM API if key available
|
|
if (process.env.GETSONGBPM_API_KEY) {
|
|
try {
|
|
const gsbRes = await axios.get('https://api.getsong.co/search/', {
|
|
params: {
|
|
api_key: process.env.GETSONGBPM_API_KEY,
|
|
type: 'both',
|
|
lookup: `song:${track} artist:${artist}`
|
|
},
|
|
timeout: 4000
|
|
});
|
|
|
|
const searchResults = gsbRes.data && gsbRes.data.search;
|
|
if (Array.isArray(searchResults) && searchResults.length > 0) {
|
|
const song = searchResults[0];
|
|
if (song.tempo) {
|
|
bpm = parseFloat(song.tempo);
|
|
}
|
|
if (song.key_of) {
|
|
camelotKey = standardizeKey(song.key_of);
|
|
}
|
|
if (song.artist && song.artist.genres && tags.length === 0) {
|
|
tags = song.artist.genres.slice(0, 4);
|
|
}
|
|
}
|
|
} catch (err) {
|
|
console.warn('[Metadata] GetSongBPM Error:', err.message);
|
|
}
|
|
}
|
|
|
|
// 3. Last.fm API for genre tags
|
|
if (process.env.LASTFM_API_KEY) {
|
|
try {
|
|
const lfmRes = await axios.get('https://ws.audioscrobbler.com/2.0/', {
|
|
params: {
|
|
method: 'track.getInfo',
|
|
api_key: process.env.LASTFM_API_KEY,
|
|
artist: artist,
|
|
track: track,
|
|
format: 'json'
|
|
},
|
|
timeout: 4000
|
|
});
|
|
|
|
if (lfmRes.data && lfmRes.data.track && lfmRes.data.track.toptags && lfmRes.data.track.toptags.tag) {
|
|
tags = lfmRes.data.track.toptags.tag
|
|
.slice(0, 4)
|
|
.map(t => t.name.toLowerCase());
|
|
}
|
|
} catch (err) {
|
|
console.warn('[Metadata] Last.fm Error:', err.message);
|
|
}
|
|
}
|
|
|
|
// 4. Query Deezer for cover and genre fallback
|
|
try {
|
|
const deezerRes = await axios.get(`https://api.deezer.com/search?q=${encodeURIComponent(artist + ' ' + track)}&limit=1`, {
|
|
timeout: 3000
|
|
});
|
|
if (deezerRes.data && deezerRes.data.data && deezerRes.data.data[0]) {
|
|
const dTrack = deezerRes.data.data[0];
|
|
if (dTrack.album && dTrack.album.cover_medium) {
|
|
coverUrl = dTrack.album.cover_medium;
|
|
}
|
|
}
|
|
} catch (e) {
|
|
// ignore
|
|
}
|
|
|
|
// Defaults if not found online
|
|
if (!bpm) bpm = 120;
|
|
if (!camelotKey) camelotKey = '8B';
|
|
if (tags.length === 0) tags = ['electronic', 'dance'];
|
|
|
|
const musicKey = camelotToMusicKey[camelotKey] || '';
|
|
|
|
// 5. Persist newly fetched track into PostgreSQL
|
|
await db.saveTrack({
|
|
title: track,
|
|
artist: artist,
|
|
camelot_key: camelotKey,
|
|
bpm: Math.round(bpm),
|
|
music_key: musicKey,
|
|
cover_url: coverUrl,
|
|
tags: tags,
|
|
is_corrected: false,
|
|
source: 'online'
|
|
});
|
|
|
|
res.json({
|
|
status: 'success',
|
|
data: {
|
|
key: camelotKey,
|
|
bpm: Math.round(bpm),
|
|
music_key: musicKey,
|
|
tags: tags,
|
|
is_corrected: false,
|
|
cover_url: coverUrl,
|
|
source: 'online'
|
|
}
|
|
});
|
|
|
|
} catch (err) {
|
|
console.error('[Metadata] error:', err.message);
|
|
res.status(500).json({ error: 'Failed to fetch metadata' });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|