To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

youtubedl_test.go 3.64 KB
Newer Older
1
package youtubedl
2
3
4

import (
	"fmt"
5
	log "github.com/sirupsen/logrus"
6
	"github.com/stretchr/testify/assert"
7
	"os"
8
	"os/exec"
9
	"path/filepath"
10
11
12
	"testing"
)

13
14
func TestMain(m *testing.M) {
	// Make sure youtube-dl binary exists
15
	path, err := exec.LookPath("youtube-dl")
16
17
18
	if err != nil {
		log.Fatal(err)
	}
19
20
21
22
23
	log.Warn(path)
	cmd := exec.Command(path, "--version")
	out, err := cmd.Output()
	log.Warn(string(out))
	log.Warn(err)
24
25
26
	os.Exit(m.Run())
}

27
func TestGetPlaylistVideos(t *testing.T) {
28
	assert.Equal(t, true, os.Getenv("YOUTUBE_DATA_API_KEY") != "")
29
	videos, err := GetPlaylistVideos("PLpTJzCmGYaZFjE7jfH1tKFZt9rtqvvuj0", os.Getenv("YOUTUBE_DATA_API_KEY"))
Christof Gerber's avatar
Christof Gerber committed
30
	assert.Equal(t, nil, err)
31
	assert.Equal(t, 3, len(videos))
Christof Gerber's avatar
Christof Gerber committed
32
33
}

34
func TestGetYouTubeDownloadLink(t *testing.T) {
35
	video, audio, err := GetVideoURL("OsFEV35tWsg")
36
	assert.Equal(t, nil, err)
37
38
	assert.Equal(t, "/videoplayback", video.Path)
	assert.Equal(t, "/videoplayback", audio.Path)
39
40
}

41
42
43
44
func TestVideoIDToSub(t *testing.T) {

	info, err := getVideoInfo("OsFEV35tWsg")
	assert.Equal(t, nil, err)
45
46
47
48
49
50
51
52
53
	if tags, ok := info["tags"].([]interface{}); ok {
		if tag, ok := tags[0].(string); ok {
			assert.Equal(t, "TED", tag)
		} else {
			t.Errorf("got data of type %T but wanted string", tag)
		}
	} else {
		t.Errorf("got data of type %T but wanted []interface{}", info["tags"])
	}
54
55
56
57
58
59
}

func TestListSubs(t *testing.T) {

	subs, err := listSubs("OsFEV35tWsg")
	assert.Equal(t, nil, err)
60
61
62
63
	assert.Equal(t, "vtt", subs.AutomaticCaptions["en"][0])
	assert.Equal(t, "ttml", subs.AutomaticCaptions["en"][1])
	assert.Equal(t, "vtt", subs.Subtitles["en"][0])
	assert.Equal(t, "ttml", subs.Subtitles["en"][1])
64
65
	assert.Equal(t, true, len(subs.Subtitles) > 0)
	assert.Equal(t, true, len(subs.AutomaticCaptions) > 0)
66
67
68
69
70
71

}

func TestFindSub(t *testing.T) {
	formats, err := findSub("OsFEV35tWsg", "en")
	assert.Equal(t, nil, err)
72
	assert.Equal(t, true, len(formats) > 0)
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
	assert.Equal(t, "vtt", formats[0])
	assert.Equal(t, "ttml", formats[1])
}

func TestSubDownload(t *testing.T) {

	id := "OsFEV35tWsg"
	language := "en"
	format := "vtt"
	filename := filepath.Join("/tmp", "sub-"+id)
	err := downloadSubtitle(false, id, language, format, filename)
	assert.Equal(t, nil, err)
	fileWithSuffix := filename + "." + language + "." + format
	_, err = os.Stat(fileWithSuffix)
	assert.Equal(t, nil, err)
	assert.Equal(t, false, os.IsNotExist(err))
	assert.Equal(t, nil, os.Remove(fileWithSuffix))
}

func TestSubDownloadLanguageNotSupported(t *testing.T) {

	id := "OsFEV35tWsg"
	language := "zz"
	format := "vtt"
	filename := filepath.Join("/tmp", "sub-"+id)
	err := downloadSubtitle(false, id, language, format, filename)
	assert.Equal(t, fmt.Sprintf("subtitles language %s not available", language), err.Error())
}

func TestSubDownloadFormatNotSupported(t *testing.T) {

	id := "OsFEV35tWsg"
	language := "en"
	format := "srt"
	filename := filepath.Join("/tmp", "sub-"+id)
	err := downloadSubtitle(false, id, language, format, filename)
	assert.Equal(t, fmt.Sprintf("subtitle format %s not found", format), err.Error())
}

func TestAutomaticCaptionDownload(t *testing.T) {

	id := "OsFEV35tWsg"
	language := "en"
	format := "vtt"
	filename := filepath.Join("/tmp", "automaticCaption-"+id)
	err := downloadSubtitle(true, id, language, format, filename)
	assert.Equal(t, nil, err)
	fileWithSuffix := filename + "." + language + "." + format
	_, err = os.Stat(fileWithSuffix)
	assert.Equal(t, nil, err)
	assert.Equal(t, false, os.IsNotExist(err))
	assert.Equal(t, nil, os.Remove(fileWithSuffix))
}

func TestGetSubtitle(t *testing.T) {
	sub, err := GetSubtitle(false, "OsFEV35tWsg", "en")
	assert.Equal(t, nil, err)
	assert.Equal(t, "Philosophers, dramatists, theologians", sub.Items[0].String())
131
}