pkg: they changed the class on the audio tags

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2022-09-06 23:00:39 -04:00
parent a6dff28c54
commit 3124fa7a92
Signed by: vbatts
GPG key ID: 10937E57733F1362

View file

@ -29,7 +29,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings"
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
"github.com/cheggaaa/pb/v3" "github.com/cheggaaa/pb/v3"
@ -40,6 +39,7 @@ type Book struct {
URL string URL string
Title string Title string
Files []string Files []string
Image string
} }
func BookScrape(bookURL string) (*Book, error) { func BookScrape(bookURL string) (*Book, error) {
@ -66,16 +66,22 @@ func BookScrape(bookURL string) (*Book, error) {
b.URL = bookURL b.URL = bookURL
// Find the review items // Find the review items
doc.Find(".lazy-hidden").Each(func(i int, s *goquery.Selection) { doc.Find("source").Each(func(i int, s *goquery.Selection) {
//title := s.Find("source").Text() //title := s.Find("source").Text()
if src, exists := s.Attr("src"); exists && strings.Contains(src, ".mp3") { if t, exists := s.Attr("type"); exists && t == "audio/mpeg" {
//fmt.Println(i, src) src, _ := s.Attr("src")
fmt.Println(i, src)
b.Files = append(b.Files, src) b.Files = append(b.Files, src)
} }
}) })
doc.Find("title").Each(func(i int, s *goquery.Selection) { doc.Find("title").Each(func(i int, s *goquery.Selection) {
b.Title = s.Text() b.Title = s.Text()
}) })
doc.Find("meta").Each(func(i int, s *goquery.Selection) {
if p, exists := s.Attr("property"); exists && p == "og:image" {
b.Image, _ = s.Attr("content")
}
})
return &b, nil return &b, nil
} }
@ -90,6 +96,42 @@ func BookFetcher(b *Book, dest string, createLocal bool) error {
} }
} }
log.Infof("Title: %q", b.Title) log.Infof("Title: %q", b.Title)
log.Infof("Image: %q", b.Image)
err := func() error {
fd, err := os.OpenFile(filepath.Join(p, "cover.jpg"), os.O_RDWR|os.O_CREATE, os.FileMode(0644))
if err != nil {
return err
}
resp, err := http.Get(b.Image)
if err != nil {
return err
}
//log.Infof("%#v", resp)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("status: %d; url: %q", resp.StatusCode, b.Image)
}
size, err := strconv.ParseInt(resp.Header.Get("content-length"), 10, 64)
if err != nil {
size = -1
}
bar := pb.Full.Start64(size)
barReader := bar.NewProxyReader(resp.Body)
i, err := io.Copy(fd, barReader)
if err != nil {
return err
}
bar.Finish()
fd.Close()
log.Infof("wrote 'cover.jpg' (%d)", i)
return nil
}()
if err != nil {
return err
}
for _, f := range b.Files { for _, f := range b.Files {
u, err := url.Parse(f) u, err := url.Parse(f)
@ -123,6 +165,7 @@ func BookFetcher(b *Book, dest string, createLocal bool) error {
return err return err
} }
bar.Finish() bar.Finish()
fd.Close()
log.Infof("wrote %q (%d)", fname, i) log.Infof("wrote %q (%d)", fname, i)
} }
return nil return nil