feat(song): add ID3v2 tags
This commit is contained in:
@@ -10,15 +10,16 @@ import (
|
||||
|
||||
type Album struct {
|
||||
Data struct {
|
||||
ID string `json:"ALB_ID"`
|
||||
Name string `json:"ALB_TITLE"`
|
||||
ArtistID string `json:"ART_ID"`
|
||||
ArtistName string `json:"ART_NAME"`
|
||||
CoverID string `json:"ALB_PICTURE"`
|
||||
ReleaseData string `json:"PHYSICAL_RELEASE_DATE"`
|
||||
Name string `json:"ALB_TITLE"`
|
||||
Artist string `json:"ART_NAME"`
|
||||
CoverID string `json:"ALB_PICTURE"`
|
||||
OriginalReleaseDate string `json:"ORIGINAL_RELEASE_DATE"`
|
||||
PhysicalReleaseDate string `json:"PHYSICAL_RELEASE_DATE"`
|
||||
Label string `json:"LABEL_NAME"`
|
||||
ProducerLine string `json:"PRODUCER_LINE"`
|
||||
} `json:"DATA"`
|
||||
Songs struct {
|
||||
Data []Song `json:"data"`
|
||||
Data []*Song `json:"data"`
|
||||
} `json:"SONGS"`
|
||||
}
|
||||
|
||||
|
||||
+15
-5
@@ -11,11 +11,21 @@ import (
|
||||
)
|
||||
|
||||
type Song struct {
|
||||
ID string `json:"SNG_ID"`
|
||||
ArtistName string `json:"ART_NAME"`
|
||||
Title string `json:"SNG_TITLE"`
|
||||
Version string `json:"VERSION"`
|
||||
TrackToken string `json:"TRACK_TOKEN"`
|
||||
ID string `json:"SNG_ID"`
|
||||
Artist string `json:"ART_NAME"`
|
||||
Title string `json:"SNG_TITLE"`
|
||||
Version string `json:"VERSION"`
|
||||
Contributors struct {
|
||||
MainArtists []string `json:"main_artist"`
|
||||
Composers []string `json:"composer"`
|
||||
Authors []string `json:"author"`
|
||||
} `json:"SNG_CONTRIBUTORS"`
|
||||
Duration string `json:"DURATION"`
|
||||
Gain string `json:"GAIN"`
|
||||
ISRC string `json:"ISRC"`
|
||||
ExplicitLyrics string `json:"EXPLICIT_LYRICS"`
|
||||
TrackNumber string `json:"TRACK_NUMBER"`
|
||||
TrackToken string `json:"TRACK_TOKEN"`
|
||||
}
|
||||
|
||||
func (s *Song) GetMediaData(quality string) (*Media, error) {
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package deezer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/bogem/id3v2/v2"
|
||||
)
|
||||
|
||||
func AddTags(album *Album, song *Song, pathFile string) error {
|
||||
ext := path.Ext(pathFile)
|
||||
if ext == ".mp3" {
|
||||
tag, err := id3v2.Open(pathFile, id3v2.Options{Parse: true})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tag.Close()
|
||||
|
||||
var year string
|
||||
duration, _ := strconv.Atoi(song.Duration)
|
||||
dateParts := strings.Split(album.Data.PhysicalReleaseDate, "-")
|
||||
if len(dateParts) == 3 {
|
||||
year = dateParts[0]
|
||||
}
|
||||
|
||||
addTag(tag, "TALB", album.Data.Name)
|
||||
addTag(tag, "TPE2", album.Data.Artist)
|
||||
addTag(tag, "TPUB", album.Data.Label)
|
||||
addTag(tag, "TDOR", album.Data.OriginalReleaseDate)
|
||||
addTag(tag, "COMM", album.Data.ProducerLine)
|
||||
if year != "" {
|
||||
addTag(tag, "TYER", year)
|
||||
}
|
||||
|
||||
addTag(tag, "TIT2", song.Title)
|
||||
addTag(tag, "TPE1", strings.Join(song.Contributors.MainArtists, " / "))
|
||||
addTag(tag, "TCOM", strings.Join(song.Contributors.Composers, " / "))
|
||||
addTag(tag, "TEXT", strings.Join(song.Contributors.Authors, " / "))
|
||||
addTag(tag, "TRCK", song.TrackNumber)
|
||||
if duration > 0 {
|
||||
addTag(tag, "TLEN", fmt.Sprintf("%d", duration*1000))
|
||||
}
|
||||
addTXXXTag(tag, "GAIN", song.Gain)
|
||||
addTXXXTag(tag, "ISRC", song.ISRC)
|
||||
addTXXXTag(tag, "Explicit lyrics", song.ExplicitLyrics)
|
||||
|
||||
return tag.Save()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func addTag(tag *id3v2.Tag, name, value string) {
|
||||
if value != "" {
|
||||
tag.AddTextFrame(name, tag.DefaultEncoding(), value)
|
||||
}
|
||||
}
|
||||
|
||||
func addTXXXTag(tag *id3v2.Tag, description, value string) {
|
||||
if value != "" {
|
||||
udf := id3v2.UserDefinedTextFrame{
|
||||
Encoding: tag.DefaultEncoding(),
|
||||
Description: description,
|
||||
Value: value,
|
||||
}
|
||||
tag.AddUserDefinedTextFrame(udf)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user