From 8f4345e7d02464edefddf6bd058194ee5602504b Mon Sep 17 00:00:00 2001 From: Robert Gerus Date: Tue, 22 Dec 2015 11:00:12 +0100 Subject: [PATCH] WIP --- bot/urltitle.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/bot/urltitle.go b/bot/urltitle.go index 8b2fe13..917c39c 100644 --- a/bot/urltitle.go +++ b/bot/urltitle.go @@ -19,6 +19,30 @@ 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 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) + } + return youtube(string(res)) +} + +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) + } + return youtube(string(res)) +} + func genericURLTitle(l string) string { title, err := httpGetXpath(l, "//head/title") if err == errElementNotFound { @@ -42,6 +66,14 @@ 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,