Skip to content

Commit

Permalink
add json output
Browse files Browse the repository at this point in the history
  • Loading branch information
celogeek committed Sep 30, 2023
1 parent 4a3fc60 commit a552d7a
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 25 deletions.
26 changes: 20 additions & 6 deletions internal/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ It use goflag with additional feature:
package converter

import (
"encoding/json"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -155,6 +156,7 @@ func (c *Converter) InitParse() {
c.AddBoolParam(&c.Options.Dry, "dry", false, "Dry run to show all options")
c.AddBoolParam(&c.Options.DryVerbose, "dry-verbose", false, "Display also sorted files after the TOC")
c.AddBoolParam(&c.Options.Quiet, "quiet", false, "Disable progress bar")
c.AddBoolParam(&c.Options.Json, "json", false, "Output progression and information in Json format")
c.AddBoolParam(&c.Options.Version, "version", false, "Show current and available version")
c.AddBoolParam(&c.Options.Help, "help", false, "Show this help message")
}
Expand Down Expand Up @@ -400,12 +402,24 @@ func (c *Converter) Fatal(err error) {

func (c *Converter) Stats() {
// Display elapse time and memory usage
elapse := time.Since(c.startAt).Round(time.Millisecond)
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
fmt.Fprintf(
os.Stderr,
"Completed in %s, Memory usage %d Mb\n",
time.Since(c.startAt).Round(time.Millisecond),
mem.Sys/1024/1024,
)

if c.Options.Json {
json.NewEncoder(os.Stdout).Encode(map[string]any{
"type": "stats",
"data": map[string]any{
"elapse": elapse,
"memory_usage_mb": mem.Sys / 1024 / 1024,
},
})
} else {
fmt.Fprintf(
os.Stderr,
"Completed in %s, Memory usage %d Mb\n",
elapse,
mem.Sys/1024/1024,
)
}
}
86 changes: 74 additions & 12 deletions internal/converter/options/converter_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Manage options with default value from config.
package options

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -69,6 +70,7 @@ type Options struct {
Dry bool `yaml:"-"`
DryVerbose bool `yaml:"-"`
Quiet bool `yaml:"-"`
Json bool `yaml:"-"`
Version bool `yaml:"-"`
Help bool `yaml:"-"`

Expand Down Expand Up @@ -116,13 +118,73 @@ func (o *Options) String() string {
{"Title", o.Title},
{"Workers", o.Workers},
} {
b.WriteString(fmt.Sprintf("\n %-26s: %v", v.K, v.V))
b.WriteString(fmt.Sprintf("\n %-32s: %v", v.K, v.V))
}
b.WriteString(o.ShowConfig())
b.WriteRune('\n')
return b.String()
}

func (o *Options) MarshalJSON() ([]byte, error) {
out := map[string]any{
"input": o.Input,
"output": o.Output,
"author": o.Author,
"title": o.Title,
"workers": o.Workers,
"profile": o.GetProfile(),
"format": o.Format,
"grayscale": o.Grayscale,
"crop": o.Crop,
"autocontrast": o.AutoContrast,
"autorotate": o.AutoRotate,
"noblankimage": o.NoBlankImage,
"manga": o.Manga,
"hascover": o.HasCover,
"stripfirstdirectoryfromtoc": o.StripFirstDirectoryFromToc,
"sortpathmode": o.SortPathMode,
"foregroundcolor": o.ForegroundColor,
"backgroundcolor": o.BackgroundColor,
"resize": !o.NoResize,
"aspectratio": o.AspectRatio,
"portraitonly": o.PortraitOnly,
"titlepage": o.TitlePage,
}
if o.Format == "jpeg" {
out["quality"] = o.Quality
}
if o.Grayscale {
out["grayscale_mode"] = o.GrayscaleMode
}
if o.Crop {
out["crop_ratio"] = map[string]any{
"left": o.CropRatioLeft,
"right": o.CropRatioRight,
"up": o.CropRatioUp,
"bottom": o.CropRatioBottom,
}
}
if o.Brightness != 0 {
out["brightness"] = o.Brightness
}
if o.Contrast != 0 {
out["contrast"] = o.Contrast
}
if o.PortraitOnly || !o.AppleBookCompatibility {
out["autosplitdoublepage"] = o.AutoSplitDoublePage
if o.AutoSplitDoublePage {
out["keepdoublepageifsplitted"] = o.KeepDoublePageIfSplitted
}
}
if o.LimitMb != 0 {
out["limitmb"] = o.LimitMb
}
if !o.PortraitOnly {
out["applebookcompatibility"] = o.AppleBookCompatibility
}
return json.Marshal(out)
}

// Config file: ~/.go-comic-converter.yaml
func (o *Options) FileName() string {
home, _ := os.UserHomeDir()
Expand Down Expand Up @@ -205,19 +267,19 @@ func (o *Options) ShowConfig() string {
{"Grayscale", o.Grayscale, true},
{"Grayscale Mode", grayscaleMode, o.Grayscale},
{"Crop", o.Crop, true},
{"CropRatio", fmt.Sprintf("%d Left - %d Up - %d Right - %d Bottom", o.CropRatioLeft, o.CropRatioUp, o.CropRatioRight, o.CropRatioBottom), o.Crop},
{"Crop Ratio", fmt.Sprintf("%d Left - %d Up - %d Right - %d Bottom", o.CropRatioLeft, o.CropRatioUp, o.CropRatioRight, o.CropRatioBottom), o.Crop},
{"Brightness", o.Brightness, o.Brightness != 0},
{"Contrast", o.Contrast, o.Contrast != 0},
{"AutoContrast", o.AutoContrast, true},
{"AutoRotate", o.AutoRotate, true},
{"AutoSplitDoublePage", o.AutoSplitDoublePage, o.PortraitOnly || !o.AppleBookCompatibility},
{"KeepDoublePageIfSplitted", o.KeepDoublePageIfSplitted, (o.PortraitOnly || !o.AppleBookCompatibility) && o.AutoSplitDoublePage},
{"NoBlankImage", o.NoBlankImage, true},
{"Auto Contrast", o.AutoContrast, true},
{"Auto Rotate", o.AutoRotate, true},
{"Auto Split DoublePage", o.AutoSplitDoublePage, o.PortraitOnly || !o.AppleBookCompatibility},
{"Keep DoublePage If Splitted", o.KeepDoublePageIfSplitted, (o.PortraitOnly || !o.AppleBookCompatibility) && o.AutoSplitDoublePage},
{"No Blank Image", o.NoBlankImage, true},
{"Manga", o.Manga, true},
{"HasCover", o.HasCover, true},
{"LimitMb", fmt.Sprintf("%d Mb", o.LimitMb), o.LimitMb != 0},
{"StripFirstDirectoryFromToc", o.StripFirstDirectoryFromToc, true},
{"SortPathMode", sortpathmode, true},
{"Has Cover", o.HasCover, true},
{"Limit", fmt.Sprintf("%d Mb", o.LimitMb), o.LimitMb != 0},
{"Strip First Directory From Toc", o.StripFirstDirectoryFromToc, true},
{"Sort Path Mode", sortpathmode, true},
{"Foreground Color", fmt.Sprintf("#%s", o.ForegroundColor), true},
{"Background Color", fmt.Sprintf("#%s", o.BackgroundColor), true},
{"Resize", !o.NoResize, true},
Expand All @@ -227,7 +289,7 @@ func (o *Options) ShowConfig() string {
{"Apple Book Compatibility", o.AppleBookCompatibility, !o.PortraitOnly},
} {
if v.Condition {
b.WriteString(fmt.Sprintf("\n %-30s: %v", v.Key, v.Value))
b.WriteString(fmt.Sprintf("\n %-32s: %v", v.Key, v.Value))
}
}
return b.String()
Expand Down
8 changes: 4 additions & 4 deletions internal/converter/profiles/converter_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
)

type Profile struct {
Code string
Description string
Width int
Height int
Code string `json:"code"`
Description string `json:"description"`
Width int `json:"width"`
Height int `json:"height"`
}

type Profiles []Profile
Expand Down
5 changes: 4 additions & 1 deletion internal/epub/epub.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ func (e *ePub) Write() error {
CurrentJob: 2,
TotalJob: 2,
Quiet: e.Quiet,
Json: e.Json,
})

e.computeViewPort(epubParts)
Expand Down Expand Up @@ -454,7 +455,9 @@ func (e *ePub) Write() error {
bar.Add(1)
}
bar.Close()
fmt.Fprintln(os.Stderr)
if !e.Json {
fmt.Fprintln(os.Stderr)
}

// display corrupted images
hasError := false
Expand Down
1 change: 1 addition & 0 deletions internal/epub/imageprocessor/epub_image_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
// processing
bar := epubprogress.New(epubprogress.Options{
Quiet: e.Quiet,
Json: e.Json,
Max: imageCount,
Description: "Processing",
CurrentJob: 1,
Expand Down
1 change: 1 addition & 0 deletions internal/epub/options/epub_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Options struct {
DryVerbose bool
SortPathMode int
Quiet bool
Json bool
Workers int
Image *Image
}
Expand Down
8 changes: 7 additions & 1 deletion internal/epub/progress/epub_progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

type Options struct {
Quiet bool
Json bool
Max int
Description string
CurrentJob int
Expand All @@ -21,13 +22,18 @@ type Options struct {

type EpubProgress interface {
Add(num int) error
Close() (err error)
Close() error
}

func New(o Options) EpubProgress {
if o.Quiet {
return progressbar.DefaultSilent(int64(o.Max))
}

if o.Json {
return newEpubProgressJson(o)
}

fmtJob := fmt.Sprintf("%%0%dd", len(fmt.Sprint(o.TotalJob)))
fmtDesc := fmt.Sprintf("[%s/%s] %%-15s", fmtJob, fmtJob)
return progressbar.NewOptions(o.Max,
Expand Down
42 changes: 42 additions & 0 deletions internal/epub/progress/epub_progress_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package epubprogress

import (
"encoding/json"
"os"
)

type EpubProgressJson struct {
o Options
e *json.Encoder
current int
}

func newEpubProgressJson(o Options) EpubProgress {
return &EpubProgressJson{
o: o,
e: json.NewEncoder(os.Stdout),
}
}

func (p *EpubProgressJson) Add(num int) error {
p.current += num
p.e.Encode(map[string]any{
"type": "progress",
"data": map[string]any{
"progress": map[string]any{
"current": p.current,
"total": p.o.Max,
},
"steps": map[string]any{
"current": p.o.CurrentJob,
"total": p.o.TotalJob,
},
"description": p.o.Description,
},
})
return nil
}

func (p *EpubProgressJson) Close() error {
return nil
}
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ EPUB is now support by Amazon through [SendToKindle](https://www.amazon.com/gp/s
package main

import (
"encoding/json"
"fmt"
"os"
"runtime/debug"
Expand Down Expand Up @@ -96,7 +97,13 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
cmd.Fatal(err)
}

fmt.Fprintln(os.Stderr, cmd.Options)
if cmd.Options.Json {
json.NewEncoder(os.Stdout).Encode(map[string]any{
"type": "options", "data": cmd.Options,
})
} else {
fmt.Fprintln(os.Stderr, cmd.Options)
}

profile := cmd.Options.GetProfile()

Expand All @@ -113,6 +120,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
Dry: cmd.Options.Dry,
DryVerbose: cmd.Options.DryVerbose,
Quiet: cmd.Options.Quiet,
Json: cmd.Options.Json,
Image: &epuboptions.Image{
Crop: &epuboptions.Crop{Enabled: cmd.Options.Crop, Left: cmd.Options.CropRatioLeft, Up: cmd.Options.CropRatioUp, Right: cmd.Options.CropRatioRight, Bottom: cmd.Options.CropRatioBottom},
Quality: cmd.Options.Quality,
Expand Down

0 comments on commit a552d7a

Please sign in to comment.