accept several urls, fix tests, remove unused book struct

This commit is contained in:
lapwat
2023-01-18 00:52:48 +01:00
parent bf3fc65444
commit ab5e2ede07
8 changed files with 73 additions and 114 deletions

View File

@@ -141,7 +141,7 @@ go install github.com/lapwat/papeer@latest
```sh
# use platform=darwin for MacOS
platform=linux
release=0.5.6
release=0.6.0
# download and extract
curl -L https://github.com/lapwat/papeer/releases/download/v$release/papeer-v$release-$platform-amd64.tar.gz > papeer.tar.gz
@@ -154,7 +154,7 @@ sudo mv papeer /usr/local/bin
### Windows
Download [latest release](https://github.com/lapwat/papeer/releases/download/v0.5.6/papeer-v0.5.6-windows-amd64.zip).
Download [latest release](https://github.com/lapwat/papeer/releases/download/v0.6.0/papeer-v0.6.0-windows-amd64.zip).
## MOBI support

View File

@@ -1,27 +0,0 @@
package book
type book struct {
name string
author string
chapters []chapter
}
func New(name, author string) book {
return book{name, author, []chapter{}}
}
func (b *book) AddChapter(c chapter) {
b.chapters = append(b.chapters, c)
}
func (b book) Name() string {
return b.name
}
func (b book) Author() string {
return b.name
}
func (b *book) Chapters() []chapter {
return b.chapters
}

View File

@@ -9,6 +9,10 @@ type chapter struct {
config *ScrapeConfig
}
func NewEmptyChapter() chapter {
return chapter{"", "", "", "", []chapter{}, NewScrapeConfigNoInclude()}
}
func NewChapter(body, name, author, content string, subChapters []chapter, config *ScrapeConfig) chapter {
return chapter{body, name, author, content, subChapters, config}
}
@@ -21,6 +25,10 @@ func (c chapter) Name() string {
return c.name
}
func (c *chapter) SetName(name string) {
c.name = name
}
func (c chapter) Author() string {
return c.author
}
@@ -32,3 +40,7 @@ func (c chapter) Content() string {
func (c chapter) SubChapters() []chapter {
return c.subChapters
}
func (c *chapter) AddSubChapter(newChapter chapter) {
c.subChapters = append(c.subChapters, newChapter)
}

File diff suppressed because one or more lines are too long

View File

@@ -36,6 +36,10 @@ func NewScrapeConfig() *ScrapeConfig {
return &ScrapeConfig{0, "", false, -1, 0, false, -1, -1, true, false, false}
}
func NewScrapeConfigNoInclude() *ScrapeConfig {
return &ScrapeConfig{0, "", false, -1, 0, false, -1, -1, false, false, false}
}
func NewScrapeConfigs(selectors []string) []*ScrapeConfig {
configs := []*ScrapeConfig{}
@@ -93,46 +97,6 @@ func NewScrapeConfigFake() *ScrapeConfig {
return config
}
func NewBookFromURL(url string, selector []string, name, author string, include, ImagesOnly, useLinkName, quiet bool, limit, offset, delay, threads int) book {
config1 := NewScrapeConfig()
config1.ImagesOnly = ImagesOnly
config1.UseLinkName = useLinkName
var chapters []chapter
var home chapter
if len(selector) > 0 {
config2 := NewScrapeConfig()
config2.Selector = selector[0]
config2.Limit = limit
config2.Offset = offset
config2.Delay = delay
config2.Threads = threads
config2.Include = include
config2.ImagesOnly = ImagesOnly
config2.UseLinkName = useLinkName
chapters, home = tableOfContent(url, config2, config1, quiet)
} else {
chapters = []chapter{NewChapterFromURL(url, "", []*ScrapeConfig{config1}, 0, func(index int, name string) {})}
home = chapters[0]
}
if len(name) == 0 {
name = home.Name()
}
if len(author) == 0 {
author = home.Author()
}
b := New(name, author)
for _, c := range chapters {
b.AddChapter(c)
}
return b
}
func NewChapterFromURL(url, linkName string, configs []*ScrapeConfig, index int, updateProgressBarName func(index int, name string)) chapter {
config := configs[0]
@@ -397,6 +361,8 @@ func GetLinks(url *urllib.URL, selector string, limit, offset int, reverse, incl
parser := gofeed.NewParser()
feed, err := parser.ParseURL(url.String())
fmt.Println(feed, url.String(), err)
if err == nil {
// RSS feed

File diff suppressed because one or more lines are too long

View File

@@ -132,7 +132,6 @@ var getCmd = &cobra.Command{
return nil
},
Run: func(cmd *cobra.Command, args []string) {
url := args[0]
// generate config for each level
configs := make([]*book.ScrapeConfig, len(getOpts.Selector))
@@ -162,7 +161,13 @@ var getCmd = &cobra.Command{
configs[index] = config
}
c := book.NewChapterFromURL(url, "", configs, 0, func(index int, name string) {})
// dummy root chapter to contain all subchapters
c := book.NewEmptyChapter()
for _, u := range args {
newChapter := book.NewChapterFromURL(u, "", configs, 0, func(index int, name string) {})
c.AddSubChapter(newChapter)
}
c.SetName(c.SubChapters()[0].Name())
if getOpts.Format == "md" {
filename := book.ToMarkdown(c, getOpts.output)

View File

@@ -14,6 +14,6 @@ var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of papeer",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("papeer v0.5.6")
fmt.Println("papeer v0.6.0")
},
}