book-fetch/main.go
Vincent Batts 8fcf30be13
initial commit: collect the book info
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2021-05-17 07:52:48 -05:00

61 lines
1.6 KiB
Go

package main
import (
"fmt"
"log"
"net/http"
"strings"
"github.com/PuerkitoBio/goquery"
)
type Book struct {
URL string
Title string
Files []string
}
func BookScrape(bookURL string) (*Book, error) {
// Request the HTML page.
res, err := http.Get(bookURL)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("status code error: %d %s", res.StatusCode, res.Status)
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, err
}
/*
<audio class="wp-audio-shortcode" id="audio-24287-11" preload="none" style="width: 100%;" controls="controls"><source class="lazy lazy-hidden" type="audio/mpeg" src="https://ipaudio.club/wp-content/uploads/GOLN/Water%20Dancer%20(Ta%20Nehisi%20Coates)/11.mp3?_=11" /><a href="https://ipaudio.club/wp-content/uploads/GOLN/Water%20Dancer%20(Ta%20Nehisi%20Coates)/11.mp3">https://ipaudio.club/wp-content/uploads/GOLN/Water%20Dancer%20(Ta%20Nehisi%20Coates)/11.mp3</a></audio>
*/
b := Book{}
b.URL = bookURL
// Find the review items
doc.Find(".lazy-hidden").Each(func(i int, s *goquery.Selection) {
//title := s.Find("source").Text()
if src, exists := s.Attr("src"); exists && strings.Contains(src, ".mp3") {
fmt.Println(i, src)
b.Files = append(b.Files, src)
}
})
doc.Find("title").Each(func(i int, s *goquery.Selection) {
b.Title = s.Text()
})
return &b, nil
}
func main() {
b, err := BookScrape("https://goldenaudiobooks.com/the-water-dancer-oprahs-book-club-audiobook/")
if err != nil {
log.Fatal(err)
}
fmt.Println(b)
}