Fix spotify m3u Windows path parsing

This commit is contained in:
2026-04-12 10:03:43 -05:00
parent c2db2b51e7
commit 26a968797c
4 changed files with 46 additions and 3 deletions
+17 -1
View File
@@ -24,6 +24,22 @@ export function stripTrackNumberPrefix(value: string): string {
.replace(/^\d{1,3}\s+/u, "");
}
function stripTransientSuffixes(value: string): string {
return value.replace(/(?:[.\s_-]+temp)$/iu, "");
}
function replaceDotWordSeparators(value: string): string {
const dotParts = value.split(".").filter(Boolean);
if (dotParts.length > 1 && dotParts.every((part) => /^\p{L}$/u.test(part))) {
return value;
}
return value.replace(/\.(?=\S)/gu, " ");
}
function cleanTrackSource(value: string): string {
return normalizeText(replaceDotWordSeparators(stripTrackNumberPrefix(stripTransientSuffixes(stripAudioExtension(value)))));
}
function ref(source: string, artist: string | undefined, title: string | undefined, query?: string): ParsedTrackRef {
const cleanedArtist = artist ? normalizeText(artist) : undefined;
const cleanedTitle = title ? normalizeText(title) : undefined;
@@ -36,7 +52,7 @@ function ref(source: string, artist: string | undefined, title: string | undefin
}
export function parseArtistTitle(value: string): ParsedTrackRef[] {
const source = normalizeText(stripTrackNumberPrefix(stripAudioExtension(value)));
const source = cleanTrackSource(value);
if (!source) {
return [];
}
+5 -2
View File
@@ -1,5 +1,4 @@
import { readFile } from "node:fs/promises";
import { basename } from "node:path";
import type { ParsedTrackRef } from "../types.js";
import { dedupeTrackRefs, parseArtistTitle } from "./importer-utils.js";
@@ -12,6 +11,10 @@ function parseExtInf(line: string): string | undefined {
return line.slice(comma + 1).trim();
}
function filenameFromPlaylistPath(path: string): string {
return path.split(/[\\/]/u).at(-1) ?? path;
}
export function parseM3u(content: string): ParsedTrackRef[] {
const refs: ParsedTrackRef[] = [];
let pendingExtInf: string | undefined;
@@ -27,7 +30,7 @@ export function parseM3u(content: string): ParsedTrackRef[] {
if (line.startsWith("#")) {
continue;
}
refs.push(...parseArtistTitle(pendingExtInf ?? basename(line)));
refs.push(...parseArtistTitle(pendingExtInf ?? filenameFromPlaylistPath(line)));
pendingExtInf = undefined;
}
return dedupeTrackRefs(refs);