forked from nkanaev/yarr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
iframe.go
38 lines (33 loc) · 912 Bytes
/
iframe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package silo
import (
"fmt"
"net/url"
"regexp"
"strings"
)
var (
youtubeFrame = `<iframe src="https://www.youtube.com/embed/%s" width="560" height="315" frameborder="0" allowfullscreen></iframe>`
vimeoFrame = `<iframe src="https://player.vimeo.com/video/%s" width="640" height="360" frameborder="0" allowfullscreen></iframe>`
vimeoRegex = regexp.MustCompile(`\/(\d+)$`)
)
func VideoIFrame(link string) string {
l, err := url.Parse(link)
if err != nil {
return ""
}
youtubeID := ""
if l.Host == "www.youtube.com" && l.Path == "/watch" {
youtubeID = l.Query().Get("v")
} else if l.Host == "youtu.be" {
youtubeID = strings.TrimLeft(l.Path, "/")
}
if youtubeID != "" {
return fmt.Sprintf(youtubeFrame, youtubeID)
}
if l.Host == "vimeo.com" {
if matches := vimeoRegex.FindStringSubmatch(l.Path); len(matches) > 0 {
return fmt.Sprintf(vimeoFrame, matches[1])
}
}
return ""
}