108 lines
2.4 KiB
Go
108 lines
2.4 KiB
Go
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"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
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 (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
|
|
}
|
|
|
|
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)
|
|
|
|
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
|
|
}
|