enabling gos comression negotiation
This commit is contained in:
parent
018dbe1ef0
commit
4a54048feb
@ -1,11 +1,15 @@
|
|||||||
package plugin
|
package plugin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"compress/gzip"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitea.elara.ws/Hazel/music-kraken/internal/common"
|
"gitea.elara.ws/Hazel/music-kraken/internal/common"
|
||||||
"gitea.elara.ws/Hazel/music-kraken/internal/data"
|
"gitea.elara.ws/Hazel/music-kraken/internal/data"
|
||||||
|
"gitea.elara.ws/Hazel/music-kraken/internal/scraper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func extractName(s string) string {
|
func extractName(s string) string {
|
||||||
@ -17,6 +21,7 @@ func extractName(s string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Musify struct {
|
type Musify struct {
|
||||||
|
session *scraper.Session
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Musify) Name() string {
|
func (m Musify) Name() string {
|
||||||
@ -36,16 +41,51 @@ func (m Musify) RegexAlbum() *regexp.Regexp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Musify) Init() {
|
func (m *Musify) Init() {
|
||||||
|
m.session = scraper.NewSession()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Musify) RegexSong() *regexp.Regexp {
|
func (m Musify) RegexSong() *regexp.Regexp {
|
||||||
return regexp.MustCompile(`(?i)https?://musify\.club/track/[a-z\-0-9]+`)
|
return regexp.MustCompile(`(?i)https?://musify\.club/track/[a-z\-0-9]+`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Musify) Search(query common.Query) ([]data.MusicObject, error) {
|
func (m *Musify) Search(query common.Query) ([]data.MusicObject, error) {
|
||||||
res := []data.MusicObject{}
|
musicObjects := []data.MusicObject{}
|
||||||
return res, nil
|
|
||||||
|
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) {
|
func (m Musify) FetchSong(source data.Source) (data.Song, error) {
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -39,7 +40,6 @@ func NewSession() *Session {
|
|||||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
|
||||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
|
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
|
||||||
"Accept-Language": "en-US,en;q=0.5",
|
"Accept-Language": "en-US,en;q=0.5",
|
||||||
"Accept-Encoding": "gzip, deflate, br",
|
|
||||||
"Connection": "keep-alive",
|
"Connection": "keep-alive",
|
||||||
"Upgrade-Insecure-Requests": "1",
|
"Upgrade-Insecure-Requests": "1",
|
||||||
},
|
},
|
||||||
@ -87,22 +87,26 @@ func (s *Session) Get(url string, headers ...map[string]string) (*http.Response,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Post performs a POST request with form data
|
// Post performs a POST request with form data
|
||||||
func (s *Session) Post(url string, data map[string]string, headers ...map[string]string) (*http.Response, error) {
|
func (s *Session) PostMultipartForm(url string, data map[string]string, headers ...map[string]string) (*http.Response, error) {
|
||||||
fullURL := s.buildURL(url)
|
fullURL := s.buildURL(url)
|
||||||
|
|
||||||
// This is the corrected line - url.Values is from net/url package
|
var requestBody bytes.Buffer
|
||||||
formData := make(url.Values)
|
writer := multipart.NewWriter(&requestBody)
|
||||||
for key, value := range data {
|
for k, v := range data {
|
||||||
formData.Add(key, value)
|
err := writer.WriteField(k, v)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
writer.Close()
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", fullURL, bytes.NewBufferString(formData.Encode()))
|
req, err := http.NewRequest("POST", fullURL, &requestBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
s.setDefaultHeaders(req)
|
s.setDefaultHeaders(req)
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||||
|
|
||||||
// Add any additional headers provided
|
// Add any additional headers provided
|
||||||
if len(headers) > 0 {
|
if len(headers) > 0 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user