Skip to content

Commit

Permalink
#34: removed tmpdir use
Browse files Browse the repository at this point in the history
  • Loading branch information
Girbons committed Jul 15, 2019
1 parent 43e4af1 commit 6939d22
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 188 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ To choose the format use `-images-format` flag, the available formats are:
* png
* jpg

Default is __png__.
Default is __jpg__.

```bash
./comics-downloader -url=[your url] -images-only -images-format=jpg
Expand Down
4 changes: 2 additions & 2 deletions cmd/app/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func download(link, format, country string, all, last, bindLogsToChannel, images
sendToChannel(bindLogsToChannel, msg)
// in case the link is supported
// setup the right strategy to parse a comic
collection, err := sites.LoadComicFromSource(source, u, country, format, all, last)
collection, err := sites.LoadComicFromSource(source, u, country, format, imagesFormat, all, last, imagesOnly)
if err != nil {
log.Error(err)
sendToChannel(bindLogsToChannel, fmt.Sprintf("ERROR: %s", err))
Expand All @@ -65,7 +65,7 @@ func download(link, format, country string, all, last, bindLogsToChannel, images

for _, comic := range collection {
if imagesOnly {
err = comic.DownloadImages(imagesFormat)
_, err = comic.DownloadImages()
} else {
err = comic.MakeComic()
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/downloader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {
deamon := flag.Bool("deamon", false, "Run the download as deamon")
format := flag.String("format", "pdf", "Comic format output, supported formats are pdf,epub,cbr,cbz")
imagesOnly := flag.Bool("images-only", false, "Download comic/manga images")
imagesFormat := flag.String("images-format", "png", "To use with `images-only` flag, choose the image format, available png,jpeg,img")
imagesFormat := flag.String("images-format", "jpg", "To use with `images-only` flag, choose the image format, available png,jpeg,img")
last := flag.Bool("last", false, "Download the last Comic issue")
timeout := flag.Int("timeout", 600, "Timeout (seconds), specifies how often the downloader runs")
url := flag.String("url", "", "Comic URL or Comic URLS by separating each site with a comma without the use of spaces")
Expand Down
241 changes: 90 additions & 151 deletions pkg/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,16 @@ const (
PDF = "pdf"
)

// images format
const (
IMG = "img"
JPG = "jpg"
JPEG = "jpeg"
PNG = "png"
)

// Comic struct contains all the informations about a comic
type Comic struct {
Author string
Name string
IssueNumber string
Source string
URLSource string
Links []string
Format string
Author string
Name string
IssueNumber string
Source string
URLSource string
Links []string
Format string
ImagesFormat string
}

// RetrieveImageFromResponse will return the image byte and its type
Expand Down Expand Up @@ -99,62 +92,34 @@ func (comic *Comic) makeEPUB() error {
if comic.Author != "" {
e.SetAuthor(comic.Author)
}
// in order to create an epub we'll need to download all the images so we create a tempdir for that
tempDir, err := ioutil.TempDir("", "comics-images")

imagesPath, err := comic.DownloadImages()
if err != nil {
return err
}
defer os.RemoveAll(tempDir) // clean up
defer os.RemoveAll(imagesPath)

if err = os.Chdir(tempDir); err != nil {
files, err := ioutil.ReadDir(imagesPath)
if err != nil {
return err
}
// setup the progress bar
bar := progressbar.NewOptions(len(comic.Links), progressbar.OptionSetRenderBlankState(true))

for _, link := range comic.Links {
if link != "" {
rsp, err := http.Get(link)
if err != nil {
return err
}

defer rsp.Body.Close()
// retrieve the image from the response
content, tp, err := comic.retrieveImageFromResponse(rsp)
if err != nil {
return err
}
// create a tempfile to store the image
tmpfile, err := ioutil.TempFile(tempDir, fmt.Sprintf("image.*.%s", tp))

if err != nil {
return err
}
defer os.Remove(tmpfile.Name()) // clean up

if _, err = io.Copy(tmpfile, content); err != nil {
log.Error(err)
}
// add the image to the epub will return a path
imgpath, err := e.AddImage(tmpfile.Name(), "")
for _, file := range files {
// add the image to the epub will return a path
imgpath, err := e.AddImage(fmt.Sprintf("%s/%s", imagesPath, file.Name()), "")
if err != nil {
log.Error(err)
}
// if the cover is not set we'll use the first image
// otherwise the image will be added as a section
if !isCoverSet {
isCoverSet = true
e.SetCover(imgpath, "")
} else {
_, err = e.AddSection(fmt.Sprintf(imgTag, imgpath), "", "", "")
if err != nil {
log.Error(err)
}
// if the cover is not set we'll use the first image
// otherwise the image will be added as a section
if !isCoverSet {
isCoverSet = true
e.SetCover(imgpath, "")
} else {
_, err = e.AddSection(fmt.Sprintf(imgTag, imgpath), "", "", "")
if err != nil {
log.Error(err)
}
}
}
if barErr := bar.Add(1); barErr != nil {
log.Error(barErr)
}
}

Expand All @@ -174,7 +139,6 @@ func (comic *Comic) makeEPUB() error {
}

log.Info(fmt.Sprintf("%s %s", strings.ToUpper(comic.Format), DEFAULT_MESSAGE))

return err
}

Expand All @@ -183,32 +147,36 @@ func (comic *Comic) makePDF() error {
var err error
// setup the pdf
pdf := gofpdf.New("P", "mm", "A4", "")
// setup the progress bar
bar := progressbar.NewOptions(len(comic.Links), progressbar.OptionSetRenderBlankState(true))
// for each link get the image to add to the pdf file
for _, link := range comic.Links {
if link != "" {
rsp, err := http.Get(link)
if err != nil {
return err
}

defer rsp.Body.Close()
// add a new PDF page
pdf.AddPage()
content, tp, err := comic.retrieveImageFromResponse(rsp)
if err != nil {
return err
}
imageOptions := gofpdf.ImageOptions{ImageType: tp, ReadDpi: true, AllowNegativePosition: false}
pdf.RegisterImageOptionsReader(link, imageOptions, content)
// set the image position on the pdf page
pdf.Image(link, 0, 0, 210, 297, false, tp, 0, "")
// increase the progressbar
imagesPath, err := comic.DownloadImages()
if err != nil {
return err
}

defer os.RemoveAll(imagesPath)

files, err := ioutil.ReadDir(imagesPath)
if err != nil {
return err
}

// for each link get the image to add to the pdf file
for _, file := range files {
// add a new PDF page
pdf.AddPage()
if err != nil {
return err
}
if barErr := bar.Add(1); barErr != nil {
log.Error(barErr)
imageOptions := gofpdf.ImageOptions{ImageType: util.ImageType(comic.ImagesFormat), ReadDpi: true, AllowNegativePosition: false}
fileName := fmt.Sprintf("%s/%s", imagesPath, file.Name())
data, err := ioutil.ReadFile(fileName)
if err != nil {
return err
}
content := bytes.NewReader(data)
pdf.RegisterImageOptionsReader(file.Name(), imageOptions, content)
// set the image position on the pdf page
pdf.Image(file.Name(), 0, 0, 210, 297, false, comic.ImagesFormat, 0, "")
}
// get the PathSetup where the file should be saved
// e.g. /www.mangarock.com/comic-name/
Expand All @@ -231,61 +199,24 @@ func (comic *Comic) makeCBRZ() error {
var filesToAdd []string
var err error

currentDir, err := util.CurrentDir()
if err != nil {
return err
}

// setup a new Epub instance
archive := archiver.NewZip()
// in order to create the archive we'll need to download all the images
tempDir, err := ioutil.TempDir("", "comics-images")

imagesPath, err := comic.DownloadImages()
if err != nil {
return err
}
defer os.RemoveAll(tempDir) // clean up
defer os.RemoveAll(imagesPath)

if err = os.Chdir(tempDir); err != nil {
files, err := ioutil.ReadDir(imagesPath)
if err != nil {
return err
}
// setup the progress bar
bar := progressbar.NewOptions(len(comic.Links), progressbar.OptionSetRenderBlankState(true))

for i, link := range comic.Links {
if link != "" {
rsp, err := http.Get(link)
if err != nil {
return err
}

defer rsp.Body.Close()
// retrieve the image from the response
content, tp, err := comic.retrieveImageFromResponse(rsp)
if err != nil {
return err
}
// create a tempfile to store the image
tmpfile, err := ioutil.TempFile(tempDir, fmt.Sprintf("%04d-image.*.%s", i, tp))
defer os.Remove(tmpfile.Name()) // clean up

if err != nil {
return err
}

if _, err = io.Copy(tmpfile, content); err != nil {
return err
}

filesToAdd = append(filesToAdd, tmpfile.Name())
}
if barErr := bar.Add(1); barErr != nil {
log.Error(barErr)
}
for _, file := range files {
filesToAdd = append(filesToAdd, fmt.Sprintf("%s/%s", imagesPath, file.Name()))
}

if err = os.Chdir(currentDir); err != nil {
return err
}
// e.g. /www.mangarock.com/comic-name/
dir, err := util.PathSetup(comic.Source, comic.Name)
if err != nil {
Expand All @@ -308,57 +239,63 @@ func (comic *Comic) makeCBRZ() error {
return err
}

// DownloadImages will download only the comic/manga images
func (comic *Comic) DownloadImages(imagesFormat string) error {
var format string
// DownloadImages will download the comic/manga images
func (comic *Comic) DownloadImages() (string, error) {
var dir string
var err error

switch imagesFormat {
case IMG:
format = IMG
case JPEG, JPG:
format = JPG
default:
format = PNG
dir, err = util.ImagesPathSetup(comic.Source, comic.Name, comic.IssueNumber)
if err != nil {
return dir, err
}

files, err := ioutil.ReadDir(dir)
if err != nil {
return dir, err
}

if !util.DirectoryOrFileDoesNotExist(dir) && len(files) == len(comic.Links) {
log.Info(fmt.Sprintf("Images folder for %s-%s already exist", comic.Name, comic.IssueNumber))
return dir, err
}

format := util.ImageType(comic.ImagesFormat)

currentDir, err := util.CurrentDir()
if err != nil {
return err
return "", err
}

// setup the progress bar
bar := progressbar.NewOptions(len(comic.Links), progressbar.OptionSetRenderBlankState(true))

dir, err := util.PathSetup(comic.Source, comic.Name)
err = os.Chdir(dir)
if err != nil {
return err
return dir, err
}

os.Chdir(dir)

for i, link := range comic.Links {
if link != "" {
rsp, err := http.Get(link)
if err != nil {
return err
return dir, err
}

defer rsp.Body.Close()
// retrieve the image from the response
content, _, err := comic.retrieveImageFromResponse(rsp)
if err != nil {
return err
return dir, err
}

imgFile, err := os.Create(fmt.Sprintf("%04d-image.%s", i, format))

if err != nil {
return err
return dir, err
}

if _, err = io.Copy(imgFile, content); err != nil {
return err
return dir, err
}
}

Expand All @@ -367,10 +304,12 @@ func (comic *Comic) DownloadImages(imagesFormat string) error {
}
}

os.Chdir(currentDir)
err = os.Chdir(currentDir)
if err != nil {
return dir, err
}

log.Info(fmt.Sprintf("Images correctly downloaded for: %s-%s", comic.Name, comic.IssueNumber))
return err
return dir, err
}

// MakeComic will create the file based on the output format selected.
Expand Down

0 comments on commit 6939d22

Please sign in to comment.