-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
244 lines (202 loc) · 6.04 KB
/
main.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"os"
"github.com/joho/godotenv"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
var (
query = flag.String("query", "Google", "Search term")
id = flag.String("id", "", "Video/channel/playlist id")
maxResults = flag.Int64("max-results", 5, "Max YouTube results")
)
func handleError(err error, message string) {
if message == "" {
message = "Error making API call"
}
if err != nil {
log.Fatalf(message+": %v", err.Error())
}
}
const apiKeyEnv = "API_KEY"
// Print the ID and title of each result in a list as well as a name that
// identifies the list. For example, print the word section name "Videos"
// above a list of video search results, followed by the video ID and title
// of each matching video.
func printIDs(sectionName string, matches map[string]string) {
fmt.Printf("%v:\n", sectionName)
for id, title := range matches {
fmt.Printf("[%v] %v\n", id, title)
}
fmt.Printf("\n\n")
}
func search(s *youtube.Service, query string, maxResults int64) error {
// Make the API call to YouTube.
call := s.Search.List([]string{"id", "snippet"}).
Q(query).
MaxResults(maxResults)
response, err := call.Do()
if err != nil {
return err
}
// Group video, channel, and playlist results in separate lists.
videos := make(map[string]string)
channels := make(map[string]string)
playlists := make(map[string]string)
// Iterate through each item and add it to the correct list.
for _, item := range response.Items {
switch item.Id.Kind {
case "youtube#video":
videos[item.Id.VideoId] = item.Snippet.Title
case "youtube#channel":
channels[item.Id.ChannelId] = item.Snippet.Title
case "youtube#playlist":
playlists[item.Id.PlaylistId] = item.Snippet.Title
}
}
printIDs("Videos", videos)
printIDs("Channels", channels)
printIDs("Playlists", playlists)
return nil
}
func videoDetails(s *youtube.Service, id string, parts []string) error {
// Make the API call to YouTube.
fmt.Printf("get video %s, parts %v\n", id, parts)
call := s.Videos.List(parts).Id(id)
response, err := call.Do()
if err != nil {
return err
}
if len(response.Items) == 0 {
fmt.Printf("%+v\n", response)
return errors.New("no content found")
}
v := response.Items[0]
j, err := json.MarshalIndent(v, "", " ")
if err != nil {
return err
}
fmt.Println(string(j))
return nil
}
func playlistItems(s *youtube.Service, id string, parts []string, pageToken string) error {
fmt.Printf("get video %s, parts %v\n", id, parts)
call := s.PlaylistItems.List(parts).PlaylistId(id).MaxResults(3).Fields("items/snippet/title", "items/snippet/resourceId/videoId")
if pageToken != "" {
call = call.PageToken(pageToken)
}
response, err := call.Do()
if err != nil {
return err
}
if len(response.Items) == 0 {
fmt.Printf("%+v\n", response)
return errors.New("no content found")
}
j, err := json.MarshalIndent(response, "", " ")
if err != nil {
return err
}
fmt.Println(string(j))
return nil
}
func playlists(s *youtube.Service, id string, parts []string) error {
fmt.Printf("get playlist %s, parts %v\n", id, parts)
call := s.Playlists.List(parts).ChannelId(id).MaxResults(50).Fields("items/snippet/title", "items/id")
response, err := call.Do()
if err != nil {
return err
}
if len(response.Items) == 0 {
fmt.Printf("%+v\n", response)
return errors.New("no content found")
}
j, err := json.MarshalIndent(response, "", " ")
if err != nil {
return err
}
fmt.Println(string(j))
return nil
}
func channels(s *youtube.Service, id string, parts []string) error {
fmt.Printf("get channels %s, parts %v\n", id, parts)
call := s.Channels.List(parts).Id(id)
response, err := call.Do()
if err != nil {
return err
}
if len(response.Items) == 0 {
fmt.Printf("%+v\n", response)
return errors.New("no content found")
}
j, err := json.MarshalIndent(response, "", " ")
if err != nil {
return err
}
fmt.Println(string(j))
return nil
}
func getChannelID(s *youtube.Service, channelName string) (string, error) {
fmt.Printf("get channels %s\n", channelName)
call := s.Search.List([]string{"id"}).
Q(channelName).
MaxResults(1).Type("channel")
response, err := call.Do()
if err != nil {
return "", err
}
if len(response.Items) == 0 {
return "", errors.New("no result found")
}
item := response.Items[0]
if item.Id.Kind != "youtube#channel" {
return "", errors.New("result not channel type")
}
j, err := json.MarshalIndent(response, "", " ")
if err != nil {
return "", err
}
fmt.Println(string(j))
return item.Id.ChannelId, nil
}
func getUploadsPlaylistID(s *youtube.Service, channelID string) (string, error) {
call := s.Channels.List([]string{"contentDetails"}).MaxResults(1).Id(channelID).Fields("items/contentDetails/relatedPlaylists/uploads")
response, err := call.Do()
if err != nil {
return "", err
}
return response.Items[0].ContentDetails.RelatedPlaylists.Uploads, nil
}
func main() {
flag.Parse()
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
apiKey := os.Getenv(apiKeyEnv)
ctx := context.Background()
service, err := youtube.NewService(ctx, option.WithAPIKey(apiKey))
if err != nil {
log.Fatalf("Error creating new YouTube client: %v", err)
}
// handleError(videoDetails(service, *videoID, []string{"snippet", "player", "topicDetails", "recordingDetails"}), "show video details")
// handleError(playlistItems(service, *id, []string{"snippet"}, ""), "show playlist")
// handleError(playlists(service, *id, []string{"snippet"}), "show playlists")
// handleError(channels(service, *id, []string{"contentDetails"}), "show playlists")
// uploads, err := getUploadsPlaylistID(service, *id)
// handleError(err, "getUploadsPlaylistID")
// fmt.Println(uploads)
// handleError(playlistItems(service, uploads, []string{"snippet"}, ""), "show playlist")
chID, err := getChannelID(service, *id)
handleError(err, "getChannelID")
fmt.Println(chID)
}
// my channel UC64mIIOlYMWB5ac6VoaRj8w
// dailydev UCXUjtTfQWJa0G9K_SqRm3jQ
// https://www.youtube.com/@dailydotdev/shorts