Skip to content

add PlaylistSearch function #89

Open

Description

@iconoclasthero

Something like

type Song struct {
    Fields map[string][]string
}

func PlaylistSearch(conn net.Conn, filter string) ([]Song, error) {
    cmd := fmt.Sprintf("playlistsearch \"(%s)\"\n", filter)

    if _, err := conn.Write([]byte(cmd)); err != nil {
        return nil, err
    }

    reader := bufio.NewReader(conn)

    var songs []Song
    var current *Song

    for {
        line, err := reader.ReadString('\n')
        if err != nil {
            return nil, err
        }

        line = strings.TrimSpace(line)

        if line == "OK" {
            break
        }

        if strings.HasPrefix(line, "ACK") {
            return nil, fmt.Errorf("mpd error: %s", line)
        }

        if line == "" {
            continue
        }

        // New object starts at file:
        if strings.HasPrefix(line, "file: ") {
            if current != nil {
                songs = append(songs, *current)
            }
            current = &Song{
                Fields: make(map[string][]string),
            }
        }

        parts := strings.SplitN(line, ": ", 2)
        if len(parts) == 2 {
            if current == nil {
                current = &Song{
                    Fields: make(map[string][]string),
                }
            }
            key := parts[0]
            value := parts[1]
            current.Fields[key] = append(current.Fields[key], value)
        }
    }

    if current != nil {
        songs = append(songs, *current)
    }

    return songs, nil
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions