first commit

This commit is contained in:
lapwat
2021-09-14 22:21:44 +02:00
commit 14856a109a
12 changed files with 1326 additions and 0 deletions

55
cmd/list.go Normal file
View File

@@ -0,0 +1,55 @@
package cmd
import (
"errors"
"fmt"
"strings"
colly "github.com/gocolly/colly/v2"
cobra "github.com/spf13/cobra"
)
var listCmd = &cobra.Command{
Use: "ls",
Short: "Print table of content",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires an URL argument")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
url := args[0]
c := colly.NewCollector()
// visit and count link classes
classesLinks := map[string][]map[string]string{}
classesCount := map[string]int{}
classMax := ""
c.OnHTML(selector, func(e *colly.HTMLElement) {
href := e.Attr("href")
text := strings.TrimSpace(e.Text)
class := e.Attr("class")
// if class != "" && text != "" {
classesLinks[class] = append(classesLinks[class], map[string]string{
"href": href,
"text": text,
})
classesCount[class]++
if classesCount[class] > classesCount[classMax] {
classMax = class
}
// }
})
c.Visit(url)
for index, link := range classesLinks[classMax] {
fmt.Printf("Chapter %d: %s %s\n", index+1, link["text"], link["href"])
}
},
}