aboutsummaryrefslogtreecommitdiff
path: root/direct_client.go
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2020-04-05 15:19:10 +0700
committerShulhan <m.shulhan@gmail.com>2020-04-05 15:19:10 +0700
commit826f56cf7ea4ca350078538d39fd6fee6f05bf9a (patch)
tree2211e5051c97369557811b9e290edd381375ffdb /direct_client.go
parent3061f6561c746f216586c271a10219515dcdb2d7 (diff)
downloadkamusku-826f56cf7ea4ca350078538d39fd6fee6f05bf9a.tar.xz
all: simplify html parser using github.com/shuLhan/share/lib/net/html
Diffstat (limited to 'direct_client.go')
-rw-r--r--direct_client.go64
1 files changed, 27 insertions, 37 deletions
diff --git a/direct_client.go b/direct_client.go
index 7c944e0..8fa19b9 100644
--- a/direct_client.go
+++ b/direct_client.go
@@ -16,7 +16,7 @@ import (
"strings"
"github.com/shuLhan/share/lib/debug"
- "golang.org/x/net/html"
+ "github.com/shuLhan/share/lib/net/html"
"golang.org/x/net/publicsuffix"
)
@@ -231,39 +231,34 @@ func (cl *directClient) parseHTMLKataDasar(htmlBody []byte) (
kataDasar = make(DaftarKata)
- var prev *html.Node
+ prev := html.NewNode(nil)
for {
switch {
- case node.FirstChild != nil && node.FirstChild != prev &&
- node.LastChild != prev:
- node = node.FirstChild
+ case node.FirstChild != nil && node.FirstChild != prev.Node &&
+ node.LastChild != prev.Node:
+ node.Node = node.FirstChild
case node.NextSibling != nil:
- node = node.NextSibling
+ node.Node = node.NextSibling
default:
- prev = node
- node = node.Parent
+ prev.Node = node.Node
+ node.Node = node.Parent
}
- if node == nil {
+ if node.Node == nil {
break
}
-
- if node.Type != html.ElementNode {
+ if !node.IsElement() {
continue
}
if node.Data != tagNameAnchor {
continue
}
- for _, attr := range node.Attr {
- if attr.Key != attrNameHref {
- continue
- }
- if !strings.HasPrefix(attr.Val, entriPath) {
- continue
- }
- k := strings.TrimSpace(node.FirstChild.Data)
- kataDasar[k] = struct{}{}
+ hrefValue := node.GetAttrValue(attrNameHref)
+ if !strings.HasPrefix(hrefValue, entriPath) {
+ continue
}
+ k := strings.TrimSpace(node.FirstChild.Data)
+ kataDasar[k] = struct{}{}
}
return kataDasar, nil
@@ -280,38 +275,33 @@ func (cl *directClient) parseHTMLLogin(htmlBody []byte) (
return "", err
}
- var prev *html.Node
+ prev := html.NewNode(nil)
for {
switch {
- case node.FirstChild != nil && node.FirstChild != prev &&
- node.LastChild != prev:
- node = node.FirstChild
+ case node.FirstChild != nil && node.FirstChild != prev.Node &&
+ node.LastChild != prev.Node:
+ node.Node = node.FirstChild
case node.NextSibling != nil:
- node = node.NextSibling
+ node.Node = node.NextSibling
default:
- prev = node
- node = node.Parent
+ prev.Node = node.Node
+ node.Node = node.Parent
}
- if node == nil {
+ if node.Node == nil {
break
}
- if node.Type != html.ElementNode {
+ if !node.IsElement() {
continue
}
if node.Data != tagNameInput {
continue
}
- for _, attr := range node.Attr {
- if attr.Key != attrNameName {
- continue
- }
- token = getAttrValue(node.Attr)
- if len(token) > 0 {
- return token, nil
- }
+ token := node.GetAttrValue(attrNameName)
+ if len(token) > 0 {
+ return token, nil
}
}