Skip to content

Commit a0713c7

Browse files
committed
feat(interval) : finish command interval
refacto(utils) : move image processing from `utils/utils.go` to `utils/image.go`
1 parent d39b2b0 commit a0713c7

File tree

8 files changed

+172
-83
lines changed

8 files changed

+172
-83
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
* Add log system
88
* Add command interval
99

10+
### Changed
11+
12+
* move image processing from `utils/utils.go` to `utils/image.go`
13+
1014
## [1.1.0] - 2024-04-21
1115

1216
### Added

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@ Golang adaptation of [scan-to-epub](https://github.com/LordPax/scan-to-epub.git)
1414
http://www.example.com/{chap}/{page}.{png|jpg|jpeg|webp}
1515
```
1616

17-
<!--
18-
## Pre-requis
19-
20-
* Converts .webp files to other image formats and vice versa [libwebp-1.1.0](https://developers.google.com/speed/webp/docs/compiling)
21-
-->
22-
23-
## Installation
17+
## Build and install
2418

2519
1. Clone the repository:
2620

commands/interval.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package commands
22

33
import (
44
"fmt"
5+
"scan2epub/service"
56
"scan2epub/utils"
67

78
cli "github.com/urfave/cli/v2"
@@ -10,22 +11,22 @@ import (
1011
var IntervalCommand = cli.Command{
1112
Name: "inter",
1213
Usage: "Convert at regular intervals and increment the chapter number",
13-
ArgsUsage: "<interval> <chap>",
14+
ArgsUsage: "<cron> <chap>",
1415
Aliases: []string{"i"},
1516
Action: intervalAction,
1617
}
1718

1819
func intervalAction(c *cli.Context) error {
19-
log, err := utils.GetLog()
20-
if err != nil {
21-
return err
22-
}
23-
2420
if c.NArg() < 2 {
25-
return fmt.Errorf("no chapter specified")
21+
return fmt.Errorf("no chapter or cron specified")
2622
}
2723

28-
log.Printf("Converting %d chapters\n", c.NArg())
24+
cronStr := c.Args().Get(0)
25+
chap := c.Args().Get(1)
26+
27+
if err := service.CronDownloadChap(cronStr, chap); err != nil {
28+
return err
29+
}
2930

3031
if err := utils.RmTmpDir(); err != nil {
3132
return err

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module scan2epub
33
go 1.22.2
44

55
require (
6+
github.com/go-co-op/gocron/v2 v2.5.0
67
github.com/go-shiori/go-epub v1.2.1
78
github.com/joho/godotenv v1.5.1
89
github.com/urfave/cli/v2 v2.27.1
@@ -13,8 +14,12 @@ require (
1314
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
1415
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
1516
github.com/gofrs/uuid/v5 v5.0.0 // indirect
17+
github.com/google/uuid v1.6.0 // indirect
18+
github.com/jonboulle/clockwork v0.4.0 // indirect
19+
github.com/robfig/cron/v3 v3.0.1 // indirect
1620
github.com/russross/blackfriday/v2 v2.1.0 // indirect
1721
github.com/vincent-petithory/dataurl v1.0.0 // indirect
1822
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
23+
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
1924
golang.org/x/net v0.19.0 // indirect
2025
)

go.sum

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,42 @@
11
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
22
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
35
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
46
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
7+
github.com/go-co-op/gocron/v2 v2.5.0 h1:ff/TJX9GdTJBDL1il9cyd/Sj3WnS+BB7ZzwHKSNL5p8=
8+
github.com/go-co-op/gocron/v2 v2.5.0/go.mod h1:ckPQw96ZuZLRUGu88vVpd9a6d9HakI14KWahFZtGvNw=
59
github.com/go-shiori/go-epub v1.2.1 h1:+K/WxrvmfFQY69cpryiObrT6X7WhkwpqhHY65AHs2Rg=
610
github.com/go-shiori/go-epub v1.2.1/go.mod h1:3rCTODnigEgy2j3ksndClrGT9h/dcz3js9q4yPX7hf8=
711
github.com/gofrs/uuid/v5 v5.0.0 h1:p544++a97kEL+svbcFbCQVM9KFu0Yo25UoISXGNNH9M=
812
github.com/gofrs/uuid/v5 v5.0.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
13+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
14+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
915
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
1016
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
17+
github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4=
18+
github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc=
19+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
20+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
21+
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
22+
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
1123
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
1224
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
25+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
26+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
1327
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
1428
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
1529
github.com/vincent-petithory/dataurl v1.0.0 h1:cXw+kPto8NLuJtlMsI152irrVw9fRDX8AbShPRpg2CI=
1630
github.com/vincent-petithory/dataurl v1.0.0/go.mod h1:FHafX5vmDzyP+1CQATJn7WFKc9CvnvxyvZy6I1MrG/U=
1731
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
1832
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
33+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
34+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
35+
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f h1:99ci1mjWVBWwJiEKYY6jWa4d2nTQVIEhZIptnrVb1XY=
36+
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI=
1937
golang.org/x/image v0.15.0 h1:kOELfmgrmJlw4Cdb7g/QGuB3CvDrXbqEIww/pNtNBm8=
2038
golang.org/x/image v0.15.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
2139
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
2240
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
41+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
42+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

service/scanToEpub.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package service
33
import (
44
"os"
55
"scan2epub/utils"
6+
"strconv"
7+
8+
cron "github.com/go-co-op/gocron/v2"
69
)
710

811
func Scan2Epub(chaps []string) error {
@@ -31,3 +34,60 @@ func CheckChapExist(chap string) bool {
3134

3235
return workingUrl != ""
3336
}
37+
38+
func CronDownloadChap(cronStr, chap string) error {
39+
c, err := cron.NewScheduler()
40+
if err != nil {
41+
return err
42+
}
43+
44+
currentChap, err := strconv.Atoi(chap)
45+
if err != nil {
46+
return err
47+
}
48+
49+
chanErr := make(chan error)
50+
51+
_, err = c.NewJob(
52+
cron.CronJob(cronStr, false),
53+
cron.NewTask(func() { cronFunc(&currentChap, chanErr) }),
54+
)
55+
if err != nil {
56+
return err
57+
}
58+
59+
c.Start()
60+
61+
if err := <-chanErr; err != nil {
62+
return err
63+
}
64+
65+
return nil
66+
}
67+
68+
func cronFunc(currentChap *int, ch chan<- error) {
69+
log, err := utils.GetLog()
70+
if err != nil {
71+
ch <- err
72+
return
73+
}
74+
75+
log.Printf("Current chapter %d\n", *currentChap)
76+
if !CheckChapExist(strconv.Itoa(*currentChap)) {
77+
log.PrintfErr("Chapter %d not found\n", currentChap)
78+
return
79+
}
80+
81+
if err := downloadChap(strconv.Itoa(*currentChap)); err != nil {
82+
ch <- err
83+
return
84+
}
85+
86+
if err := convertChap(strconv.Itoa(*currentChap)); err != nil {
87+
ch <- err
88+
return
89+
}
90+
91+
*currentChap++
92+
log.Printf("Next chapter %d\n", *currentChap)
93+
}

utils/image.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"image"
6+
"image/jpeg"
7+
"image/png"
8+
"os"
9+
"path"
10+
11+
"golang.org/x/image/webp"
12+
)
13+
14+
// Returns the dimensions of an image
15+
func DimensionsImage(img image.Image) (int, int) {
16+
return img.Bounds().Dx(), img.Bounds().Dy()
17+
}
18+
19+
// Decodes an image from a file
20+
func DecodeImage(file string) (image.Image, string, error) {
21+
f, err := os.Open(file)
22+
if err != nil {
23+
return nil, "", err
24+
}
25+
defer f.Close()
26+
27+
if ext := path.Ext(file); ext == ".webp" {
28+
img, err := webp.Decode(f)
29+
if err != nil {
30+
return nil, "", err
31+
}
32+
return img, "webp", nil
33+
}
34+
35+
img, format, err := image.Decode(f)
36+
if err != nil {
37+
return nil, "", err
38+
}
39+
40+
return img, format, nil
41+
}
42+
43+
// Rotates an image 90 degrees
44+
func RotateImage(img image.Image) *image.NRGBA {
45+
width, height := DimensionsImage(img)
46+
47+
rotated := image.NewNRGBA(image.Rect(0, 0, height, width))
48+
for x := 0; x < width; x++ {
49+
for y := 0; y < height; y++ {
50+
rotated.Set(height-y-1, x, img.At(x, y))
51+
}
52+
}
53+
54+
return rotated
55+
}
56+
57+
// Encodes an image to a file
58+
func EncodeImage(file string, img image.Image, format string) error {
59+
f, err := os.Create(file)
60+
if err != nil {
61+
return err
62+
}
63+
defer f.Close()
64+
65+
switch format {
66+
case "jpeg", "jpg":
67+
return jpeg.Encode(f, img, nil)
68+
case "png":
69+
return png.Encode(f, img)
70+
}
71+
72+
return fmt.Errorf("Unsupported format %s", format)
73+
}

utils/utils.go

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
package utils
22

33
import (
4-
"fmt"
5-
"image"
6-
"image/jpeg"
7-
"image/png"
84
"os"
9-
"path"
10-
11-
"golang.org/x/image/webp"
125
)
136

147
func FileExist(file string) bool {
@@ -19,64 +12,3 @@ func FileExist(file string) bool {
1912
func RmTmpDir() error {
2013
return os.RemoveAll(os.Getenv("TMP_DIR"))
2114
}
22-
23-
// Returns the dimensions of an image
24-
func DimensionsImage(img image.Image) (int, int) {
25-
return img.Bounds().Dx(), img.Bounds().Dy()
26-
}
27-
28-
// Decodes an image from a file
29-
func DecodeImage(file string) (image.Image, string, error) {
30-
f, err := os.Open(file)
31-
if err != nil {
32-
return nil, "", err
33-
}
34-
defer f.Close()
35-
36-
if ext := path.Ext(file); ext == ".webp" {
37-
img, err := webp.Decode(f)
38-
if err != nil {
39-
return nil, "", err
40-
}
41-
return img, "webp", nil
42-
}
43-
44-
img, format, err := image.Decode(f)
45-
if err != nil {
46-
return nil, "", err
47-
}
48-
49-
return img, format, nil
50-
}
51-
52-
// Rotates an image 90 degrees
53-
func RotateImage(img image.Image) *image.NRGBA {
54-
width, height := DimensionsImage(img)
55-
56-
rotated := image.NewNRGBA(image.Rect(0, 0, height, width))
57-
for x := 0; x < width; x++ {
58-
for y := 0; y < height; y++ {
59-
rotated.Set(height-y-1, x, img.At(x, y))
60-
}
61-
}
62-
63-
return rotated
64-
}
65-
66-
// Encodes an image to a file
67-
func EncodeImage(file string, img image.Image, format string) error {
68-
f, err := os.Create(file)
69-
if err != nil {
70-
return err
71-
}
72-
defer f.Close()
73-
74-
switch format {
75-
case "jpeg", "jpg":
76-
return jpeg.Encode(f, img, nil)
77-
case "png":
78-
return png.Encode(f, img)
79-
}
80-
81-
return fmt.Errorf("Unsupported format %s", format)
82-
}

0 commit comments

Comments
 (0)