feat(spotify): implement milestone M4 importers
This commit is contained in:
38
skills/spotify/src/importers/m3u.ts
Normal file
38
skills/spotify/src/importers/m3u.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { basename } from "node:path";
|
||||
|
||||
import type { ParsedTrackRef } from "../types.js";
|
||||
import { dedupeTrackRefs, parseArtistTitle } from "./importer-utils.js";
|
||||
|
||||
function parseExtInf(line: string): string | undefined {
|
||||
const comma = line.indexOf(",");
|
||||
if (comma === -1 || comma === line.length - 1) {
|
||||
return undefined;
|
||||
}
|
||||
return line.slice(comma + 1).trim();
|
||||
}
|
||||
|
||||
export function parseM3u(content: string): ParsedTrackRef[] {
|
||||
const refs: ParsedTrackRef[] = [];
|
||||
let pendingExtInf: string | undefined;
|
||||
for (const rawLine of content.split(/\r?\n/u)) {
|
||||
const line = rawLine.trim();
|
||||
if (!line) {
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith("#EXTINF:")) {
|
||||
pendingExtInf = parseExtInf(line);
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
refs.push(...parseArtistTitle(pendingExtInf ?? basename(line)));
|
||||
pendingExtInf = undefined;
|
||||
}
|
||||
return dedupeTrackRefs(refs);
|
||||
}
|
||||
|
||||
export async function readM3u(path: string): Promise<ParsedTrackRef[]> {
|
||||
return parseM3u(await readFile(path, "utf8"));
|
||||
}
|
||||
Reference in New Issue
Block a user