This commit is contained in:
parent
1acd1c0cab
commit
7cd5e80468
23 changed files with 988 additions and 5 deletions
72
music-bandcamp.go
Normal file
72
music-bandcamp.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
// 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue