fetching name

This commit is contained in:
Hazel Noack
2025-10-08 11:39:57 +02:00
parent 4a54048feb
commit 82b64fe642
5 changed files with 152 additions and 33 deletions

View File

@@ -25,7 +25,7 @@ type Plugin interface {
}
var namePlugins = map[string]Plugin{}
var nameSourceType = map[string]data.SourceType{}
var NameSourceType = map[string]data.SourceType{}
func RegisterPlugin(plugin Plugin) error {
name := plugin.Name()
@@ -34,7 +34,7 @@ func RegisterPlugin(plugin Plugin) error {
return errors.New("plugin " + name + " is already registered")
}
nameSourceType[name] = data.SourceType{
NameSourceType[name] = data.SourceType{
Name: name,
Regex: plugin.Regex(),
RegexArtist: plugin.RegexArtist(),
@@ -57,7 +57,7 @@ func compileSourceType(source data.Source) (data.Source, error) {
return source, nil
}
for _, st := range nameSourceType {
for _, st := range NameSourceType {
if m := st.Regex.FindString(source.Url); m != "" {
source.Url = m
source.SourceType = &st

View File

@@ -1,15 +1,14 @@
package plugin
import (
"compress/gzip"
"fmt"
"io"
"regexp"
"strings"
"gitea.elara.ws/Hazel/music-kraken/internal/common"
"gitea.elara.ws/Hazel/music-kraken/internal/data"
"gitea.elara.ws/Hazel/music-kraken/internal/scraper"
"github.com/PuerkitoBio/goquery"
)
func extractName(s string) string {
@@ -20,12 +19,14 @@ func extractName(s string) string {
return result
}
const musifyHost = "https://musify.club"
type Musify struct {
session *scraper.Session
}
func (m Musify) Name() string {
return "Musify"
return "musify"
}
func (m Musify) Regex() *regexp.Regexp {
@@ -48,6 +49,62 @@ func (m Musify) RegexSong() *regexp.Regexp {
return regexp.MustCompile(`(?i)https?://musify\.club/track/[a-z\-0-9]+`)
}
func parseArtistContact(contact *goquery.Selection) data.Artist {
artist := data.Artist{}
anchor := contact.Find("a")
if anchor.Length() > 0 {
url, urlExists := anchor.Attr("href")
if urlExists {
artist.Sources = append(artist.Sources, data.Source{
Url: musifyHost + url,
ObjectType: data.ArtistSource,
})
}
if name, nameExists := anchor.Attr("title"); nameExists {
artist.Name = name
}
}
return artist
}
func parseAlbumContact(contact *goquery.Selection) data.Album {
album := data.Album{}
return album
}
func parseContactContainer(contactContainer *goquery.Selection) []data.MusicObject {
res := []data.MusicObject{}
contactContainer.Find("div.contacts__item").Each(func(i int, contact *goquery.Selection) {
anchor := contact.Find("a")
if anchor.Length() > 0 {
url, exists := anchor.Attr("href")
if exists {
if strings.Contains(url, "artist") {
res = append(res, parseArtistContact(contact))
} else if strings.Contains(url, "release") {
res = append(res, parseAlbumContact(contact))
}
}
}
})
return res
}
func parsePlaylist(playlist *goquery.Selection) []data.MusicObject {
res := []data.MusicObject{}
return res
}
func (m *Musify) Search(query common.Query) ([]data.MusicObject, error) {
musicObjects := []data.MusicObject{}
@@ -58,33 +115,22 @@ func (m *Musify) Search(query common.Query) ([]data.MusicObject, error) {
return musicObjects, err
}
defer resp.Body.Close()
var bodyReader io.Reader = resp.Body
// Check if we need to decompress manually
if resp.Header.Get("Content-Encoding") == "gzip" && false {
fmt.Println("Response is gzipped, decompressing...")
gzReader, err := gzip.NewReader(resp.Body)
if err != nil {
panic(err)
}
defer gzReader.Close()
bodyReader = gzReader
}
body, err := io.ReadAll(bodyReader)
if err != nil {
panic(err)
}
fmt.Printf("Response length: %d bytes\n", len(body))
fmt.Println("Content:")
fmt.Println(string(body))
fmt.Println(resp.Header)
fmt.Println(resp.StatusCode)
doc, err := scraper.GetHtml(resp)
if err != nil {
return musicObjects, err
}
doc.Find("div.contacts").Each(func(i int, contactContainer *goquery.Selection) {
musicObjects = append(musicObjects, parseContactContainer(contactContainer)...)
})
doc.Find("div.playlist").Each(func(i int, playlist *goquery.Selection) {
musicObjects = append(musicObjects, parsePlaylist(playlist)...)
})
return musicObjects, nil
}

View File

@@ -79,8 +79,8 @@ func TestRegister(t *testing.T) {
t.Errorf(`%d plugins where registered`, len(namePlugins))
}
if len(nameSourceType) != 1 {
t.Errorf(`%d source types were registered`, len(nameSourceType))
if len(NameSourceType) != 1 {
t.Errorf(`%d source types were registered`, len(NameSourceType))
}
}