shell setup
This commit is contained in:
parent
183b03ee64
commit
8587bdf360
2
go.mod
2
go.mod
@ -1,3 +1,5 @@
|
|||||||
module gitea.elara.ws/Hazel/music-kraken
|
module gitea.elara.ws/Hazel/music-kraken
|
||||||
|
|
||||||
go 1.24.2
|
go 1.24.2
|
||||||
|
|
||||||
|
require golang.org/x/net v0.45.0 // indirect
|
||||||
|
2
go.sum
Normal file
2
go.sum
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
golang.org/x/net v0.45.0 h1:RLBg5JKixCy82FtLJpeNlVM0nrSqpCRYzVU1n8kj0tM=
|
||||||
|
golang.org/x/net v0.45.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY=
|
47
internal/cli/shell.go
Normal file
47
internal/cli/shell.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"gitea.elara.ws/Hazel/music-kraken/internal/data"
|
||||||
|
"gitea.elara.ws/Hazel/music-kraken/internal/plugin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func printResults(musicObjects []data.MusicObject) {
|
||||||
|
for _, m := range musicObjects {
|
||||||
|
if a, ok := m.(data.Artist); ok {
|
||||||
|
fmt.Println("artist: " + a.Name)
|
||||||
|
} else if a, ok := m.(data.Album); ok {
|
||||||
|
fmt.Println("release: " + a.Name)
|
||||||
|
} else if a, ok := m.(data.Song); ok {
|
||||||
|
fmt.Println("track: " + a.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Shell() {
|
||||||
|
plugin.RegisterPlugin(plugin.Musify{})
|
||||||
|
|
||||||
|
for {
|
||||||
|
fmt.Print("> ")
|
||||||
|
|
||||||
|
reader := bufio.NewReader(os.Stdin)
|
||||||
|
line, err := reader.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
searchResults, err := plugin.Search(line, plugin.SearchConfig{IgnoreErrors: false})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println()
|
||||||
|
printResults(searchResults)
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,11 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -116,3 +120,26 @@ func NewQuery(search string) (Query, error) {
|
|||||||
|
|
||||||
return query, nil
|
return query, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestQueryParsing() {
|
||||||
|
for {
|
||||||
|
fmt.Print("> ")
|
||||||
|
|
||||||
|
reader := bufio.NewReader(os.Stdin)
|
||||||
|
line, err := reader.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
query, err := NewQuery(line)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
fmt.Println("search: '" + query.Search + "'")
|
||||||
|
fmt.Println("artist: '" + query.Artist + "'")
|
||||||
|
fmt.Println("album: '" + query.Album + "'")
|
||||||
|
fmt.Println("song: '" + query.Song + "'")
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
181
internal/scraper/session.go
Normal file
181
internal/scraper/session.go
Normal file
@ -0,0 +1,181 @@
|
|||||||
|
package scraper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/http/cookiejar"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/net/publicsuffix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Session represents a persistent HTTP session
|
||||||
|
type Session struct {
|
||||||
|
client *http.Client
|
||||||
|
headers map[string]string
|
||||||
|
baseURL string
|
||||||
|
UserAgent string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSession creates a new session with browser-like headers
|
||||||
|
func NewSession() *Session {
|
||||||
|
// Create cookie jar first
|
||||||
|
jar, err := cookiejar.New(&cookiejar.Options{
|
||||||
|
PublicSuffixList: publicsuffix.List,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Session{
|
||||||
|
client: &http.Client{
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
Jar: jar, // Set the cookie jar
|
||||||
|
},
|
||||||
|
headers: map[string]string{
|
||||||
|
"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-Language": "en-US,en;q=0.5",
|
||||||
|
"Accept-Encoding": "gzip, deflate, br",
|
||||||
|
"Connection": "keep-alive",
|
||||||
|
"Upgrade-Insecure-Requests": "1",
|
||||||
|
},
|
||||||
|
UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetHeader sets a header for all subsequent requests
|
||||||
|
func (s *Session) SetHeader(key, value string) {
|
||||||
|
s.headers[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetHeaders sets multiple headers at once
|
||||||
|
func (s *Session) SetHeaders(headers map[string]string) {
|
||||||
|
for key, value := range headers {
|
||||||
|
s.headers[key] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetBaseURL sets the base URL for relative paths
|
||||||
|
func (s *Session) SetBaseURL(baseURL string) {
|
||||||
|
s.baseURL = baseURL
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get performs a GET request
|
||||||
|
func (s *Session) Get(url string, headers ...map[string]string) (*http.Response, error) {
|
||||||
|
// Use base URL if set and url is relative
|
||||||
|
fullURL := s.buildURL(url)
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", fullURL, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.setDefaultHeaders(req)
|
||||||
|
|
||||||
|
// Add any additional headers provided
|
||||||
|
if len(headers) > 0 {
|
||||||
|
for key, value := range headers[0] {
|
||||||
|
req.Header.Set(key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.client.Do(req)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
fullURL := s.buildURL(url)
|
||||||
|
|
||||||
|
// This is the corrected line - url.Values is from net/url package
|
||||||
|
formData := make(url.Values)
|
||||||
|
for key, value := range data {
|
||||||
|
formData.Add(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", fullURL, bytes.NewBufferString(formData.Encode()))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.setDefaultHeaders(req)
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
|
// Add any additional headers provided
|
||||||
|
if len(headers) > 0 {
|
||||||
|
for key, value := range headers[0] {
|
||||||
|
req.Header.Set(key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.client.Do(req)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PostJSON performs a POST request with JSON data
|
||||||
|
func (s *Session) PostJSON(url string, data interface{}, headers ...map[string]string) (*http.Response, error) {
|
||||||
|
fullURL := s.buildURL(url)
|
||||||
|
|
||||||
|
jsonData, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", fullURL, bytes.NewBuffer(jsonData))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.setDefaultHeaders(req)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
// Add any additional headers provided
|
||||||
|
if len(headers) > 0 {
|
||||||
|
for key, value := range headers[0] {
|
||||||
|
req.Header.Set(key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.client.Do(req)
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildURL constructs the full URL using baseURL if set
|
||||||
|
func (s *Session) buildURL(path string) string {
|
||||||
|
if s.baseURL != "" && !isAbsoluteURL(path) {
|
||||||
|
return s.baseURL + path
|
||||||
|
}
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
|
||||||
|
// isAbsoluteURL checks if the URL is absolute
|
||||||
|
func isAbsoluteURL(urlStr string) bool {
|
||||||
|
u, err := url.Parse(urlStr)
|
||||||
|
return err == nil && u.Scheme != "" && u.Host != ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// setDefaultHeaders sets the default browser-like headers
|
||||||
|
func (s *Session) setDefaultHeaders(req *http.Request) {
|
||||||
|
for key, value := range s.headers {
|
||||||
|
req.Header.Set(key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCookies returns cookies for a given URL
|
||||||
|
func (s *Session) GetCookies(urlStr string) []*http.Cookie {
|
||||||
|
u, err := url.Parse(urlStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return s.client.Jar.Cookies(u)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCookies sets cookies for a given URL
|
||||||
|
func (s *Session) SetCookies(urlStr string, cookies []*http.Cookie) {
|
||||||
|
u, err := url.Parse(urlStr)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.client.Jar.SetCookies(u, cookies)
|
||||||
|
}
|
32
main.go
32
main.go
@ -1,39 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"gitea.elara.ws/Hazel/music-kraken/internal/common"
|
"gitea.elara.ws/Hazel/music-kraken/internal/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testQuery() {
|
|
||||||
for {
|
|
||||||
fmt.Print("> ")
|
|
||||||
|
|
||||||
reader := bufio.NewReader(os.Stdin)
|
|
||||||
line, err := reader.ReadString('\n')
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
query, err := common.NewQuery(line)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
fmt.Println("search: '" + query.Search + "'")
|
|
||||||
fmt.Println("artist: '" + query.Artist + "'")
|
|
||||||
fmt.Println("album: '" + query.Album + "'")
|
|
||||||
fmt.Println("song: '" + query.Song + "'")
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("music kraken")
|
fmt.Println("welcome to music kraken")
|
||||||
|
|
||||||
testQuery()
|
cli.Shell()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user