Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions public/placeholder.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 30 additions & 29 deletions server/src/scripts/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,26 @@ const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));


// Genre name in our app -> tag to search for in MusicBrainz
const GENRE_SEARCH_TAGS: Record<string, string> = {
Pop: 'pop',
Rock: 'rock',
HipHop: 'hip hop',
Jazz: 'jazz',
Electronic: 'electronic',
Indie: 'indie',
Soul: 'soul',
Funk: 'funk',
Country: 'country',
Metal: 'metal',
Blues: 'blues',
Tekno: 'techno',
Kpop: 'k-pop',
Classic: 'classical',
RnB: 'rnb',
const GENRE_SEARCH_TAGS: Record<string, string[]> = {
Pop: ['pop'],
Rock: ['rock'],
HipHop: ['hip hop', 'hip-hop', 'rap'],
Jazz: ['jazz'],
Electronic: ['electronic', 'electronica'],
Tekno: ['techno'],
Indie: ['indie', 'indie rock', 'indie pop'],
Soul: ['soul'],
Kpop: ['k-pop', 'kpop'],
Funk: ['funk'],
Country: ['country'],
Classic: ['classical', 'classics'],
Metal: ['metal', 'heavy metal'],
RnB: ['rnb', 'r&b', 'contemporary r&b', 'rhythm and blues'],
Blues: ['blues'],
};

const QUOTA_PER_GENRE = parseInt(process.argv[3], 10) || 10;
const MIN_RELEASES = parseInt(process.argv[4], 10) || 3;
const MIN_RELEASES = parseInt(process.argv[4], 10) || 1;

async function mbFetch<T>(url: string): Promise<T | null> {
for (let attempt = 1; attempt <= 4; attempt++) {
Expand Down Expand Up @@ -171,15 +171,16 @@ async function importDecade(startYear: number) {
const endYear = startYear + 9;
console.log(`\n=== Import for ${startYear}-${endYear} (kvote ${QUOTA_PER_GENRE}/sjanger, min ${MIN_RELEASES} utgaver) ===`);

for (const [genreName, tag] of Object.entries(GENRE_SEARCH_TAGS)) {
console.log(`\n--- ${genreName} (${startYear}-${endYear}) ---`);

const query =
`primarytype:album AND NOT secondarytype:* AND tag:"${tag}" ` +
`AND firstreleasedate:[${startYear} TO ${endYear}]` +
`AND releases:[${MIN_RELEASES} TO *]`;


for (const [genreName, tags] of Object.entries(GENRE_SEARCH_TAGS)) {
console.log(`\n--- ${genreName} (${startYear}-${endYear}) ---`);

const tagQuery = tags.map((t) => `"${t}"`).join(' OR ');
const query =
`primarytype:album AND NOT secondarytype:* AND tag:(${tagQuery}) ` +
`AND firstreleasedate:[${startYear} TO ${endYear}] ` +
`AND releases:[${MIN_RELEASES} TO *]`;


let imported = 0;
let offset = 0;
let total = Infinity; // updated after the first response
Expand All @@ -191,7 +192,7 @@ async function importDecade(startYear: number) {

const page = await mbFetch<SearchResponse>(url);
if (!page) {
console.warn(`⚠️ Søket feilet for ${genreName} ved offset ${offset}. Går videre til neste sjanger.`);
console.warn(`Søket feilet for ${genreName} ved offset ${offset}. Går videre til neste sjanger.`);
break;
}

Expand Down Expand Up @@ -222,15 +223,15 @@ async function downloadCover(mbid: string, title: string): Promise<string | null
try {
const response = await fetch(imageUrl);
if (!response.ok) {
console.warn(`⚠️ Mangler cover for: "${title}" (MBID: ${mbid})`);
console.warn(`Mangler cover for: "${title}" (MBID: ${mbid})`);
return null;
}

const arrayBuffer = await response.arrayBuffer();
fs.writeFileSync(filePath, Buffer.from(arrayBuffer));
return `/covers/${mbid}.jpg`;
} catch (err) {
console.warn(`⚠️ Kunne ikke laste ned cover for: "${title}"`);
console.warn(`Kunne ikke laste ned cover for: "${title}"`);
return null;
}
}
Expand Down