예제 #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
}
예제 #2
0
func tag(t track.Track, trackPath string, artwork []byte) error {
	tag, err := id3v2.Open(trackPath)
	if err != nil {
		return err
	}
	defer tag.Close()

	tag.SetArtist(t.Artist())
	tag.SetTitle(t.Title())
	tag.SetYear(t.Year())

	if artwork != nil {
		pic := id3v2.PictureFrame{
			Encoding:    id3v2.ENUTF8,
			MimeType:    "image/jpeg",
			PictureType: id3v2.PTFrontCover,
			Picture:     artwork,
		}
		tag.AddAttachedPicture(pic)
	}

	return tag.Save()
}