72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
// music-bandcamp.go - Bandcamp specific implementation
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
)
|
|
|
|
func SearchBandcamp(query string, page int) ([]MusicResult, error) {
|
|
baseURL := "https://bandcamp.com/search?"
|
|
params := url.Values{
|
|
"q": []string{query},
|
|
"page": []string{fmt.Sprintf("%d", page)},
|
|
}
|
|
|
|
resp, err := http.Get(baseURL + params.Encode())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("request failed: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse HTML: %v", err)
|
|
}
|
|
|
|
var results []MusicResult
|
|
|
|
doc.Find("li.searchresult").Each(func(i int, s *goquery.Selection) {
|
|
result := MusicResult{Source: "Bandcamp"}
|
|
|
|
// URL extraction
|
|
if urlSel := s.Find("div.itemurl a"); urlSel.Length() > 0 {
|
|
result.URL = strings.TrimSpace(urlSel.Text())
|
|
}
|
|
|
|
// Title extraction
|
|
if titleSel := s.Find("div.heading a"); titleSel.Length() > 0 {
|
|
result.Title = strings.TrimSpace(titleSel.Text())
|
|
}
|
|
|
|
// Artist extraction
|
|
if artistSel := s.Find("div.subhead"); artistSel.Length() > 0 {
|
|
result.Artist = strings.TrimSpace(artistSel.Text())
|
|
}
|
|
|
|
// Thumbnail extraction
|
|
if thumbSel := s.Find("div.art img"); thumbSel.Length() > 0 {
|
|
result.Thumbnail, _ = thumbSel.Attr("src")
|
|
}
|
|
|
|
// // Iframe URL construction
|
|
// if linkHref, exists := s.Find("div.itemurl a").Attr("href"); exists {
|
|
// if itemID := extractSearchItemID(linkHref); itemID != "" {
|
|
// itemType := strings.ToLower(strings.TrimSpace(s.Find("div.itemtype").Text()))
|
|
// result.IframeSrc = fmt.Sprintf(
|
|
// "https://bandcamp.com/EmbeddedPlayer/%s=%s/size=large/bgcol=000/linkcol=fff/artwork=small",
|
|
// itemType,
|
|
// itemID,
|
|
// )
|
|
// }
|
|
// }
|
|
|
|
results = append(results, result)
|
|
})
|
|
|
|
return results, nil
|
|
}
|