-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo.go
110 lines (91 loc) · 2.3 KB
/
video.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
package gotube
import (
"github.com/KiritoNya/gotube/internal/pkg/api"
)
// Video is a type that contains video information
type Video struct {
Id string
Url string
Title string
Description string
Thumbnails Thumbnails
PublishedAt string
Channel *Channel
Options []*VideoOptions
links map[string]map[string]api.Modality
}
// VideoOptions are option of video
type VideoOptions struct {
Format Format
Quality Quality
}
// Format of video
type Format string
var (
FormatMp4 Format = "mp4"
FormatM4a Format = "m4a"
FormatMp3 Format = "mp3"
Format3gp Format = "3gp"
)
// Quality of video
type Quality string
const (
QualityVideo144 Quality = "144p"
QualityVideo240 Quality = "240p"
QualityVideo480 Quality = "480p"
QualityVideo630 Quality = "630p"
QualityVideo720 Quality = "720p"
QualityVideo1080p Quality = "1080p"
QualityAudio128 Quality = "128kbps"
)
// NewVideoById is a function that creates a new Video instance by id
func NewVideoById(id string) (*Video, error) {
return newVideo(VideoBaseUrl + id)
}
// NewVideoByUrl is a function that creates a new Video instance by url
func NewVideoByUrl(url string) (*Video, error) {
return newVideo(url)
}
// GetDirectLink is a Video method that returns the direct link of the video
func (v *Video) GetDirectLink(opts *VideoOptions) (string, error) {
token := v.extractToken(
api.Options{
Format: api.Format(opts.Format),
Quality: string(opts.Quality),
},
)
rc, err := api.NewResponseConvert(v.Id, token)
if err != nil {
return "", err
}
return rc.Url, nil
}
// extractToken is a utility method of Video for extract the option token
func (v *Video) extractToken(opts api.Options) string {
var ri api.ResponseIndex
ri.Links = v.links
return ri.ExtractToken(opts)
}
// newVideo is a utility function for create a new video
func newVideo(url string) (*Video, error) {
var v Video
// Create responseIndex object
ri, err := api.NewResponseIndex(url)
if err != nil {
return nil, err
}
opts := ri.VideoOptions()
var vopts []*VideoOptions
for _, opt := range opts {
var v VideoOptions
v.Format = Format(opt.Format)
v.Quality = Quality(opt.Quality)
vopts = append(vopts, &v)
}
v.Url = url
v.Id = ri.VideoId
v.Title = ri.Title
v.Options = vopts
v.links = ri.Links
return &v, nil
}