Esempio n. 1
0
File: cmd.go Progetto: ushis/webmpc
func playlistInfo(_ *Cmd, conn *mpd.Client) (r *Result, err error) {
	pl, err := conn.PlaylistInfo(-1, -1)

	if err == nil {
		r = NewResult("Playlist", pl)
	}
	return
}
Esempio n. 2
0
File: cmd.go Progetto: ushis/webmpc
func status(cmd *Cmd, conn *mpd.Client) (r *Result, err error) {
	state, err := conn.Status()

	if err == nil {
		r = NewResult("Status", state)
	}
	return
}
Esempio n. 3
0
File: cmd.go Progetto: ushis/webmpc
func listPlaylists(_ *Cmd, conn *mpd.Client) (r *Result, err error) {
	lists, err := conn.ListPlaylists()

	if err == nil {
		r = NewResult("StoredPlaylists", lists)
	}
	return
}
Esempio n. 4
0
File: cmd.go Progetto: ushis/webmpc
func getFiles(_ *Cmd, conn *mpd.Client) (r *Result, err error) {
	files, err := conn.GetFiles()

	if err == nil {
		r = NewResult("Files", files)
	}
	return
}
Esempio n. 5
0
File: cmd.go Progetto: ushis/webmpc
func currentSong(_ *Cmd, conn *mpd.Client) (r *Result, err error) {
	info, err := conn.CurrentSong()

	if err == nil {
		r = NewResult("CurrentSong", info)
	}
	return
}
Esempio n. 6
0
File: cmd.go Progetto: ushis/webmpc
func playlistContents(cmd *Cmd, conn *mpd.Client) (r *Result, err error) {
	pl, err := conn.PlaylistContents(cmd.Playlist)

	if err == nil {
		r = NewResult("StoredPlaylist", NewStoredPlaylist(cmd.Playlist, pl))
	}
	return
}
Esempio n. 7
0
// Walk over all music files and create a .mood file for each in mood-dir.
func updateMoodDatabase(client *mpd.Client, cfg *Config) error {
	if cfg.MoodDir == "" {
		return fmt.Errorf("No mood bar directory given (-mood-dir)")
	}

	if err := os.MkdirAll(cfg.MoodDir, 0777); err != nil {
		return err
	}

	paths, err := client.GetFiles()
	if err != nil {
		return fmt.Errorf("Cannot get all files from mpd: %v", err)
	}

	// Use up to N threads:
	N := 8
	wg := &sync.WaitGroup{}
	wg.Add(N)

	moodChan := make(chan *MoodInfo, N)
	for i := 0; i < N; i++ {
		go func() {
			for pair := range moodChan {
				log.Printf("Processing: %s", pair.MusicFile)
				cmd := exec.Command("moodbar", pair.MusicFile, "-o", pair.MoodPath)
				if err := cmd.Run(); err != nil {
					log.Printf("Failed to execute moodbar on `%s`: %v", pair.MusicFile, err)
				}
			}

			wg.Done()
		}()
	}

	for _, path := range paths {
		moodName := strings.Replace(path, string(filepath.Separator), "|", -1)
		moodPath := filepath.Join(cfg.MoodDir, moodName)

		if _, err := os.Stat(moodPath); err == nil {
			// Already exists, Skipping.
			continue
		}

		dataPath := filepath.Join(cfg.MusicDir, path)

		moodChan <- &MoodInfo{
			MusicFile: dataPath,
			MoodPath:  moodPath,
		}
	}

	close(moodChan)
	wg.Wait()

	return nil
}
Esempio n. 8
0
File: cmd.go Progetto: ushis/webmpc
func setPlaylist(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	list := conn.BeginCommandList()
	list.Clear()

	for _, uri := range cmd.Uris {
		list.Add(uri)
	}
	list.Play(0)

	return nil, list.End()
}
Esempio n. 9
0
File: cmd.go Progetto: ushis/webmpc
func addMulti(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	list := conn.BeginCommandList()

	for i, uri := range cmd.Uris {
		if cmd.Pos < 0 {
			list.Add(uri)
		} else {
			list.AddId(uri, cmd.Pos+i)
		}
	}
	return nil, list.End()
}
Esempio n. 10
0
//Updates the library
func (l *library) update() error {
	var conn *mpd.Client

	fmt.Println("Connecting to MPD")
	conn, err := mpd.Dial("tcp", "127.0.0.1:6600")
	if err != nil {
		fmt.Println("Error: could not connect to MPD for lib update")
		return errors.New("Could not connect to MPD!")
	}
	defer conn.Close()

	_, err = conn.Update("")
	if err != nil {
		fmt.Println("Error: could not update library!")
		return err
	}

	//Let the update happen
	time.Sleep(2 * time.Second)
	songs, err := conn.ListAllInfo("/")
	if err != nil {
		fmt.Println("Error: could not retrieve new library!")
		return err
	}

	l.library = songs
	return nil
}
Esempio n. 11
0
func (m MPDInstance) Render() (item modules.Item) {
	mpdItem := MPDItem{Name: m.name, Markup: "pango"}
	mpdFormatData := MPDFormatData{}
	var client *go_mpd.Client

	if c, err := go_mpd.Dial("tcp", m.host_name+":"+strconv.Itoa(m.port)); err != nil {
		log.Error(err.Error())
		return nil
	} else {
		client = c
	}

	defer func(client *go_mpd.Client) {
		if err := client.Close(); err != nil {
			log.Error("Failed to disconnect: " + err.Error())
		}
	}(client)

	if attrs, err := client.Status(); err == nil {
		if state, ok := attrs["state"]; ok {
			mpdFormatData.State = state
		} else {
			log.Error("Failed to read state.")
			return nil
		}
	} else {
		log.Error("Failed to obtain status: " + err.Error())
		return nil
	}

	if attrs, err := client.CurrentSong(); err != nil {
		log.Error("Failed to obtain current song: " + err.Error())
		return nil
	} else {
		obj := reflect.ValueOf(&mpdFormatData).Elem()
		for key, val := range attrs {
			if f := obj.FieldByName(key); f.IsValid() {
				val = strings.TrimSpace(val)
				f.Set(reflect.ValueOf(val))
			}
		}
	}

	buffer := bytes.Buffer{}

	if err := m.template.Execute(&buffer, mpdFormatData); err != nil {
		log.Error("Failed to render mpd template: " + err.Error())
		return nil
	} else {
		mpdItem.Text = buffer.String()
	}

	item = mpdItem
	return
}
Esempio n. 12
0
//Create a new queue
func newLibrary() *library {
	var conn *mpd.Client

	fmt.Println("Connecting to MPD")
	conn, err := mpd.Dial("tcp", "127.0.0.1:6600")
	if err != nil {
		fmt.Println("Error: could not connect to MPD, exiting")
		os.Exit(1)
	}
	defer conn.Close()

	songs, err := conn.ListAllInfo("/")
	if err != nil {
		fmt.Println("Error: could not connect to MPD, exiting")
		os.Exit(1)
	}
	shuffle(songs)

	return &library{
		library: songs,
	}
}
Esempio n. 13
0
File: cmd.go Progetto: ushis/webmpc
func add(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	return nil, conn.Add(cmd.Uri)
}
Esempio n. 14
0
File: cmd.go Progetto: ushis/webmpc
func setVolume(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	return nil, conn.SetVolume(cmd.Volume)
}
Esempio n. 15
0
File: cmd.go Progetto: ushis/webmpc
func shuffle(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	return nil, conn.Shuffle(cmd.Start, cmd.End)
}
Esempio n. 16
0
File: cmd.go Progetto: ushis/webmpc
func seekId(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	return nil, conn.SeekId(cmd.Id, cmd.Time)
}
Esempio n. 17
0
File: cmd.go Progetto: ushis/webmpc
func moveId(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	return nil, conn.MoveId(cmd.Id, cmd.Pos)
}
Esempio n. 18
0
File: cmd.go Progetto: ushis/webmpc
func random(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	return nil, conn.Random(cmd.Random)
}
Esempio n. 19
0
File: cmd.go Progetto: ushis/webmpc
func repeat(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	return nil, conn.Repeat(cmd.Repeat)
}
Esempio n. 20
0
File: cmd.go Progetto: ushis/webmpc
func playlistSave(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	return nil, conn.PlaylistSave(cmd.Playlist)
}
Esempio n. 21
0
File: cmd.go Progetto: ushis/webmpc
func previous(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	return nil, conn.Previous()
}
Esempio n. 22
0
File: cmd.go Progetto: ushis/webmpc
func playlistRename(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	return nil, conn.PlaylistRename(cmd.Playlist, cmd.Name)
}
Esempio n. 23
0
File: cmd.go Progetto: ushis/webmpc
func next(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	return nil, conn.Next()
}
Esempio n. 24
0
File: cmd.go Progetto: ushis/webmpc
func pause(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	return nil, conn.Pause(cmd.Pause)
}
Esempio n. 25
0
File: cmd.go Progetto: ushis/webmpc
func stop(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	return nil, conn.Stop()
}
Esempio n. 26
0
File: cmd.go Progetto: ushis/webmpc
func playId(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	return nil, conn.PlayId(cmd.Id)
}
Esempio n. 27
0
File: cmd.go Progetto: ushis/webmpc
func update(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	_, err := conn.Update("")
	return nil, err
}
Esempio n. 28
0
File: cmd.go Progetto: ushis/webmpc
func playlistLoad(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	return nil, conn.PlaylistLoad(cmd.Playlist, cmd.Start, cmd.End)
}
Esempio n. 29
0
File: mpd.go Progetto: ushis/webmpc
// Pings MPD.
func (m *Mpd) ping(c *mpd.Client) {
	if err := c.Ping(); err != nil {
		m.err <- err
	}
}
Esempio n. 30
0
File: cmd.go Progetto: ushis/webmpc
func playlistMove(cmd *Cmd, conn *mpd.Client) (*Result, error) {
	if err := conn.PlaylistMove(cmd.Playlist, cmd.Id, cmd.Pos); err != nil {
		return nil, err
	}
	return playlistContents(cmd, conn)
}