Skip to content

Commit

Permalink
Merge pull request #12 from pjkaufman/main
Browse files Browse the repository at this point in the history
Allow CBZ Files to Set Image Ext to Match Webpage Image Type
  • Loading branch information
robinovitch61 authored Aug 29, 2024
2 parents f3643c8 + add766c commit 6e756b0
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"encoding/json"
"flag"
"fmt"
"github.com/anaskhan96/soup"
"github.com/signintech/gopdf"
"image"
"io"
"math"
Expand All @@ -18,6 +16,9 @@ import (
"strconv"
"strings"
"time"

"github.com/anaskhan96/soup"
"github.com/signintech/gopdf"
)

type MotiontoonJson struct {
Expand All @@ -33,7 +34,7 @@ type EpisodeBatch struct {
}

type ComicFile interface {
addImage([]byte) error
addImage([]byte, string) error
save(outFile string) error
}

Expand All @@ -50,7 +51,7 @@ func newPDFComicFile() *PDFComicFile {
return &PDFComicFile{pdf: &pdf}
}

func (c *PDFComicFile) addImage(img []byte) error {
func (c *PDFComicFile) addImage(img []byte, ext string) error {
holder, err := gopdf.ImageHolderByBytes(img)
if err != nil {
return err
Expand Down Expand Up @@ -91,8 +92,8 @@ func newCBZComicFile() (*CBZComicFile, error) {
return &CBZComicFile{zipWriter: zipWriter, buffer: buffer, numFiles: 0}, nil
}

func (c *CBZComicFile) addImage(img []byte) error {
f, err := c.zipWriter.Create(fmt.Sprintf("%010d.jpg", c.numFiles))
func (c *CBZComicFile) addImage(img []byte, ext string) error {
f, err := c.zipWriter.Create(fmt.Sprintf("%010d.%s", c.numFiles, ext))
if err != nil {
return err
}
Expand Down Expand Up @@ -431,12 +432,20 @@ func main() {
var err error
outFile := getOutFile(opts, episodeBatch)
comicFile := getComicFile(opts.format)
var lowercaseImgLink string
for idx, imgLink := range episodeBatch.imgLinks {
if strings.Contains(imgLink, ".gif") {
lowercaseImgLink = strings.ToLower(imgLink)
if strings.Contains(lowercaseImgLink, ".gif") {
fmt.Println(fmt.Sprintf("WARNING: skipping gif %s", imgLink))
continue
}
err := comicFile.addImage(fetchImage(imgLink))

if strings.Contains(lowercaseImgLink, ".png") {
err = comicFile.addImage(fetchImage(imgLink), "png")
} else {
err = comicFile.addImage(fetchImage(imgLink), "jpg")
}

if err != nil {
fmt.Println(err.Error())
os.Exit(1)
Expand Down

0 comments on commit 6e756b0

Please sign in to comment.