From 7d16d62b0c02f8abdb72f52cc73a2e5c8978a0dc Mon Sep 17 00:00:00 2001 From: Robert Gerus Date: Tue, 22 Dec 2015 11:30:34 +0100 Subject: [PATCH] It's working now. Still need to add tests. --- bot/urltitle.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/bot/urltitle.go b/bot/urltitle.go index 506c9dc..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,17 +20,26 @@ var trimTitle *regexp.Regexp var trimLink *regexp.Regexp var enc = charmap.ISO8859_2 -func youtube(l string) string { - // get data from - // https://www.youtube.com/oembed?format=json&url=http://www.youtube.com/watch?v=dQw4w9WgXcQ - return "placeholder" +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[a-zA-Z0-9-_]+)`) res := []byte{} for _, s := range pattern.FindAllSubmatchIndex([]byte(l), -1) { - res = pattern.ExpandString(res, "$cmd", l, s) + res = pattern.ExpandString(res, "$vid", l, s) } return youtube(string(res)) } @@ -38,7 +48,7 @@ func youtubeShort(l string) string { pattern := regexp.MustCompile(`youtu.be/(?P[a-zA-Z0-9-_]+)`) res := []byte{} for _, s := range pattern.FindAllSubmatchIndex([]byte(l), -1) { - res = pattern.ExpandString(res, "$cmd", l, s) + res = pattern.ExpandString(res, "$vid", l, s) } return youtube(string(res)) }