streamdata: iterator over known idents

Useful for searching.

Signed-off-by: Yonah <contrib@gensokyo.uk>
This commit is contained in:
2026-03-19 01:44:42 +09:00
parent 522b576cfa
commit c6da581ec0
2 changed files with 46 additions and 0 deletions

View File

@@ -6,11 +6,15 @@ import (
"encoding/json"
"errors"
"io"
"io/fs"
"iter"
"os"
"path"
"strconv"
"strings"
"syscall"
"time"
"unsafe"
)
// Channel represents a Twitch channel.
@@ -198,6 +202,38 @@ func (c *Channel) Add(ident *Ident, f func(v *VOD, w io.Writer) error) error {
return nil
}
// All returns an iterator over all known [Ident] in the on-disk representation.
// Iteration stops when encountering the first non-nil error, and its value is
// saved to the value pointed to by errP.
func (c *Channel) All(errP *error) iter.Seq[*Ident] {
return func(yield func(*Ident) bool) {
dents, err := c.root.FS().(fs.ReadDirFS).ReadDir(channelPathVOD)
if err != nil {
*errP = err
return
}
var ident Ident
for _, dent := range dents {
name := dent.Name()
if strings.HasSuffix(name, ChannelVODSuffix) {
continue
}
if err = ident.UnmarshalText(
unsafe.Slice(unsafe.StringData(name), len(name)),
); err != nil {
*errP = err
return
}
if !yield(&ident) {
return
}
}
}
}
// Load loads the metadata of a [VOD] by [Ident] and returns its address.
func (c *Channel) Load(ident *Ident) (*VOD, error) {
var v VOD