summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert Gerus <rgerus@dpd.com.pl>2015-12-22 11:30:34 +0100
committerRobert Gerus <rgerus@dpd.com.pl>2015-12-22 11:30:34 +0100
commit7d16d62b0c02f8abdb72f52cc73a2e5c8978a0dc (patch)
treea71c1a4713ce91d8ec84ae69af0e703fafcb73d5
parent66f207023ef33b969c3a5b6e71937719a4bcf13f (diff)
downloadgorepost-7d16d62b0c02f8abdb72f52cc73a2e5c8978a0dc.tar.gz
gorepost-7d16d62b0c02f8abdb72f52cc73a2e5c8978a0dc.tar.bz2
gorepost-7d16d62b0c02f8abdb72f52cc73a2e5c8978a0dc.tar.xz
gorepost-7d16d62b0c02f8abdb72f52cc73a2e5c8978a0dc.zip
It's working now.
Still need to add tests.
-rw-r--r--bot/urltitle.go22
1 files 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<vid>[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<vid>[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))
}