summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--href.go20
-rw-r--r--main.go21
2 files changed, 35 insertions, 6 deletions
diff --git a/href.go b/href.go
index 3a9239f..44a6efb 100644
--- a/href.go
+++ b/href.go
@@ -3,10 +3,13 @@ package main
import (
"net/http"
"strings"
+ "fmt"
"golang.org/x/net/html"
)
+var cache = make(map[string]string)
+
func findTitle(n *html.Node) (s string) {
if n.Type == html.ElementNode && n.Data == "title" {
for c := n.FirstChild; c != nil; c = c.NextSibling {
@@ -23,6 +26,10 @@ func findTitle(n *html.Node) (s string) {
}
func FetchTitle(url string) (string, error) {
+ if title, ok := cache[url]; ok {
+ return title, nil
+ }
+
resp, err := http.Get(url)
if err != nil {
return "", err
@@ -34,5 +41,16 @@ func FetchTitle(url string) (string, error) {
return "", err
}
- return findTitle(doc), nil
+ title := findTitle(doc)
+ cache[url] = title
+
+ return title, nil
+}
+
+func showLinks() (s string) {
+ for k, v := range cache {
+ s += fmt.Sprintf("%v (%v) ", k, v)
+ }
+ return
+
}
diff --git a/main.go b/main.go
index b2b4924..8a02ea3 100644
--- a/main.go
+++ b/main.go
@@ -31,11 +31,12 @@ type Command struct {
}
type (
- Last struct{ Command }
- RSS struct{ Command }
- Theo struct{ Command }
- Help struct{ Command }
- Top struct{ Command }
+ Last struct{ Command }
+ RSS struct{ Command }
+ Theo struct{ Command }
+ Help struct{ Command }
+ Top struct{ Command }
+ Links struct{ Command }
)
var (
@@ -96,6 +97,10 @@ func (_ Top) Handle(conn *irc.Conn, line *irc.Line) {
conn.Privmsg(line.Target(), s)
}
+func (_ Links) Handle(conn *irc.Conn, line *irc.Line) {
+ conn.Privmsg(line.Nick, showLinks())
+}
+
func privmsg(conn *irc.Conn, line *irc.Line) {
f := strings.Fields(line.Text())
@@ -177,6 +182,12 @@ func init() {
Help: "Top 10 flooder",
},
})
+ Register("links", Links{
+ Command{
+ Help: "Show cached links",
+ },
+ })
+
}
func main() {