Browse Source

comics: added hb

Signed-off-by: gryffyn <me@neveris.one>
main
gryffyn 2 months ago
parent
commit
b8bd51bb98
Signed by: gryffyn GPG Key ID: 6948DD6514D02BEF
  1. 19
      cmd/cmd.go
  2. 58
      comics/hb.go
  3. 8
      comics/utils.go

19
cmd/cmd.go

@ -99,18 +99,25 @@ func Run() {
if last == "" {
bar := progressbar.Default(1)
err = comics.GetGGARStrip(fi, dir, bar)
if text {
err = comics.GetGGARText(fi, dir)
}
} else {
li, _ := strconv.Atoi(last)
max := li - fi + 1
bar := progressbar.Default(int64(max - 1))
err = dlstrip.GetAllInt(comics.GenIntArray(fi, li), dir, bar, comics.GetGGARStrip)
if text {
err = dlstrip.GetAllText(comics.GenIntArray(fi, li), dir, comics.GetGGARText)
}
}
} else if strings.ToLower(c.String("comic")) == "hb" {
fi, _ := strconv.Atoi(first)
if last == "" {
bar := progressbar.Default(1)
err = comics.GetHBStrip(fi, dir, bar)
} else {
li, _ := strconv.Atoi(last)
max := li - fi + 1
bar := progressbar.Default(int64(max - 1))
err = dlstrip.GetAllInt(comics.GenIntArray(fi, li), dir, bar, comics.GetHBStrip)
}
} else {
log.Fatalln("Comic not found.")
}
fmt.Println("\nFinished downloading.")
return err

58
comics/hb.go

@ -0,0 +1,58 @@
package comics
import (
"fmt"
"io"
"net/http"
"os"
"strconv"
"github.com/schollz/progressbar/v3"
"golang.org/x/net/html"
)
func GetHBStrip(strip int, filepath string, bar *progressbar.ProgressBar) error {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://www.headlessbliss.com/comic/page-"+strconv.Itoa(strip+1), nil)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
var url string
doc, _ := html.Parse(resp.Body)
var f func(*html.Node)
f = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "img" {
if len(n.Attr) == 3 {
if n.Attr[2].Val == "cc-comic" {
url = n.Attr[1].Val
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(doc)
reqStrip, err := http.NewRequest("GET", url, nil)
respStrip, err := client.Do(reqStrip)
if err != nil {
return err
}
defer respStrip.Body.Close()
var out *os.File
out, err = os.Create(filepath + fmt.Sprintf("%04d", strip+1) + url[len(url)-4:])
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, respStrip.Body)
bar.Add(1)
return err
}

8
comics/utils.go

@ -3,6 +3,7 @@ package comics
import (
"bytes"
"io"
"net/http"
"time"
)
@ -35,3 +36,10 @@ func isPNG(input io.Reader) (io.Reader, bool, error) {
isGzip := buf[0] == 137 && buf[1] == 80 && buf[2] == 78 && buf[3] == 71
return io.MultiReader(bytes.NewReader(buf[:n]), input), isGzip, nil
}
func Get(url string) (*http.Response, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
resp, err := client.Do(req)
return resp, err
}
Loading…
Cancel
Save