absorb: playlist/xspf (new: XSPF 1 read / write on codecs/Xml, xml:base, every track field); tests vs ElementTree-written files

This commit is contained in:
4DBug committed 2026-10-31 08:57:35 -05:00
1 parent 443a53a984
commit 4e5f10b646
5 files changed
+317 -1

No files matched your search

+12
View File
@@ -122,6 +122,18 @@ local list: { Entry } = {
return require("@formats/playlist/pls")
end,
},
{
name = "xspf",
category = "playlist",
path = "playlist/xspf",
extensions = { "xspf" },
mime = { "application/xspf+xml" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Url", "codecs/Xml" },
caps = { decode = true, encode = true },
load = function()
return require("@formats/playlist/xspf")
end,
},
}
return list
+214
View File
@@ -0,0 +1,214 @@
--!strict
-- playlist/xspf: XSPF 1 ("spiff", https://xspf.org/spec) playlists on codecs/Xml. New in formats-luau (playlist-luau
-- had no XSPF). Decode: the playlist's title / creator / annotation / info / location / identifier / image / date,
-- every track's location (the first one is the entry url, all of them in entry.locations), title, creator, album,
-- annotation, info, image, trackNum and duration (milliseconds -> seconds), xml:base on the playlist and on tracks;
-- locations resolve against xml:base, then opts.baseUrl (RFC 3986, codecs/Url), and are classified like the other
-- playlist formats (local / relative / non-HTTP locations are unplayable). Encode: XSPF version 1 with those fields.
-- Not supported: extension / link / meta elements (skipped), XSPF version 0 differences.
--
-- local pl = Xspf.decode(Bytes.reader(buf), { baseUrl = "https://example.com/list.xspf" })
-- for _, e in pl.entries do print(e.url, e.title, e.creator, e.duration) end
-- local out = Xspf.encode(pl)
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Types = require("@formats/Types")
local Url = require("@formats/codecs/Url")
local Xml = require("@formats/codecs/Xml")
-- ------------------------------------------------------------------------------------------------ contract
local Xspf = {
name = "xspf",
category = "playlist",
extensions = { "xspf" },
mime = { "application/xspf+xml" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Url", "codecs/Xml" },
caps = { decode = true, encode = true },
}
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
baseUrl: string?, -- the playlist's own URL (or Windows path), for relative locations
maxBytes: number?, -- refuse bigger files (default 64 MB)
}
-- ------------------------------------------------------------------------------------------------ constants
local NS = "http://xspf.org/ns/0/"
local PLAYLIST_FIELDS = { "title", "creator", "annotation", "info", "location", "identifier", "image", "date", "license" }
local TRACK_FIELDS = { "title", "creator", "album", "annotation", "info", "image" }
local function trim(s: string): string
return (string.match(s, "^%s*(.-)%s*$") :: string)
end
-- the text of the first child element with this local name, trimmed (nil when missing or empty)
local function field(node: any, name: string): string?
for _, c in node.children do
if type(c) == "table" and Xml.localName(c.name) == name then
local s = trim(Xml.text(c))
return if s ~= "" then s else nil
end
end
return nil
end
local function elements(node: any, name: string): { any }
local out = {}
for _, c in node.children do
if type(c) == "table" and Xml.localName(c.name) == name then
table.insert(out, c)
end
end
return out
end
-- ------------------------------------------------------------------------------------------------ sniff
function Xspf.sniff(r: Bytes.Reader): number
local s = r:peek(1024)
local i = string.find(s, "<%w*:?playlist[%s>]")
if i and string.find(s, NS, 1, true) then
return 0.95
end
return 0
end
-- ------------------------------------------------------------------------------------------------ decode
-- decode(r, opts?) -> Types.Playlist | nil, Err (malformed XML or a root that is not <playlist> is an error; odd
-- tracks become warnings)
function Xspf.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Playlist?, Err.Err?)
local n = r.limit - r.pos
if n > (opts and opts.maxBytes or 2 ^ 26) then
return nil, Err.new(Err.LIMIT, "playlist over the size limit")
end
local doc, err = Xml.decode(r:string(n))
if not doc then
return nil, err
end
local root = (doc :: any).root
if not root or Xml.localName(root.name) ~= "playlist" then
return nil, Err.new(Err.CORRUPT, "the root element is not <playlist>")
end
local warnings: { string } = {}
local base, baseScheme = Url.base(opts and opts.baseUrl, warnings)
local function withXmlBase(node: any, b: string?, s: string?): (string?, string?)
local xb = node.attrs and node.attrs["xml:base"]
if type(xb) ~= "string" or xb == "" then
return b, s
end
local l = Url.locate(xb, b, s)
if Url.scheme(l.url) then
return l.url, Url.scheme(l.url)
end
return b, s
end
local pbase, pscheme = withXmlBase(root, base, baseScheme)
local pl: any = {
kind = "playlist",
format = "xspf",
entries = {},
warnings = warnings,
title = field(root, "title"),
meta = { source = { format = "xspf", version = root.attrs and root.attrs.version } },
}
for _, k in PLAYLIST_FIELDS do
if k ~= "title" then
pl.meta[k] = field(root, k)
end
end
local nLocal = 0
for _, list in elements(root, "trackList") do
for i, track in elements(list, "track") do
local tbase, tscheme = withXmlBase(track, pbase, pscheme)
local locs = {}
for _, l in elements(track, "location") do
local s = trim(Xml.text(l))
if s ~= "" then
table.insert(locs, s)
end
end
if #locs == 0 then
table.insert(warnings, string.format("track %d: no location", i))
else
local e: any = Url.locate(locs[1], tbase, tscheme)
for _, k in TRACK_FIELDS do
e[k] = field(track, k)
end
local d = tonumber(field(track, "duration"))
e.duration = if d and d >= 0 and d == d and d ~= math.huge then d / 1000 else nil
e.trackNum = tonumber(field(track, "trackNum"))
if #locs > 1 then
local all = {}
for _, l in locs do
table.insert(all, Url.locate(l, tbase, tscheme).url)
end
e.locations = all
end
if e.reason == "local path" then
nLocal += 1
elseif e.reason then
table.insert(warnings, string.format("track %d: %s: %s", i, e.reason, e.url))
end
table.insert(pl.entries, e)
end
end
end
if nLocal > 0 then
table.insert(warnings, string.format("%d local path%s (not playable)", nLocal, if nLocal == 1 then "" else "s"))
end
return pl, nil
end
-- ------------------------------------------------------------------------------------------------ encode
-- encode(playlist, opts?) -> buffer | nil, Err: XSPF version 1. playlist: a Types.Playlist (any format's) or a list
-- of entries ({ url, title?, duration?, creator?, album?, ... }). opts.indent (default 2) pretty-prints.
function Xspf.encode(value: any, opts: { indent: number?, title: string? }?): (buffer?, Err.Err?)
if type(value) ~= "table" then
return nil, Err.new(Err.ARG, "encode: needs a Playlist or a list of entries")
end
local entries = if type(value.entries) == "table" then value.entries else value
local root = Xml.element("playlist", { version = "1", xmlns = NS })
local function add(parent: any, name: string, v: any)
if type(v) == "string" and v ~= "" then
Xml.append(parent, Xml.element(name, nil, { v }))
elseif type(v) == "number" and v == v and v ~= math.huge and v ~= -math.huge then
Xml.append(parent, Xml.element(name, nil, { tostring(math.floor(v + 0.5)) }))
end
end
local title = if opts and opts.title then opts.title else value.title
add(root, "title", title)
local meta = if type(value.meta) == "table" then value.meta else {}
for _, k in PLAYLIST_FIELDS do
if k ~= "title" then
add(root, k, meta[k])
end
end
local list = Xml.append(root, Xml.element("trackList"))
for _, e in entries do
if type(e) == "table" and type(e.url) == "string" then
local tr = Xml.append(list, Xml.element("track"))
add(tr, "location", e.url)
if type(e.locations) == "table" then
for i = 2, #e.locations do
add(tr, "location", e.locations[i])
end
end
for _, k in TRACK_FIELDS do
add(tr, k, e[k])
end
local d = tonumber(e.duration)
if d and d >= 0 then
add(tr, "duration", d * 1000)
end
add(tr, "trackNum", tonumber(e.trackNum))
end
end
local s = Xml.encode(Xml.document(root), { indent = if opts and opts.indent ~= nil then opts.indent else 2 })
if type(s) ~= "string" then
return nil, Err.new(Err.ARG, "encode: the XML serializer failed")
end
return buffer.fromstring(s), nil
end
return Xspf