feat: remove iTunes release year refresh API endpoint and UI from admin page
This commit is contained in:
@@ -1718,73 +1718,7 @@ export default function AdminPage() {
|
|||||||
☢️ Rebuild Database
|
☢️ Rebuild Database
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
<div style={{ marginTop: '1rem', borderTop: '1px solid #eee', paddingTop: '1rem' }}>
|
|
||||||
<p style={{ marginBottom: '1rem', color: '#666' }}>
|
|
||||||
Update release years for all songs using the iTunes API. This will overwrite existing years.
|
|
||||||
</p>
|
|
||||||
<button
|
|
||||||
onClick={async () => {
|
|
||||||
if (window.confirm('This will scan all songs and overwrite their release years using data from iTunes. This process may take a while.\n\nContinue?')) {
|
|
||||||
try {
|
|
||||||
let offset = 0;
|
|
||||||
let hasMore = true;
|
|
||||||
let totalUpdated = 0;
|
|
||||||
let totalSkipped = 0;
|
|
||||||
let totalFailed = 0;
|
|
||||||
let totalProcessed = 0;
|
|
||||||
let totalSongs = 0;
|
|
||||||
|
|
||||||
setMessage('Initializing release year refresh...');
|
|
||||||
|
|
||||||
while (hasMore) {
|
|
||||||
const res = await fetch('/api/admin/refresh-years', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: getAuthHeaders(),
|
|
||||||
body: JSON.stringify({ offset, limit: 10 }) // Process 10 at a time
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!res.ok) {
|
|
||||||
throw new Error('Batch request failed');
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await res.json();
|
|
||||||
|
|
||||||
totalUpdated += data.updated;
|
|
||||||
totalSkipped += data.skipped;
|
|
||||||
totalFailed += data.failed;
|
|
||||||
totalProcessed += data.processed;
|
|
||||||
totalSongs = data.total;
|
|
||||||
hasMore = data.hasMore;
|
|
||||||
offset = data.nextOffset;
|
|
||||||
|
|
||||||
setMessage(`Processing... ${totalProcessed} / ${totalSongs} songs.\nUpdated: ${totalUpdated} | Skipped: ${totalSkipped} | Failed: ${totalFailed}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const finalMsg = `✅ Completed!\nTotal Processed: ${totalProcessed}\nUpdated: ${totalUpdated}\nSkipped: ${totalSkipped}\nFailed: ${totalFailed}`;
|
|
||||||
alert(finalMsg);
|
|
||||||
setMessage(finalMsg);
|
|
||||||
fetchSongs(); // Refresh the table
|
|
||||||
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
alert('Process failed due to network error or timeout.');
|
|
||||||
setMessage('Refresh failed.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
style={{
|
|
||||||
padding: '0.75rem 1.5rem',
|
|
||||||
background: '#f59e0b',
|
|
||||||
color: 'white',
|
|
||||||
border: 'none',
|
|
||||||
borderRadius: '0.25rem',
|
|
||||||
cursor: 'pointer',
|
|
||||||
fontWeight: 'bold'
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
🔄 Refresh Release Years (iTunes)
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,78 +0,0 @@
|
|||||||
|
|
||||||
import { NextResponse } from 'next/server';
|
|
||||||
import { PrismaClient } from '@prisma/client';
|
|
||||||
import { requireAdminAuth } from '@/lib/auth';
|
|
||||||
import { getReleaseYearFromItunes } from '@/lib/itunes';
|
|
||||||
|
|
||||||
const prisma = new PrismaClient();
|
|
||||||
|
|
||||||
// Helper to delay execution to avoid rate limits
|
|
||||||
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
// Check authentication
|
|
||||||
const authError = await requireAdminAuth(request as any);
|
|
||||||
if (authError) return authError;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const { offset = 0, limit = 20 } = await request.json();
|
|
||||||
|
|
||||||
// Fetch batch of songs
|
|
||||||
const songs = await prisma.song.findMany({
|
|
||||||
select: { id: true, title: true, artist: true },
|
|
||||||
orderBy: { id: 'asc' },
|
|
||||||
skip: offset,
|
|
||||||
take: limit
|
|
||||||
});
|
|
||||||
|
|
||||||
const totalSongs = await prisma.song.count();
|
|
||||||
|
|
||||||
console.log(`Processing batch: offset=${offset}, limit=${limit}, found=${songs.length}`);
|
|
||||||
|
|
||||||
let updatedCount = 0;
|
|
||||||
let failedCount = 0;
|
|
||||||
let skippedCount = 0;
|
|
||||||
const results = [];
|
|
||||||
|
|
||||||
for (const song of songs) {
|
|
||||||
try {
|
|
||||||
// Rate limiting: wait 2000ms between requests to be safe (iTunes can be strict)
|
|
||||||
await sleep(2000);
|
|
||||||
|
|
||||||
const year = await getReleaseYearFromItunes(song.artist, song.title);
|
|
||||||
|
|
||||||
if (year) {
|
|
||||||
await prisma.song.update({
|
|
||||||
where: { id: song.id },
|
|
||||||
data: { releaseYear: year }
|
|
||||||
});
|
|
||||||
updatedCount++;
|
|
||||||
results.push({ id: song.id, title: song.title, artist: song.artist, year, status: 'updated' });
|
|
||||||
} else {
|
|
||||||
skippedCount++;
|
|
||||||
results.push({ id: song.id, title: song.title, artist: song.artist, status: 'not_found' });
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Failed to update year for ${song.title} - ${song.artist}:`, error);
|
|
||||||
failedCount++;
|
|
||||||
results.push({ id: song.id, title: song.title, artist: song.artist, status: 'error' });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
processed: songs.length,
|
|
||||||
total: totalSongs,
|
|
||||||
hasMore: offset + songs.length < totalSongs,
|
|
||||||
nextOffset: offset + songs.length,
|
|
||||||
updated: updatedCount,
|
|
||||||
failed: failedCount,
|
|
||||||
skipped: skippedCount,
|
|
||||||
results
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error refreshing release years:', error);
|
|
||||||
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user