feat(song): add ID3v2 tags
This commit is contained in:
+7
-2
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/mathismqn/godeez/internal/deezer"
|
"github.com/mathismqn/godeez/internal/deezer"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@@ -46,7 +47,7 @@ var downloadCmd = &cobra.Command{
|
|||||||
|
|
||||||
fmt.Printf("\r[%d/%d] Getting data for album %s... DONE\n", i+1, nAlbums, id)
|
fmt.Printf("\r[%d/%d] Getting data for album %s... DONE\n", i+1, nAlbums, id)
|
||||||
|
|
||||||
output = path.Join(output, fmt.Sprintf("%s - %s", album.Data.ArtistName, album.Data.Name))
|
output = path.Join(output, fmt.Sprintf("%s - %s", album.Data.Artist, album.Data.Name))
|
||||||
if _, err := os.Stat(output); os.IsNotExist(err) {
|
if _, err := os.Stat(output); os.IsNotExist(err) {
|
||||||
if err := os.MkdirAll(output, 0755); err != nil {
|
if err := os.MkdirAll(output, 0755); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Error: could not create output directory: %v\n", err)
|
fmt.Fprintf(os.Stderr, "Error: could not create output directory: %v\n", err)
|
||||||
@@ -92,7 +93,7 @@ var downloadCmd = &cobra.Command{
|
|||||||
ext = "flac"
|
ext = "flac"
|
||||||
}
|
}
|
||||||
|
|
||||||
filePath := path.Join(output, fmt.Sprintf("%s - %s.%s", song.ArtistName, songTitle, ext))
|
filePath := path.Join(output, fmt.Sprintf("%s - %s.%s", strings.Join(song.Contributors.MainArtists, ", "), songTitle, ext))
|
||||||
err = media.Download(url, filePath, song.ID)
|
err = media.Download(url, filePath, song.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("\r Downloading %s... FAILED\n", songTitle)
|
fmt.Printf("\r Downloading %s... FAILED\n", songTitle)
|
||||||
@@ -100,6 +101,10 @@ var downloadCmd = &cobra.Command{
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Printf("\r Downloading %s... DONE\n", songTitle)
|
fmt.Printf("\r Downloading %s... DONE\n", songTitle)
|
||||||
|
|
||||||
|
if err := deezer.AddTags(album, song, filePath); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Error: could not add tags to song: %v\n", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/bogem/id3v2/v2 v2.1.4
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
golang.org/x/crypto v0.28.0
|
golang.org/x/crypto v0.28.0
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
github.com/bogem/id3v2/v2 v2.1.4 h1:CEwe+lS2p6dd9UZRlPc1zbFNIha2mb2qzT1cCEoNWoI=
|
||||||
|
github.com/bogem/id3v2/v2 v2.1.4/go.mod h1:l+gR8MZ6rc9ryPTPkX77smS5Me/36gxkMgDayZ9G1vY=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
@@ -57,18 +59,42 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
|
|||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||||
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
||||||
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||||
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
|
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
|
||||||
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
|
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
|
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
|
||||||
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
|
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
|
||||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
|
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
|
||||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
|
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
|
||||||
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
|
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
|
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
|
||||||
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
|
||||||
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
|
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
|
||||||
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
|||||||
@@ -10,15 +10,16 @@ import (
|
|||||||
|
|
||||||
type Album struct {
|
type Album struct {
|
||||||
Data struct {
|
Data struct {
|
||||||
ID string `json:"ALB_ID"`
|
|
||||||
Name string `json:"ALB_TITLE"`
|
Name string `json:"ALB_TITLE"`
|
||||||
ArtistID string `json:"ART_ID"`
|
Artist string `json:"ART_NAME"`
|
||||||
ArtistName string `json:"ART_NAME"`
|
|
||||||
CoverID string `json:"ALB_PICTURE"`
|
CoverID string `json:"ALB_PICTURE"`
|
||||||
ReleaseData string `json:"PHYSICAL_RELEASE_DATE"`
|
OriginalReleaseDate string `json:"ORIGINAL_RELEASE_DATE"`
|
||||||
|
PhysicalReleaseDate string `json:"PHYSICAL_RELEASE_DATE"`
|
||||||
|
Label string `json:"LABEL_NAME"`
|
||||||
|
ProducerLine string `json:"PRODUCER_LINE"`
|
||||||
} `json:"DATA"`
|
} `json:"DATA"`
|
||||||
Songs struct {
|
Songs struct {
|
||||||
Data []Song `json:"data"`
|
Data []*Song `json:"data"`
|
||||||
} `json:"SONGS"`
|
} `json:"SONGS"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-1
@@ -12,9 +12,19 @@ import (
|
|||||||
|
|
||||||
type Song struct {
|
type Song struct {
|
||||||
ID string `json:"SNG_ID"`
|
ID string `json:"SNG_ID"`
|
||||||
ArtistName string `json:"ART_NAME"`
|
Artist string `json:"ART_NAME"`
|
||||||
Title string `json:"SNG_TITLE"`
|
Title string `json:"SNG_TITLE"`
|
||||||
Version string `json:"VERSION"`
|
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"`
|
TrackToken string `json:"TRACK_TOKEN"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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