mirror of
https://github.com/captbaritone/webamp.git
synced 2026-07-23 01:57:29 +00:00
Write our own m3u parser
This commit is contained in:
parent
82f2b7a0b6
commit
112e7091cc
4 changed files with 44 additions and 15 deletions
|
|
@ -3,7 +3,6 @@
|
|||
import Raven from "raven-js";
|
||||
import createMiddleware from "raven-for-redux";
|
||||
import isButterchurnSupported from "butterchurn/lib/isSupported.min";
|
||||
import { parse } from "m3u-parser";
|
||||
import osx from "../skins/MacOSXAqua1-5.wsz";
|
||||
import topaz from "../skins/TopazAmp1-2.wsz";
|
||||
import visor from "../skins/Vizor1-01.wsz";
|
||||
|
|
@ -16,6 +15,7 @@ import "../mp3/test/01 Ghosts I.mp3";
|
|||
import "../mp3/test/02 Ghosts I.mp3";
|
||||
import "../mp3/test/03 Ghosts I.mp3";
|
||||
import internetArchive from "../skins/Internet-Archive.wsz";
|
||||
import m3uParser from "./m3uParser";
|
||||
import MilkdropWindow from "./components/MilkdropWindow";
|
||||
import screenshotInitialState from "./screenshotInitialState";
|
||||
import { WINDOWS } from "./constants";
|
||||
|
|
@ -305,7 +305,7 @@ Raven.context(async () => {
|
|||
const parentDirectory = getParentDirectory(m3u);
|
||||
const resonse = await fetch(m3u);
|
||||
const text = await resonse.text();
|
||||
const playlist = await parse(text);
|
||||
const playlist = m3uParser(text);
|
||||
const m3uTracks = playlist.map(m3uTrack => ({
|
||||
url: `${parentDirectory}${m3uTrack.file}`,
|
||||
duration: m3uTrack.duration,
|
||||
|
|
|
|||
41
js/m3uParser.ts
Normal file
41
js/m3uParser.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
const EXTINF_REGEX = /^#EXTINF:(-?)(\d+),(.*)$/;
|
||||
|
||||
interface M3uTrack {
|
||||
title: string;
|
||||
duration: number;
|
||||
file: string;
|
||||
}
|
||||
|
||||
export default function parseM3u(content: string): Array<M3uTrack> {
|
||||
// TODO: Support Windows line endings
|
||||
const lines = content.split("\n").filter(Boolean);
|
||||
const firstLine = lines[0];
|
||||
|
||||
if (!firstLine || !firstLine.startsWith("#EXTM3U")) {
|
||||
throw new Error("Content is not a valid M3U playlist");
|
||||
}
|
||||
|
||||
const tracks = [];
|
||||
for (var i = 1; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
if (!line.startsWith("#EXTINF")) {
|
||||
continue;
|
||||
}
|
||||
var result = EXTINF_REGEX.exec(line);
|
||||
if (!result) {
|
||||
throw new Error(`Invalid EXTINF at line ${i}`);
|
||||
}
|
||||
const nextLine = lines[++i];
|
||||
if (nextLine == null) {
|
||||
throw new Error(`Missing track path at line ${i}`);
|
||||
}
|
||||
|
||||
tracks.push({
|
||||
title: result[3].trim(),
|
||||
duration: Number(result[1] + result[2]),
|
||||
file: nextLine.trim()
|
||||
});
|
||||
}
|
||||
|
||||
return tracks;
|
||||
}
|
||||
|
|
@ -139,7 +139,5 @@
|
|||
]
|
||||
},
|
||||
"prettier": {},
|
||||
"dependencies": {
|
||||
"m3u-parser": "^1.0.3"
|
||||
}
|
||||
"dependencies": {}
|
||||
}
|
||||
|
|
|
|||
10
yarn.lock
10
yarn.lock
|
|
@ -1376,10 +1376,6 @@ block-stream@*:
|
|||
dependencies:
|
||||
inherits "~2.0.0"
|
||||
|
||||
bluebird@^3.1.1:
|
||||
version "3.5.2"
|
||||
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.2.tgz#1be0908e054a751754549c270489c1505d4ab15a"
|
||||
|
||||
bluebird@^3.5.1:
|
||||
version "3.5.1"
|
||||
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.1.tgz#d9551f9de98f1fcda1e683d17ee91a0602ee2eb9"
|
||||
|
|
@ -5496,12 +5492,6 @@ lru-cache@^4.1.1:
|
|||
pseudomap "^1.0.2"
|
||||
yallist "^2.1.2"
|
||||
|
||||
m3u-parser@^1.0.3:
|
||||
version "1.0.3"
|
||||
resolved "https://registry.yarnpkg.com/m3u-parser/-/m3u-parser-1.0.3.tgz#220fbdd5e54ccda7c230fcb09b59fd5ab4ba06a3"
|
||||
dependencies:
|
||||
bluebird "^3.1.1"
|
||||
|
||||
macaddress@^0.2.8:
|
||||
version "0.2.8"
|
||||
resolved "https://registry.yarnpkg.com/macaddress/-/macaddress-0.2.8.tgz#5904dc537c39ec6dbefeae902327135fa8511f12"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue