fetching name
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user