implemented fetch song

This commit is contained in:
Hazel Noack
2025-10-08 14:36:03 +02:00
parent 6ce1eb5f92
commit 960598971e

View File

@@ -2,6 +2,7 @@ package plugin
import (
"errors"
"fmt"
"regexp"
"strings"
@@ -273,10 +274,106 @@ func (m *Musify) Search(query common.Query) ([]data.MusicObject, error) {
return musicObjects, nil
}
func (m Musify) FetchSong(source data.Source) (data.Song, error) {
return data.Song{
Name: extractName(source.Url),
}, nil
func (m *Musify) FetchSong(source data.Source) (data.Song, error) {
song := data.Song{
Sources: []data.Source{
source,
},
}
resp, err := m.session.Get(source.Url)
if err != nil {
return song, err
}
doc, err := scraper.GetHtml(resp)
if err != nil {
return song, err
}
// Download URL
/*
doc.Find("a[itemprop='audio']").Each(func(i int, anchor *goquery.Selection) {
href, exists := anchor.Attr("href")
if exists {
source.AudioURL = p.host + href
}
})
*/
// Song detail
var listElement *goquery.Selection
doc.Find("ul.album-info").Each(func(i int, albumInfo *goquery.Selection) {
listElement = albumInfo.Find("li").First()
})
if listElement != nil {
listElement.Find("a").Each(func(i int, artistAnchor *goquery.Selection) {
if href, exists := artistAnchor.Attr("href"); exists {
song.Artists = append(song.Artists, data.Artist{
Name: strings.TrimSpace(artistAnchor.Text()),
Sources: []data.Source{
{Url: musifyHost + href},
},
})
}
})
}
// Breadcrumbs
if breadcrumbList := doc.Find("ol.breadcrumb"); breadcrumbList.Length() > 0 {
listPoints := breadcrumbList.Find("li.breadcrumb-item")
if listPoints.Length() != 5 {
return song, errors.New("too many breadcrumbs on page")
}
fmt.Println("found breadcrumbs")
if artistAnchor := listPoints.Eq(2).Find("a"); artistAnchor != nil && artistAnchor.Length() > 0 {
artist := data.Artist{}
useArtist := true
if href, exists := artistAnchor.Attr("href"); exists {
hrefParts := strings.Split(href, "/")
if len(hrefParts) <= 1 || hrefParts[len(hrefParts)-2] != "artist" {
useArtist = false
}
artist.Sources = append(artist.Sources, data.Source{Url: musifyHost + href})
} else {
useArtist = false
}
if nameElem := artistAnchor.Find("span[itemprop='name']"); nameElem.Length() > 0 {
artist.Name = strings.TrimSpace(nameElem.Text())
} else {
useArtist = false
}
if useArtist {
song.Artists = append(song.Artists, artist)
}
}
if albumAnchor := listPoints.Eq(3).Find("a"); albumAnchor != nil && albumAnchor.Length() > 0 {
fmt.Println("found album")
if href, exists := albumAnchor.Attr("href"); exists {
song.Album.Sources = append(song.Album.Sources, data.Source{
Url: musifyHost + href,
})
}
if nameElem := albumAnchor.Find("span[itemprop='name']"); nameElem.Length() > 0 {
song.Album.Name = strings.TrimSpace(nameElem.Text())
}
}
song.Name = strings.TrimSpace(listPoints.Eq(4).Text())
}
return song, nil
}
func (m Musify) FetchAlbum(source data.Source) (data.Album, error) {