From 960598971e419c079b269b71faad8ad54dc9dfea Mon Sep 17 00:00:00 2001 From: Hazel Noack Date: Wed, 8 Oct 2025 14:36:03 +0200 Subject: [PATCH] implemented fetch song --- internal/plugin/musify.go | 105 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 4 deletions(-) diff --git a/internal/plugin/musify.go b/internal/plugin/musify.go index 66a83b2..88a8a44 100644 --- a/internal/plugin/musify.go +++ b/internal/plugin/musify.go @@ -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) {