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
1 changed files with 47 additions and 4 deletions

View File

@ -29,7 +29,6 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/cheggaaa/pb/v3"
@ -40,6 +39,7 @@ type Book struct {
URL string
Title string
Files []string
Image string
}
func BookScrape(bookURL string) (*Book, error) {
@ -66,16 +66,22 @@ func BookScrape(bookURL string) (*Book, error) {
b.URL = bookURL
// 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()
if src, exists := s.Attr("src"); exists && strings.Contains(src, ".mp3") {
//fmt.Println(i, src)
if t, exists := s.Attr("type"); exists && t == "audio/mpeg" {
src, _ := s.Attr("src")
fmt.Println(i, src)
b.Files = append(b.Files, src)
}
})
doc.Find("title").Each(func(i int, s *goquery.Selection) {
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
}
@ -90,6 +96,42 @@ func BookFetcher(b *Book, dest string, createLocal bool) error {
}
}
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 {
u, err := url.Parse(f)
@ -123,6 +165,7 @@ func BookFetcher(b *Book, dest string, createLocal bool) error {
return err
}
bar.Finish()
fd.Close()
log.Infof("wrote %q (%d)", fname, i)
}
return nil