summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert Gerus <rgerus@dpd.com.pl>2015-12-22 08:10:18 +0100
committerRobert Gerus <rgerus@dpd.com.pl>2015-12-22 08:10:18 +0100
commit42e4ecddfa1bb43e6e8efb14c5bde8f6db8b8220 (patch)
treeee00ee1ed0a70f4c5fae9a8d870df53fdb299228
parentd2621d1c8a655c6fffd131678c375507c7b6f014 (diff)
downloadgorepost-42e4ecddfa1bb43e6e8efb14c5bde8f6db8b8220.tar.gz
gorepost-42e4ecddfa1bb43e6e8efb14c5bde8f6db8b8220.tar.bz2
gorepost-42e4ecddfa1bb43e6e8efb14c5bde8f6db8b8220.tar.xz
gorepost-42e4ecddfa1bb43e6e8efb14c5bde8f6db8b8220.zip
Reworked url title fetching.
It should now be possible to add custom fetchers for urls where we might benefit from it.
-rw-r--r--bot/urltitle.go28
1 files changed, 22 insertions, 6 deletions
diff --git a/bot/urltitle.go b/bot/urltitle.go
index 2dc3adf..8b2fe13 100644
--- a/bot/urltitle.go
+++ b/bot/urltitle.go
@@ -19,7 +19,7 @@ var trimTitle *regexp.Regexp
var trimLink *regexp.Regexp
var enc = charmap.ISO8859_2
-func getURLTitle(l string) string {
+func genericURLTitle(l string) string {
title, err := httpGetXpath(l, "//head/title")
if err == errElementNotFound {
return "no title"
@@ -38,6 +38,16 @@ func getURLTitle(l string) string {
return title
}
+var customDataFetchers = []struct {
+ re *regexp.Regexp
+ fetcher func(l string) string
+}{
+ {
+ re: regexp.MustCompile(".*"),
+ fetcher: genericURLTitle,
+ },
+}
+
func linktitle(output func(irc.Message), msg irc.Message) {
var r []string
@@ -51,9 +61,15 @@ func linktitle(output func(irc.Message), msg irc.Message) {
s = string(trimLink.ReplaceAll([]byte(s), []byte("http"))[:])
if b {
- t := getURLTitle(s)
- if t != "no title" {
- r = append(r, t)
+ FetchersLoop:
+ for _, d := range customDataFetchers {
+ if d.re.MatchString(s) {
+ t := d.fetcher(s)
+ if t != "no title" {
+ r = append(r, t)
+ }
+ break FetchersLoop
+ }
}
}
}
@@ -66,7 +82,7 @@ func linktitle(output func(irc.Message), msg irc.Message) {
}
func init() {
- trimTitle, _ = regexp.Compile("[\\s]+")
- trimLink, _ = regexp.Compile("^.*?http")
+ trimTitle = regexp.MustCompile("[\\s]+")
+ trimLink = regexp.MustCompile("^.*?http")
addCallback("PRIVMSG", "LINKTITLE", linktitle)
}