summaryrefslogtreecommitdiff
path: root/href.go
diff options
context:
space:
mode:
Diffstat (limited to 'href.go')
-rw-r--r--href.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/href.go b/href.go
new file mode 100644
index 0000000..9ba955f
--- /dev/null
+++ b/href.go
@@ -0,0 +1,36 @@
+package main
+
+import (
+ "net/http"
+
+ "golang.org/x/net/html"
+)
+
+func findTitle(n *html.Node) string {
+ if n.Type == html.ElementNode && n.Data == "title" {
+ if c := n.FirstChild; n != nil {
+ return c.Data
+ }
+ }
+ for c := n.FirstChild; c != nil; c = c.NextSibling {
+ if s := findTitle(c); s != "" {
+ return s
+ }
+ }
+ return ""
+}
+
+func FetchTitle(url string) (string, error) {
+ resp, err := http.Get(url)
+ if err != nil {
+ return "", err
+ }
+ defer resp.Body.Close()
+
+ doc, err := html.Parse(resp.Body)
+ if err != nil {
+ return "", err
+ }
+
+ return findTitle(doc), nil
+}