-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclients.go
133 lines (116 loc) · 3.82 KB
/
clients.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package youtube_go
import (
"errors"
"net/http"
)
// InnerTube struct
type InnerTube struct {
Adaptor Adaptor
}
// Adaptor interface
type Adaptor interface {
Dispatch(endpoint string, params map[string]string, body map[string]interface{}) (map[string]interface{}, error)
}
// NewInnerTube creates a new InnerTube instance
func NewInnerTube(httpClient *http.Client, clientName, clientVersion string, apiKey, userAgent, referer string, locale *Locale, auto bool) (*InnerTube, error) {
if clientName == "" {
return nil, errors.New("Precondition failed: Missing client name")
}
if clientVersion == "" {
return nil, errors.New("Precondition failed: Missing client version")
}
context := ClientContext{}
if auto {
context = GetContext(clientName)
} else {
context = ClientContext{
ClientName: clientName,
ClientVersion: clientVersion,
APIKey: apiKey,
UserAgent: userAgent,
Referer: referer,
Locale: locale,
}
}
return &InnerTube{
Adaptor: NewInnerTubeAdaptor(context, httpClient),
}, nil
}
// Call method to make requests
func (it *InnerTube) Call(endpoint string, params map[string]string, body map[string]interface{}) (map[string]interface{}, error) {
response, err := it.Adaptor.Dispatch(endpoint, params, body)
if err != nil {
return nil, err
}
delete(response, "responseContext") // Remove responseContext if exists
return response, nil
}
// Example API call methods
func (it *InnerTube) Config() (map[string]interface{}, error) {
return it.Call("CONFIG", nil, nil)
}
func (it *InnerTube) Guide() (map[string]interface{}, error) {
return it.Call("GUIDE", nil, nil)
}
func (it *InnerTube) Player(videoID string) (map[string]interface{}, error) {
return it.Call("PLAYER", nil, Filter(map[string]interface{}{
"videoId": videoID,
}))
}
func (it *InnerTube) Browse(browseID *string, params *string, continuation *string) (map[string]interface{}, error) {
body := map[string]interface{}{
"browseId": browseID,
"params": params,
"continuation": continuation,
}
//fmt.Println("body: ", body)
//fmt.Println("Filter(body): ", Filter(body))
return it.Call("BROWSE", nil, Filter(body))
}
func (it *InnerTube) Search(query *string, params *string, continuation *string) (map[string]interface{}, error) {
body := map[string]interface{}{
"query": query,
"params": params,
"continuation": continuation,
}
//fmt.Println("body: ", body)
//fmt.Println("Filter(body): ", Filter(body))
return it.Call("SEARCH", nil, Filter(body))
}
func (it *InnerTube) Next(videoId *string, playlistId *string, params *string, index *int, continuation *string) (map[string]interface{}, error) {
body := map[string]interface{}{
"videoId": videoId,
"playlistId": playlistId,
"params": params,
"playlistIndex": index,
"continuation": continuation,
}
//fmt.Println("body: ", body)
//fmt.Println("Filter(body): ", Filter(body))
return it.Call("NEXT", nil, Filter(body))
}
func (it *InnerTube) GetTranscript(params *string) (map[string]interface{}, error) {
body := map[string]interface{}{
"params": params,
}
//fmt.Println("body: ", body)
//fmt.Println("Filter(body): ", Filter(body))
return it.Call("GET_TRANSCRIPT", nil, Filter(body))
}
func (it *InnerTube) MusicGetSearchSuggestions(input *string) (map[string]interface{}, error) {
body := map[string]interface{}{
"input": input,
}
//fmt.Println("body: ", body)
//fmt.Println("Filter(body): ", Filter(body))
return it.Call("MUSIC/GET_SEARCH_SUGGESTIONS", nil, Filter(body))
}
func (it *InnerTube) MusicGetQueue(videoIds *[]string, playlistId *string) (map[string]interface{}, error) {
body := map[string]interface{}{
"playlistId": playlistId,
"videoIds": videoIds,
}
//fmt.Println("body: ", body)
//fmt.Println("Filter(body): ", Filter(body))
return it.Call("MUSIC/GET_QUEUE", nil, Filter(body))
}