refacto progress bars, images option

This commit is contained in:
lapwat
2021-10-06 23:41:23 +02:00
parent ee41e49dd1
commit 2d1d5a964a
9 changed files with 189 additions and 113 deletions

View File

@@ -15,9 +15,9 @@ import (
"github.com/lapwat/papeer/book"
)
var quiet, stdout, recursive, include bool
var stdout, recursive, include, images bool
var format, output, selector string
var limit, delay int
var limit, offset, delay int
var getCmd = &cobra.Command{
Use: "get",
@@ -60,6 +60,10 @@ var getCmd = &cobra.Command{
return errors.New("cannot use limit option if not in recursive mode")
}
if cmd.Flags().Changed("offset") && recursive == false {
return errors.New("cannot use offset option if not in recursive mode")
}
if cmd.Flags().Changed("delay") && recursive == false {
return errors.New("cannot use delay option if not in recursive mode")
}
@@ -68,7 +72,7 @@ var getCmd = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
url := args[0]
b := book.NewBookFromURL(url, selector, recursive, include, limit, delay)
b := book.NewBookFromURL(url, selector, recursive, include, images, limit, offset, delay)
if len(output) == 0 {
// set default output
@@ -78,12 +82,16 @@ var getCmd = &cobra.Command{
}
if format == "md" {
f, err := os.Create(output)
if err != nil {
log.Fatal(err)
}
var f *os.File
var err error
defer f.Close()
if !stdout {
f, err = os.Create(output)
if err != nil {
log.Fatal(err)
}
defer f.Close()
}
for _, c := range b.Chapters() {
content, err := md.NewConverter("", true, nil).ConvertString(c.Content())
@@ -96,7 +104,6 @@ var getCmd = &cobra.Command{
if stdout {
fmt.Println(text)
} else {
_, err := f.WriteString(text)
if err != nil {
log.Fatal(err)
@@ -115,8 +122,16 @@ var getCmd = &cobra.Command{
e.SetAuthor(b.Author())
for _, c := range b.Chapters() {
html := fmt.Sprintf("<h1>%s</h1>%s", c.Name(), c.Content())
e.AddSection(html, c.Name(), "", "")
if images {
e.AddSection(c.Content(), "", "", "")
} else {
html := fmt.Sprintf("<h1>%s</h1>%s", c.Name(), c.Content())
_, err := e.AddSection(html, c.Name(), "", "")
if err != nil {
log.Fatal(err)
}
}
}
err := e.Write(output)
@@ -152,7 +167,7 @@ var getCmd = &cobra.Command{
err2 := os.Remove(outputEPUB)
if err2 != nil {
log.Fatal(err)
log.Fatal(err2)
}
}
},

View File

@@ -27,11 +27,17 @@ var listCmd = &cobra.Command{
log.Fatal(err)
}
links := book.GetLinks(base, selector)
links, err := book.GetLinks(base, selector, limit, offset, include)
if err != nil {
log.Fatal(err)
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "Name", "Url", "Class"})
t.Style().Options.DrawBorder = false
t.Style().Options.SeparateColumns = false
t.Style().Options.SeparateHeader = false
t.AppendHeader(table.Row{"#", "Name", "Url"})
for index, link := range links {
u, err := base.Parse(link.Href())
@@ -39,7 +45,7 @@ var listCmd = &cobra.Command{
log.Fatal(err)
}
t.AppendRow([]interface{}{index + 1, link.Text(), u.String(), link.Class()})
t.AppendRow([]interface{}{index + 1, link.Text(), u.String()})
}
t.Render()

View File

@@ -24,13 +24,14 @@ func Execute() {
func init() {
rootCmd.PersistentFlags().StringVarP(&format, "format", "f", "md", "file format [md, epub, mobi]")
rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "", "output file")
rootCmd.PersistentFlags().StringVarP(&selector, "selector", "s", "", "table of content CSS selector")
rootCmd.PersistentFlags().StringVarP(&output, "output", "", "", "output file")
rootCmd.PersistentFlags().StringVarP(&selector, "selector", "s", "", "table of content CSS selector, in resursive mode")
rootCmd.PersistentFlags().BoolVarP(&recursive, "recursive", "r", false, "create one chapter per natigation item")
rootCmd.PersistentFlags().BoolVarP(&include, "include", "i", false, "include URL as first chapter, in resursive mode")
rootCmd.PersistentFlags().BoolVarP(&quiet, "quiet", "q", false, "do not show logs")
rootCmd.PersistentFlags().BoolVarP(&stdout, "stdout", "", false, "print to standard output")
rootCmd.PersistentFlags().BoolVarP(&images, "images", "", false, "retrieve images only")
rootCmd.PersistentFlags().IntVarP(&limit, "limit", "l", -1, "limit number of chapters, in recursive mode")
rootCmd.PersistentFlags().IntVarP(&offset, "offset", "o", 0, "skip first chapters, in recursive mode")
rootCmd.PersistentFlags().IntVarP(&delay, "delay", "d", -1, "time to wait before downloading next chapter, in milliseconds")
rootCmd.AddCommand(getCmd)