Girbons / comics-downloader

tool to download comics and manga in pdf/epub/cbr/cbz from a website
MIT License
491 stars 54 forks source link

[Suggestion] - Output to CBR, or CBZ instead of PDF #4

Closed bashfulrobot closed 5 years ago

bashfulrobot commented 5 years ago

I have a suggestion since the tool is just downloading the jpg files anyways, you should allow the tool to output to a CBR or CBZ file. This is a standard format used by comic readers. These formats are simply a zip file (CBZ) renamed with the cbz file extension, or a rar file (CBR) renamed with the cbr file extension.

This way people can consume the files within their favourite comic reader.

In theory, could also support the epub format for those who read them on an e-ink reader (in black and white).

bashfulrobot commented 5 years ago

OOPS - each cbr and cbz format simple contain jpg files. Likely have naming requirements (i.e. - numerical).

Here is an article covering the requirements (for reference).

http://xylasoft.com/xylamic/how-to-create-a-comic-book-archive-cbz-or-cbr/

bashfulrobot commented 5 years ago

Looks like doing a version of:

// MakeComic create the pdf file
func (c *Comic) MakeComic() {
    var (
        tp      string
        content io.Reader
    )
    log.Debug("Image Download Started")
    // setup the pdf
    pdf := gofpdf.New("P", "mm", "A4", "")
    // setup the progress bar
    bar := progressbar.New(len(c.Links))
    // this will show up the progress bar since the beginning
    bar.RenderBlank()
    // for each link get the image to add to the pdf file
    for i, link := range c.Links {
        if link != "" {
            rsp, err := http.Get(link)
            if err == nil {
                defer rsp.Body.Close()
                // add a new PDF page
                pdf.AddPage()
                switch c.Source {
                case "mangarock.com":
                    // mangarock image needs to be decoded first
                    // then converted to a `png` since `gofpdf` does not support webp format yet
                    img, decErr := mri.Decode(rsp.Body)
                    if decErr != nil {
                        log.Error("[Mangarock] Image decode failed", decErr)
                    }
                    imgData := new(bytes.Buffer)
                    util.ConvertTo8BitPNG(img, imgData)
                    tp = "png"
                    content = imgData
                default:
                    // retrieve the image format from the response header (jpeg, png...)
                    tp = pdf.ImageTypeFromMime(rsp.Header["Content-Type"][0])
                    content = rsp.Body
                }
                // The image is directly added to the pdf without being saved to the disk
                pdf.RegisterImageOptionsReader(link, gofpdf.ImageOptions{tp, false}, content)
                // Here we set the image position on the pdf page
                pdf.Image(link, 0, 0, 210, 0, false, tp, 0, "")
                // increase the progressbar
                bar.Add(i)
            } else {
                log.Error("Something went wrong with url: ", link, err)
                pdf.SetError(err)
            }
        }
    }

modified to create and rename the file would do it.

Girbons commented 5 years ago

Thank you for the suggestions,

Instead of doing another version we can move the logic out of the MakeComic. We could add a parameter like -output then select the correct way to save the file. Anyway If you'd like to work on one of these integrations feel free to do it :)

bashfulrobot commented 5 years ago

Nice job on the Epub format in the latest release. At some point I'm going to review that code and see how you implemented it organisationally in your project. Might give a hint on how to do this specific job.

Girbons commented 5 years ago

Would be great to have a code review 🙏

I was thinking to reuse the logic inside the makeEpub() to create the CBR and CBZ.

bashfulrobot commented 5 years ago

@Girbons That sounds good. Sorry - I have been a little tied up with some other tasks, but do have this on my todo list.

Girbons commented 5 years ago

I'm busy too this period, I don't have so much time to dedicate to my personal projects. I started to work on a refactoring branch that will be the base of the 0.6 version but as I said I didn't have time to complete it.