summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert S. Gerus <ar@bash.org.pl>2015-12-22 11:38:11 +0100
committerRobert S. Gerus <ar@bash.org.pl>2015-12-22 11:38:11 +0100
commit183404c9e7fe12e64987aa2543281c1b8db9a28c (patch)
treef3e6f60e1a9ba1b6ca3e8ea04205711e2a7e6bc7
parentd2621d1c8a655c6fffd131678c375507c7b6f014 (diff)
parente577b1adb07529e79b244451d84b9e04321d3ffd (diff)
downloadgorepost-183404c9e7fe12e64987aa2543281c1b8db9a28c.tar.gz
gorepost-183404c9e7fe12e64987aa2543281c1b8db9a28c.tar.bz2
gorepost-183404c9e7fe12e64987aa2543281c1b8db9a28c.tar.xz
gorepost-183404c9e7fe12e64987aa2543281c1b8db9a28c.zip
Merge pull request #49 from arachnist/fix-youtube
Reworked url title fetching.
-rw-r--r--bot/plugins_test.go24
-rw-r--r--bot/urltitle.go70
2 files changed, 88 insertions, 6 deletions
diff --git a/bot/plugins_test.go b/bot/plugins_test.go
index 5b68922..d20602a 100644
--- a/bot/plugins_test.go
+++ b/bot/plugins_test.go
@@ -809,6 +809,30 @@ var variableOutputTestEvents = []struct {
outRegex: *regexp.MustCompile("^at:"),
function: at,
},
+ {
+ in: irc.Message{
+ Command: "PRIVMSG",
+ Trailing: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
+ Params: []string{"#testchan-1"},
+ Prefix: &irc.Prefix{
+ Name: "idontexist",
+ },
+ },
+ outRegex: *regexp.MustCompile("^↳ title: Rick Astley - Never Gonna Give You Up"),
+ function: linktitle,
+ },
+ {
+ in: irc.Message{
+ Command: "PRIVMSG",
+ Trailing: "http://youtu.be/dQw4w9WgXcQ",
+ Params: []string{"#testchan-1"},
+ Prefix: &irc.Prefix{
+ Name: "idontexist",
+ },
+ },
+ outRegex: *regexp.MustCompile("^↳ title: Rick Astley - Never Gonna Give You Up"),
+ function: linktitle,
+ },
}
func TestFunctionsWithVariableOutput(t *testing.T) {
diff --git a/bot/urltitle.go b/bot/urltitle.go
index 2dc3adf..7e12fe9 100644
--- a/bot/urltitle.go
+++ b/bot/urltitle.go
@@ -5,6 +5,7 @@
package bot
import (
+ "encoding/json"
"fmt"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/transform"
@@ -19,7 +20,40 @@ var trimTitle *regexp.Regexp
var trimLink *regexp.Regexp
var enc = charmap.ISO8859_2
-func getURLTitle(l string) string {
+func youtube(vid string) string {
+ var dat map[string]interface{}
+ link := fmt.Sprintf("https://www.youtube.com/oembed?format=json&url=http://www.youtube.com/watch?v=%+v", vid)
+ data, err := httpGet(link)
+ if err != nil {
+ return "error getting data from youtube"
+ }
+
+ err = json.Unmarshal(data, &dat)
+ if err != nil {
+ return "error decoding data from youtube"
+ }
+ return dat["title"].(string)
+}
+
+func youtubeLong(l string) string {
+ pattern := regexp.MustCompile(`/watch[?]v[=](?P<vid>[a-zA-Z0-9-_]+)`)
+ res := []byte{}
+ for _, s := range pattern.FindAllSubmatchIndex([]byte(l), -1) {
+ res = pattern.ExpandString(res, "$vid", l, s)
+ }
+ return youtube(string(res))
+}
+
+func youtubeShort(l string) string {
+ pattern := regexp.MustCompile(`youtu.be/(?P<vid>[a-zA-Z0-9-_]+)`)
+ res := []byte{}
+ for _, s := range pattern.FindAllSubmatchIndex([]byte(l), -1) {
+ res = pattern.ExpandString(res, "$vid", l, s)
+ }
+ return youtube(string(res))
+}
+
+func genericURLTitle(l string) string {
title, err := httpGetXpath(l, "//head/title")
if err == errElementNotFound {
return "no title"
@@ -38,6 +72,24 @@ func getURLTitle(l string) string {
return title
}
+var customDataFetchers = []struct {
+ re *regexp.Regexp
+ fetcher func(l string) string
+}{
+ {
+ re: regexp.MustCompile("//(www.)?youtube.com/"),
+ fetcher: youtubeLong,
+ },
+ {
+ re: regexp.MustCompile("//youtu.be/"),
+ fetcher: youtubeShort,
+ },
+ {
+ re: regexp.MustCompile(".*"),
+ fetcher: genericURLTitle,
+ },
+}
+
func linktitle(output func(irc.Message), msg irc.Message) {
var r []string
@@ -51,9 +103,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 +124,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)
}