feat(tags): add tempo and key

This commit is contained in:
Mathis Maquenne
2025-04-26 00:01:19 +02:00
parent 6b8b17aacb
commit d0599f654e
8 changed files with 127 additions and 6 deletions
+83
View File
@@ -6,7 +6,11 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/mathismqn/godeez/internal/config"
)
@@ -93,3 +97,82 @@ func (s *Song) GetCoverImage() ([]byte, error) {
return io.ReadAll(resp.Body)
}
func (s *Song) GetTempoAndKey() (string, string, error) {
reqUrl := "https://songbpm.com/searches"
values := url.Values{}
values.Add("query", fmt.Sprintf("%s %s %s", s.Artist, s.Title, s.Version))
client := &http.Client{}
req, err := http.NewRequest("POST", reqUrl, bytes.NewBufferString(values.Encode()))
if err != nil {
return "", "", err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Origin", "https://songbpm.com")
resp, err := client.Do(req)
if err != nil {
return "", "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", "", fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return "", "", err
}
var key, bpm string
var found bool
doc.Find("a.flex.flex-col").Each(func(i int, selection *goquery.Selection) {
if found {
return
}
if strings.Contains(selection.Text(), s.Title) && strings.Contains(selection.Text(), s.Artist) {
foundArtist := selection.Find("p.text-sm.font-light.uppercase").Text()
foundTitle := selection.Find("p.pr-2.text-lg").Text()
if strings.Contains(strings.ToLower(foundArtist), strings.ToLower(s.Artist)) && strings.Contains(strings.ToLower(foundTitle), strings.ToLower(s.Title)) {
durationStr := strings.TrimSpace(selection.Find("div.flex-1.flex-col.items-center").Eq(1).Find("span.text-2xl").Text())
parts := strings.Split(durationStr, ":")
if len(parts) != 2 {
return
}
minutes, err := strconv.Atoi(parts[0])
if err != nil {
fmt.Println(err)
return
}
seconds, err := strconv.Atoi(parts[1])
if err != nil {
return
}
foundDuration := minutes*60 + seconds
duration, err := strconv.Atoi(s.Duration)
if err != nil {
return
}
if foundDuration > (duration-2) || foundDuration < (duration+2) {
key = selection.Find("div.flex-1.flex-col.items-center").Eq(0).Find("span.text-2xl").Text()
bpm = selection.Find("div.flex-1.flex-col.items-center").Eq(2).Find("span.text-2xl").Text()
found = true
}
}
}
})
if !found {
return "", "", fmt.Errorf("no data found")
}
return bpm, key, nil
}
+5 -1
View File
@@ -16,7 +16,7 @@ type FLACTagger struct {
Index int
}
func (t *FLACTagger) AddTags(resource deezer.Resource, song *deezer.Song, cover []byte, path string) error {
func (t *FLACTagger) AddTags(resource deezer.Resource, song *deezer.Song, cover []byte, path, tempo, key string) error {
if album, ok := resource.(*deezer.Album); ok {
dateParts := strings.Split(album.Data.PhysicalReleaseDate, "-")
if len(dateParts) == 3 {
@@ -39,6 +39,10 @@ func (t *FLACTagger) AddTags(resource deezer.Resource, song *deezer.Song, cover
t.addTag("REPLAYGAIN_TRACK_GAIN", song.Gain)
t.addTag("ISRC", song.ISRC)
t.addTag("BPM", tempo)
t.addTag("KEY", key)
t.addTag("INITIALKEY", key)
cmtsmeta := t.Cmts.Marshal()
if t.Index > 0 {
t.File.Meta[t.Index] = &cmtsmeta
+4 -1
View File
@@ -13,7 +13,7 @@ type ID3v2Tagger struct {
Tag *id3v2.Tag
}
func (t *ID3v2Tagger) AddTags(resource deezer.Resource, song *deezer.Song, cover []byte, path string) error {
func (t *ID3v2Tagger) AddTags(resource deezer.Resource, song *deezer.Song, cover []byte, path, tempo, key string) error {
defer t.Tag.Close()
duration, _ := strconv.Atoi(song.Duration)
@@ -37,6 +37,9 @@ func (t *ID3v2Tagger) AddTags(resource deezer.Resource, song *deezer.Song, cover
t.addTXXXTag("GAIN", song.Gain)
t.addTXXXTag("ISRC", song.ISRC)
t.addTag("TBPM", tempo)
t.addTag("TKEY", key)
frame := id3v2.PictureFrame{
Encoding: t.Tag.DefaultEncoding(),
MimeType: "image/jpeg",
+3 -3
View File
@@ -10,7 +10,7 @@ import (
)
type Tagger interface {
AddTags(resource deezer.Resource, song *deezer.Song, cover []byte, path string) error
AddTags(resource deezer.Resource, song *deezer.Song, cover []byte, path, tempo, key string) error
}
func NewTagger(filePath string) (Tagger, error) {
@@ -38,7 +38,7 @@ func NewTagger(filePath string) (Tagger, error) {
return &FLACTagger{File: file, Cmts: cmts, Index: idx}, nil
}
func AddTags(resource deezer.Resource, song *deezer.Song, filePath string) error {
func AddTags(resource deezer.Resource, song *deezer.Song, filePath, tempo, key string) error {
tagger, err := NewTagger(filePath)
if err != nil {
return err
@@ -48,5 +48,5 @@ func AddTags(resource deezer.Resource, song *deezer.Song, filePath string) error
return err
}
return tagger.AddTags(resource, song, cover, filePath)
return tagger.AddTags(resource, song, cover, filePath, tempo, key)
}