示例#1
0
func (tp TracksProcessor) Process(t track.Track) error {
	// Download track
	ui.Println("Downloading " + t.Artist() + " - " + t.Title())
	trackPath := filepath.Join(tp.DownloadFolder, t.Filename())
	if _, e := os.Create(trackPath); e != nil {
		return fmt.Errorf("couldn't create track file:", e)
	}
	if e := downloadTrack(t, trackPath); e != nil {
		return fmt.Errorf("couldn't download track:", e)
	}

	// err lets us to not prevent the processing of track further.
	var err error

	// Download artwork
	var artworkPath string
	var artworkBytes []byte
	artworkFile, e := ioutil.TempFile("", "")
	if e != nil {
		err = fmt.Errorf("couldn't create artwork file:", e)
	} else {
		artworkPath = artworkFile.Name()
		if e = downloadArtwork(t, artworkPath); e != nil {
			err = fmt.Errorf("couldn't download artwork file:", e)
		}
		if err == nil {
			artworkBytes, e = ioutil.ReadAll(artworkFile)
			if e != nil {
				err = fmt.Errorf("couldn't read artwork file:", e)
			}
		}
	}

	// Tag track
	if e := tag(t, trackPath, artworkBytes); e != nil {
		err = fmt.Errorf("coudln't tag file:", e)
	}

	// Delete artwork
	if e := artworkFile.Close(); e != nil {
		err = fmt.Errorf("couldn't close artwork file:", e)
	}
	if e := os.Remove(artworkPath); e != nil {
		err = fmt.Errorf("couldn't remove artwork file:", e)
	}

	// Add to iTunes
	if tp.ItunesPlaylist != "" {
		ui.Println("Adding to iTunes")
		if e := applescript.AddTrackToPlaylist(trackPath, tp.ItunesPlaylist); e != nil {
			err = fmt.Errorf("couldn't add track to playlist:", e)
		}
	}

	return err
}