scraping audio url

This commit is contained in:
acute_interpreter_panic
2025-10-10 13:44:00 +02:00
parent 82c541653c
commit 293de8b473
3 changed files with 47 additions and 9 deletions

View File

@@ -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

View File

@@ -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
}
}

View File

@@ -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