extract "object extractor"

This commit is contained in:
Kirill Zhuravlev 2023-02-15 00:42:19 +01:00 committed by Avelino
parent 90bd8d65f3
commit 1e1a153e52
No known key found for this signature in database
GPG Key ID: B345B4D52E98180A

50
main.go
View File

@ -76,29 +76,11 @@ func renderAll() error {
return fmt.Errorf("unable to create goquery instance: %w", err)
}
objs := make(map[string]Object)
doc.
Find("body #contents").
NextFiltered("ul").
Find("ul").
Each(func(_ int, selUl *goquery.Selection) {
selUl.
Find("li a").
Each(func(_ int, s *goquery.Selection) {
selector, exists := s.Attr("href")
if !exists {
return
}
obj, err := makeObjByID(selector, doc)
objs, err := extractObjects(doc)
if err != nil {
return
return fmt.Errorf("unable to extract categories: %w", err)
}
objs[selector] = *obj
})
})
if err := renderCategories(objs); err != nil {
return fmt.Errorf("unable to render categories: %w", err)
}
@ -135,6 +117,34 @@ func dropCreateDir(dir string) error {
return nil
}
func extractObjects(doc *goquery.Document) (map[string]Object, error) {
objs := make(map[string]Object)
doc.
Find("body #contents").
NextFiltered("ul").
Find("ul").
Each(func(_ int, selUl *goquery.Selection) {
selUl.
Find("li a").
Each(func(_ int, s *goquery.Selection) {
selector, exists := s.Attr("href")
if !exists {
return
}
obj, err := makeObjByID(selector, doc)
if err != nil {
return
}
objs[selector] = *obj
})
})
// FIXME: handle error
return objs, nil
}
func mkdirAll(path string) error {
_, err := os.Stat(path)
// NOTE: directory is exists