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

54
main.go
View File

@ -76,28 +76,10 @@ func renderAll() error {
return fmt.Errorf("unable to create goquery instance: %w", err) return fmt.Errorf("unable to create goquery instance: %w", err)
} }
objs := make(map[string]Object) objs, err := extractObjects(doc)
doc. if err != nil {
Find("body #contents"). return fmt.Errorf("unable to extract categories: %w", err)
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
})
})
if err := renderCategories(objs); err != nil { if err := renderCategories(objs); err != nil {
return fmt.Errorf("unable to render categories: %w", err) return fmt.Errorf("unable to render categories: %w", err)
@ -135,6 +117,34 @@ func dropCreateDir(dir string) error {
return nil 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 { func mkdirAll(path string) error {
_, err := os.Stat(path) _, err := os.Stat(path)
// NOTE: directory is exists // NOTE: directory is exists