feat(song): add cover image
This commit is contained in:
@@ -2,6 +2,8 @@ package deezer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -10,6 +12,11 @@ import (
|
||||
)
|
||||
|
||||
func AddTags(album *Album, song *Song, pathFile string) error {
|
||||
cover, err := getCoverImage(album.Data.CoverID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ext := path.Ext(pathFile)
|
||||
if ext == ".mp3" {
|
||||
tag, err := id3v2.Open(pathFile, id3v2.Options{Parse: true})
|
||||
@@ -46,12 +53,36 @@ func AddTags(album *Album, song *Song, pathFile string) error {
|
||||
addTXXXTag(tag, "ISRC", song.ISRC)
|
||||
addTXXXTag(tag, "Explicit lyrics", song.ExplicitLyrics)
|
||||
|
||||
frame := id3v2.PictureFrame{
|
||||
Encoding: tag.DefaultEncoding(),
|
||||
MimeType: "image/jpeg",
|
||||
PictureType: id3v2.PTFrontCover,
|
||||
Description: "Cover",
|
||||
Picture: cover,
|
||||
}
|
||||
tag.AddAttachedPicture(frame)
|
||||
|
||||
return tag.Save()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getCoverImage(id string) ([]byte, error) {
|
||||
url := fmt.Sprintf("https://e-cdn-images.dzcdn.net/images/cover/%s/500x500-000000-80-0-0.jpg", id)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
func addTag(tag *id3v2.Tag, name, value string) {
|
||||
if value != "" {
|
||||
tag.AddTextFrame(name, tag.DefaultEncoding(), value)
|
||||
|
||||
Reference in New Issue
Block a user