From 293de8b473228e96a7b3233f5d0be32a66aa6422 Mon Sep 17 00:00:00 2001 From: acute_interpreter_panic <223899499+acute-interpreter-panic@users.noreply.github.com> Date: Fri, 10 Oct 2025 13:44:00 +0200 Subject: [PATCH] scraping audio url --- internal/cli/shell.go | 2 +- internal/common/strings.go | 2 +- internal/plugin/musify.go | 52 +++++++++++++++++++++++++++++++++----- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/internal/cli/shell.go b/internal/cli/shell.go index 79d291a..f892de3 100644 --- a/internal/cli/shell.go +++ b/internal/cli/shell.go @@ -49,7 +49,7 @@ func printResults(musicObjects []data.MusicObject) { sources := m.GetSources() if len(sources) > 0 { for _, source := range sources { - results[i] += "\n\t- " + source.SourceType.Name + " " + string(source.ObjectType) + " " + source.Url + results[i] += "\n\t- " + source.SourceType.Name + " " + string(source.ObjectType) + " " + source.Url + " " + source.AudioUrl } } else { results[i] = color.StrikeThrough + results[i] + color.Reset diff --git a/internal/common/strings.go b/internal/common/strings.go index 42eae54..a6efdaa 100644 --- a/internal/common/strings.go +++ b/internal/common/strings.go @@ -24,7 +24,7 @@ func ZeroPad(num int, length int) string { func IsNumeric(num string) bool { for _, c := range num { - if c >= '0' && c <= '9' { + if c < '0' || c > '9' { return false } } diff --git a/internal/plugin/musify.go b/internal/plugin/musify.go index d1434ec..5546355 100644 --- a/internal/plugin/musify.go +++ b/internal/plugin/musify.go @@ -280,6 +280,39 @@ func (m *Musify) Search(query common.Query) ([]data.MusicObject, error) { return musicObjects, nil } +type parsedSongUrl struct { + id string + name string + url string +} + +func newParsedSongUrl(rawUrl string) (parsedSongUrl, error) { + res := parsedSongUrl{ + url: rawUrl, + } + + parsed, err := url.Parse(rawUrl) + if err != nil { + return res, err + } + + dirs := strings.Split(parsed.Path, "/") + correctPart := dirs[len(dirs)-1] + split := strings.Split(correctPart, "-") + if len(split) < 2 { + return res, errors.New("last part of path has to consist of at least one - " + correctPart) + } + + res.id = strings.TrimSpace(split[len(split)-1]) + res.name = strings.Join(split[:len(split)-1], "-") + + if !common.IsNumeric(res.id) { + return res, errors.New("last elem (id) has to be numeric " + res.id) + } + + return res, nil +} + func (m *Musify) FetchSong(source data.Source) (data.Song, error) { song := data.Song{ Sources: []data.Source{ @@ -298,14 +331,19 @@ func (m *Musify) FetchSong(source data.Source) (data.Song, error) { } // 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 + doc.Find("a[itemprop='audio']").Each(func(i int, anchor *goquery.Selection) { + if href, _ := anchor.Attr("href"); true { + // will be the source first added at the begining + song.Sources[0].AudioUrl = musifyHost + href + } else { + // http://musify.club/track/dl/7141298/crystal-f-sekundenschlaf.mp3 + parsed, err := newParsedSongUrl(song.Sources[0].Url) + if err != nil { + return } - }) - */ + song.Sources[0].AudioUrl = "http://musify.club/track/dl/" + parsed.id + "/" + parsed.name + ".mp3" + } + }) // Song detail var listElement *goquery.Selection