refactor: dedup buildTrackList, extract topsong row helper, split romaji to lib

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
HoneyandClaude Sonnet 4.6 committed 2026-05-21 11:36:06 +02:00
1 parent 84f1fd006d
commit b9a5c6468f
4 files changed
+162 -171

No files matched your search

+5 -118
View File
@@ -10,7 +10,6 @@
const express = require('express');
const { execSync } = require('child_process');
const { createHash } = require('crypto');
const path = require('path');
const fs = require('fs');
const https = require('https');
@@ -19,100 +18,7 @@ const CONFIG = require('./lib/config');
const { jobs, updateJob, log } = require('./lib/jobs');
const { lookupItunesTrackMeta, fetchItunesTracks, searchItunesAlbumTracks } = require('./lib/metadata');
const { downloadTrack, downloadAlbumTracks } = require('./lib/downloader');
// ─── ROMAJI INDEX ─────────────────────────────────────────────────────────────
// Converts kanji/kana → romaji so users can search Japanese songs phonetically.
let _tokenizer = null;
try {
const kuromoji = require('kuromoji');
const dicPath = path.join(path.dirname(require.resolve('kuromoji')), '..', 'dict');
kuromoji.builder({ dicPath })
.build((err, t) => {
if (err) console.warn('⚠️ kuromoji failed to load:', err.message);
else { _tokenizer = t; console.log('✅ kuromoji tokenizer ready'); }
});
} catch { console.warn('⚠️ kuromoji not installed — run: cd backend && npm install'); }
// katakana→hiragana shift (kuromoji readings come back in katakana)
function _kataToHira(s) { return s.replace(/[ァ-ヶ]/g, c => String.fromCharCode(c.charCodeAt(0) - 0x60)); }
// Full hiragana → romaji table (includes digraphs + っ doubling)
const _H2R = {
'あ':'a','い':'i','う':'u','え':'e','お':'o',
'か':'ka','き':'ki','く':'ku','け':'ke','こ':'ko',
'さ':'sa','し':'shi','す':'su','せ':'se','そ':'so',
'た':'ta','ち':'chi','つ':'tsu','て':'te','と':'to',
'な':'na','に':'ni','ぬ':'nu','ね':'ne','の':'no',
'は':'ha','ひ':'hi','ふ':'fu','へ':'he','ほ':'ho',
'ま':'ma','み':'mi','む':'mu','め':'me','も':'mo',
'や':'ya','ゆ':'yu','よ':'yo',
'ら':'ra','り':'ri','る':'ru','れ':'re','ろ':'ro',
'わ':'wa','を':'wo','ん':'n',
'が':'ga','ぎ':'gi','ぐ':'gu','げ':'ge','ご':'go',
'ざ':'za','じ':'ji','ず':'zu','ぜ':'ze','ぞ':'zo',
'だ':'da','ぢ':'di','づ':'du','で':'de','ど':'do',
'ば':'ba','び':'bi','ぶ':'bu','べ':'be','ぼ':'bo',
'ぱ':'pa','ぴ':'pi','ぷ':'pu','ぺ':'pe','ぽ':'po',
'きゃ':'kya','きゅ':'kyu','きょ':'kyo',
'しゃ':'sha','しゅ':'shu','しょ':'sho',
'ちゃ':'cha','ちゅ':'chu','ちょ':'cho',
'にゃ':'nya','にゅ':'nyu','にょ':'nyo',
'ひゃ':'hya','ひゅ':'hyu','ひょ':'hyo',
'みゃ':'mya','みゅ':'myu','みょ':'myo',
'りゃ':'rya','りゅ':'ryu','りょ':'ryo',
'ぎゃ':'gya','ぎゅ':'gyu','ぎょ':'gyo',
'じゃ':'ja','じゅ':'ju','じょ':'jo',
'びゃ':'bya','びゅ':'byu','びょ':'byo',
'ぴゃ':'pya','ぴゅ':'pyu','ぴょ':'pyo',
'ふぁ':'fa','ふぃ':'fi','ふぇ':'fe','ふぉ':'fo',
'てぃ':'ti','でぃ':'di','とぅ':'tu','どぅ':'du',
'ゐ':'i','ゑ':'e','ヴ':'vu',
};
function _hiraToRomaji(str) {
let out = '', i = 0;
while (i < str.length) {
if (str[i] === 'っ') {
const nxt = _H2R[str.slice(i+1,i+3)] || _H2R[str[i+1]] || '';
out += nxt[0] || ''; i++; continue;
}
const two = _H2R[str.slice(i,i+2)];
if (two) { out += two; i += 2; continue; }
out += _H2R[str[i]] ?? str[i]; i++;
}
return out;
}
const JP_RE = /[぀-ヿ㐀-䶿一-鿿＀-￯]/;
function _toRomaji(text) {
if (!text || !_tokenizer || !JP_RE.test(text)) return text || '';
const tokens = _tokenizer.tokenize(text);
const hira = tokens.map(t => {
const r = t.reading;
return (r && r !== '*') ? _kataToHira(r) : t.surface_form;
}).join('');
return _hiraToRomaji(hira).toLowerCase();
}
// Cache: key = "ndUrl|user" → { ts, index }
const _romajiCache = new Map();
const ROMAJI_TTL = 20 * 60 * 1000; // 20 min
async function _fetchNdSongs(ndUrl, user, pass) {
const salt = Math.random().toString(36).slice(2, 10);
const token = createHash('md5').update(pass + salt).digest('hex');
const base = `${ndUrl}/rest/search3.view?u=${encodeURIComponent(user)}&t=${token}&s=${salt}&v=1.16.1&c=novasonic&f=json&albumCount=0&artistCount=0`;
const songs = [];
let offset = 0;
while (true) {
const r = await fetch(`${base}&query=&songCount=500&songOffset=${offset}`).then(r => r.json());
const page = r['subsonic-response']?.searchResult3?.song || [];
songs.push(...page);
if (page.length < 500) break;
offset += 500;
}
return songs;
}
const { buildRomajiIndex, clearRomajiCache, isTokenizerReady } = require('./lib/romaji');
// ─── INIT ─────────────────────────────────────────────────────────────────────
const app = express();
@@ -169,29 +75,10 @@ function parseSpotifyUrl(url) {
app.get('/api/romaji-index', async (req, res) => {
const { ndUrl, user, pass } = req.query;
if (!ndUrl || !user || !pass) return res.status(400).json({ error: 'missing params' });
if (!_tokenizer) return res.status(503).json({ error: 'tokenizer not ready — try again in a few seconds' });
const key = `${ndUrl}|${user}`;
const cached = _romajiCache.get(key);
if (cached && Date.now() - cached.ts < ROMAJI_TTL) return res.json(cached.index);
if (!isTokenizerReady()) return res.status(503).json({ error: 'tokenizer not ready — try again in a few seconds' });
try {
const songs = await _fetchNdSongs(ndUrl, user, pass);
const index = songs
.filter(s => JP_RE.test(s.title) || JP_RE.test(s.artist) || JP_RE.test(s.album))
.map(s => ({
id: s.id,
r: [_toRomaji(s.title), _toRomaji(s.artist), _toRomaji(s.album)].join(' '),
title: s.title,
artist: s.artist || '',
album: s.album || '',
coverArt: s.coverArt || '',
duration: s.duration || 0,
albumId: s.albumId || '',
artistId: s.artistId || '',
}));
_romajiCache.set(key, { ts: Date.now(), index });
console.log(`✅ Romaji index built: ${index.length} JP songs (${songs.length} total)`);
const { fromCache, index } = await buildRomajiIndex(ndUrl, user, pass);
if (!fromCache) console.log(`✅ Romaji index built: ${index.length} JP songs`);
res.json(index);
} catch (e) {
console.error('Romaji index error:', e);
@@ -201,7 +88,7 @@ app.get('/api/romaji-index', async (req, res) => {
// DELETE /api/romaji-index — force rebuild on next request
app.delete('/api/romaji-index', (req, res) => {
_romajiCache.clear();
clearRomajiCache();
res.json({ ok: true });
});