-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.go
137 lines (119 loc) · 2.68 KB
/
models.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
134
135
136
137
package youtube_go
import (
"fmt"
"net/http"
"strconv"
"strings"
)
type Locale struct {
hl string
gl *string
}
func (l *Locale) AcceptLanguage() string {
langs := []string{l.hl}
if l.gl != nil {
langs = append(langs, *l.gl)
}
return strings.Join(langs, ",")
}
type Error struct {
code int
message string
reason string
}
func (e *Error) String() string {
return fmt.Sprintf("%d %s: %s", e.code, http.StatusText(e.code), e.message)
}
func (e *Error) Code() int {
return e.code
}
type ClientContext struct {
ClientName string
ClientVersion string
ClientID int
APIKey string
UserAgent string
Referer string
Locale *Locale
XGoogVisitorId string
}
func (c *ClientContext) Params() map[string]string {
params := map[string]string{
"alt": "json",
}
if c.APIKey != "" {
params["key"] = c.APIKey
}
return params
}
func (c *ClientContext) Context() map[string]string {
return map[string]string{
"clientName": c.ClientName,
"clientVersion": c.ClientVersion,
}
}
func (c *ClientContext) Headers() http.Header {
// 将 map[string]string 转换为 http.Header
headers2 := http.Header{}
headers2.Add("Host", config.Host)
headers2.Add("Accept", "*/*")
headers2.Add("Accept-Encoding", "gzip, deflate")
headers2.Add("Connection", "keep-alive")
headers2.Add("Content-Type", "application/json")
headers2.Add("X-Goog-Api-Format-Version", "1")
headers2.Add("X-YouTube-Client-Name", fmt.Sprintf("%s", strconv.Itoa(c.ClientID)))
headers2.Add("X-YouTube-Client-Version", c.ClientVersion)
headers := map[string]string{
"X-Goog-Api-Format-Version": "1",
"X-YouTube-Client-Name": fmt.Sprintf("%s", strconv.Itoa(c.ClientID)),
"X-YouTube-Client-Version": c.ClientVersion,
}
if c.UserAgent != "" {
headers["User-Agent"] = c.UserAgent
headers2.Add("User-Agent", c.UserAgent)
}
if c.Referer != "" {
headers["Referer"] = c.Referer
headers2.Add("Referer", c.Referer)
}
if c.Locale != nil {
headers["Accept-Language"] = c.Locale.AcceptLanguage()
headers2.Add("Accept-Language", c.Locale.AcceptLanguage())
}
if c.XGoogVisitorId != "" {
headers2.Add("X-Goog-Visitor-Id", c.XGoogVisitorId)
}
return headers2
}
type Config struct {
Host string
BaseURL string
Clients []ClientContext
}
type ResponseContext struct {
Function *string
BrowseID *string
Context *string
VisitorData *string
Client Client
Request Request
Flags Flags
}
type Request struct {
Type *string
ID *string
}
type Client struct {
Name *string
Version *string
}
type Flags struct {
LoggedIn *bool
}
type ResponseFingerprint struct {
Request *string
Function *string
BrowseID *string
Context *string
Client *string
}