summaryrefslogtreecommitdiff
path: root/importers/vimeo_importer.go
blob: 0efec7ffc6c995c03f037cc84620da50c478e36e (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
package importers

import (
	"fmt"
	"strings"

	"git.mills.io/prologic/vimeodl"
)

type VimeoImporter struct{}

func (i *VimeoImporter) GetVideoInfo(url string) (videoInfo VideoInfo, err error) {
	if strings.HasPrefix(strings.ToLower(url), "vimeo:") {
		url = strings.TrimSpace(strings.SplitN(url, ":", 2)[1])
	}

	if !strings.HasPrefix(url, "http") {
		url = "https://player.vimeo.com/video/" + url
	}

	if !strings.HasPrefix(url, "https://player.vimeo.com/video/") {
		playerURL, err := vimeodl.GetPlayerURL(url)
		if err != nil {
			err := fmt.Errorf("error finding player url: %w", err)
			return VideoInfo{}, err
		}
		url = playerURL
	}

	if !strings.HasSuffix(url, "/") {
		url += "/"
	}

	url += "config"

	config, err := vimeodl.GetVideoConfig(url)
	if err != nil {
		err := fmt.Errorf("error retrieving video config: %w", err)
		return VideoInfo{}, err
	}

	videoInfo.VideoURL = vimeodl.PickBestVideo(config)

	videoInfo.ThumbnailURL = vimeodl.PickBestThumbnail(config)

	videoInfo.ID = string(config.Video.Id)
	videoInfo.Title = config.Video.Title

	return
}