154 lines
3.5 KiB
Go
154 lines
3.5 KiB
Go
package plugin
|
|
|
|
import (
|
|
"fmt"
|
|
"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 {
|
|
parts := strings.Split(s, "/")
|
|
lastPart := parts[len(parts)-1]
|
|
hyphenParts := strings.Split(lastPart, "-")
|
|
result := strings.Join(hyphenParts[:len(hyphenParts)-1], " ")
|
|
return result
|
|
}
|
|
|
|
const musifyHost = "https://musify.club"
|
|
|
|
type Musify struct {
|
|
session *scraper.Session
|
|
}
|
|
|
|
func (m Musify) Name() string {
|
|
return "musify"
|
|
}
|
|
|
|
func (m Musify) Regex() *regexp.Regexp {
|
|
return regexp.MustCompile(`(?i)https?://musify\.club/(artist|release|track)/[a-z\-0-9]+`)
|
|
}
|
|
|
|
func (m Musify) RegexArtist() *regexp.Regexp {
|
|
return regexp.MustCompile(`(?i)https?://musify\.club/artist/[a-z\-0-9]+`)
|
|
}
|
|
|
|
func (m Musify) RegexAlbum() *regexp.Regexp {
|
|
return regexp.MustCompile(`(?i)https?://musify\.club/release/[a-z\-0-9]+`)
|
|
}
|
|
|
|
func (m *Musify) Init() {
|
|
m.session = scraper.NewSession()
|
|
}
|
|
|
|
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{}
|
|
|
|
resp, err := m.session.PostMultipartForm("https://musify.club/en/search", map[string]string{
|
|
"SearchText": query.Search, // alternatively I could also add year and genre
|
|
})
|
|
if err != nil {
|
|
return musicObjects, err
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func (m Musify) FetchSong(source data.Source) (data.Song, error) {
|
|
return data.Song{
|
|
Name: extractName(source.Url),
|
|
}, nil
|
|
}
|
|
|
|
func (m Musify) FetchAlbum(source data.Source) (data.Album, error) {
|
|
return data.Album{
|
|
Name: extractName(source.Url),
|
|
}, nil
|
|
}
|
|
|
|
func (m Musify) FetchArtist(source data.Source) (data.Artist, error) {
|
|
return data.Artist{
|
|
Name: extractName(source.Url),
|
|
}, nil
|
|
}
|