Merge pull request #49 from arachnist/fix-youtube

Reworked url title fetching.
master
Robert S. Gerus 2015-12-22 11:38:11 +01:00
commit 183404c9e7
2 changed files with 88 additions and 6 deletions

View File

@ -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) {

View File

@ -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)
}