Example #1
0
File: cmd.go Project: ushis/webmpc
func getFiles(_ *Cmd, conn *mpd.Client) (r *Result, err error) {
	files, err := conn.GetFiles()

	if err == nil {
		r = NewResult("Files", files)
	}
	return
}
Example #2
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
}